VIM: Indentation changes when opening Python files - vim

when I open Python files my indentation changes. I have the following vimrc settings:
set number
colorscheme wombat256mod
set guifont=Consolas\ 11
set linespace=2
filetype on
filetype plugin on
au BufRead,BufNewFile *py,*pyw,*.c,*.h set tabstop=4
au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
fu Select_c_style()
if search('^\t', 'n', 150)
set shiftwidth=8
set noexpandtab
el
set shiftwidth=4
set expandtab
en
endf
au BufRead,BufNewFile *.c,*.h call Select_c_style()
au BufRead,BufNewFile Makefile* set noexpandtab
highlight BadWhitespace ctermbg=red guibg=red
au BufRead,BufNewFile *.py,*.pyw match BadWhitespace /^\t\+/
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h set textwidth=79
au BufRead,BufNewFile *.c,*.h set formatoptions-=c formatoptions-=o formatoptions-=r
au BufNewFile *.py,*.pyw,*.c,*.h set fileformat=unix
let python_highlight_all=1
syntax on
filetype indent on
set autoindent
Anybody see something that can cause it?
For example I open an *.html file the indentation is a tab. But once I opened any Python file and go back to the html file the indentation switches to python preferences.

I have an addition to #dash-tom-bang's answer: both shiftwidth and expandtab options are local to buffer. No need to add any autocommands, just change all set statements with setlocal, so it will not alter the defaults.

The au lines with *.py in them are what are changing your settings. Notably,
au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
You need to "undo" these settings when opening HTML files. Just add a few more autocmds that work on *.html to set noexpandtab and set your shiftwidth back to 8.

I would suggest saving http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc at some place and sourcing it from .vimrc. That should save hassles.
EDIT: Oh, well apparently, that's already something you're doing!

Related

set a colorscheme for a filetype in vim

Within vim, I am trying to use a special colorscheme for some filetypes using an autocmd. Strangely, that does not work for all the filetypes. Here is my vimrc:
autocmd filetype troff :colorscheme troff
autocmd filetype tintin :colorscheme troff
autocmd BufNewFile,BufRead *.tt set ft=tintin
autocmd BufNewFile,BufRead *.tr set ft=troff
While openning f.tr, the colorscheme "troff" is used, but while openning f.tt, while the filetype is correctly set to "tintin", the default colorscheme is used. If I manually set the filetype (sef ft=tintin), then the colorscheme troff is loaded. Could you please help me to figure what could cause that strange behaviour?
I cannot reproduce your problem. However I would suggest the following auto commands:
autocmd BufNewFile,BufRead *.tt let g:tmpcolor=g:colors_name
autocmd BufNewFile,BufRead *.tr let g:tmpcolor=g:colors_name
autocmd BufEnter *.tt colorscheme troff | set ft=tintin
autocmd BufEnter *.tr colorscheme troff | set ft=troff
autocmd BufLeave *.tt exe 'colorscheme '.g:tmpcolor
autocmd BufLeave *.tr exe 'colorscheme '.g:tmpcolor
This will create a variable g:tmpcolor that stores the original color scheme. When you edit a file of type .tt or .tr the color scheme will change to troff. When you leave these files, the color scheme will change to g:tmpcolor.

Vim permanent syntax highlighting

I'm using :set syntax=javascript whenever I work on TypeScript files for example. I want to set the syntax automatically based on my file type. Now here's the thing, I've done it before by adding:
au BufNewFile,BufRead,BufReadPost *.ts set syntax=javascript
to my ~/.vimrc file, however, it doesn't seem to be working anymore.
Here is the contents of my .vimrc file. Is there a reason why this wouldn't work?
colorscheme atom-dark-256
set number
set autoindent
set guifont=monaco:h12
set expandtab
set tabstop=4 shiftwidth=4 expandtab
au BufNewFile,BufRead,BufReadPost *.ts set syntax=javascript
au BufNewFile,BufRead,BufReadPost *.handlebars set syntax=HTML
au BufNewFile,BufRead,BufReadPost *.twig set syntax=HTML
au BufNewFile,BufRead,BufReadPost *.theme set syntax=PHP
I'm sure it used to work just fine, but it just stopped for some reason. Any ideas? I'm using MacVim if that helps?
Let us set the filetype of *.ts as javascript so that it applies javascript syntax using
au BufEnter,BufRead *.ts set filetype=javascript

Vim - autocmd not working for C++ file type

I'm using Windows with Vim 7.4. In my _vimrc file I have
set ts=4 sw=4 sts=4
augroup Foo
au!
au BufRead,BufNewFile *.xml,*.css setlocal ts=2 sw=2 sts=2
au BufRead,BufNewFile *.h,*.cc setlocal ts=2 sw=2 sts=2
augroup END
When I open myFile.h or myFile.cc, TAB is set to 4 spaces not 2 spaces, but when I open MyFile.css, TAB is set to 2 spaces. It seems that for *.cc and *.h file, my autocmd not getting executed. Does anyone knows why and how can I fix this? (I want my TAB to be set to 4 spaces for *.cpp and *.hpp files and all other files except *.xml, *.css, *.h and *.cc).
You can just put them on one line.
set ts=4 sw=4 sts=4
augroup Foo
au!
au BufEnter,BufNewFile *.xml,*.css,*.h,*.cc setlocal ts=2 sw=2 sts=2
augroup END

unset a filetype option when switching to any other filetype?

I have this line in my .vimrc:
au filetype python set colorcolumn=80
I would prefer if the colorcolumn disappeared when I switched to a non-python buffer. Is there a way to accomplish this?
Use setlocal to set that to just the buffer you want.
au filetype python setlocal colorcolumn=80
You can also abbreviate it to setl
au filetype python setl colorcolumn=80

Autocmd seems to not trigger cinkeys

I have the following lines in my vimrc
" Python indenting and folding
au! FileType python set foldmethod=indent
au! FileType python set nosmartindent
" C++ indenting and folding
au! FileType cpp set cino=i-s
au! FileType cpp set cinkeys=0{,0},0),:,!^F,o,O,e
au! FileType cpp set cinkeys-=0#
au! FileType cpp set smartindent
au! FileType cpp set foldmethod=syntax
fu! FUNC_ECHOVAR(varname)
:let varstr=a:varname
:exec 'let g:foo = &'.varstr
:echo varstr.' = '.g:foo
endfu
command! -nargs=1 ECHOVAR :call FUNC_ECHOVAR(<f-args>)
func! MYINFO()
:ECHOVAR cino
:ECHOVAR cinkeys
:ECHOVAR foldmethod
:ECHOVAR filetype
:ECHOVAR smartindent
endfu
command! MYINFOCMD call MYINFO() <C-R>
when I open a C++ file and execute the MYINFOCMD command I see this printout
cino = {1s
cinkeys = 0{0,},),:,0#,!^F,o,O,e
foldmethod = syntax
filetype = cpp
smartindent = 0
I don't understand why the autocmd FileType cpp has failed to set these variables correctly, or at least the way I expected them to be set.
Does anyone know why my au! commands are not triggering when I load a .cpp file?
Use :au! at most once for each autocommand event and pattern, since it removes previously defined autocommands. (Use it in case your vimrc file gets sourced more than once, so the autocommands are not duplicated.) From :help autocmd-remove:
:au[tocmd]! [group] {event} {pat} [nested] {cmd}
Remove all autocommands associated with {event} and
{pat}, and add the command {cmd}. See
|autocmd-nested| for [nested].
Alternatively, put all your autocommands in a group and use :au! just once (:help autocmd-groups):
augroup Erotemic
au!
" Python indenting and folding
au FileType python set foldmethod=indent
au FileType python set nosmartindent
" C++ indenting and folding
au FileType cpp set cino=i-s
au FileType cpp set cinkeys=0{,0},0),:,!^F,o,O,e
au FileType cpp set cinkeys-=0#
au FileType cpp set smartindent
au FileType cpp set foldmethod=syntax
augroup END
Here is what happens when I try the first two of your autocommand lines, and then list the FileType python autocommands after each one:
:au! FileType python set foldmethod=indent
:au FileType python
--- Auto-Commands ---
FileType
python set foldmethod=indent
:au! FileType python set nosmartindent
:au FileType python
--- Auto-Commands ---
FileType
python set nosmartindent

Resources