Autocmd seems to not trigger cinkeys - vim

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

Related

In gVim, how do I unbold this text?

Title, I want the code in the image attached to be unbolded. Here is my _vimrc for context:
call plug#begin('~/AppData/Local/nvim/plugged')
Plug 'jiangmiao/auto-pairs'
call plug#end()
au GUIEnter * simalt ~x
set is
set cb=unnamed
set nu
set gfn=#Fixedsys:h10
set backspace=indent,eol,start
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
autocmd filetype cpp nnoremap <F9> :w <bar> !g++ -std=c++14 % -o %:r -Wl,--stack,268435456<CR>
autocmd filetype cpp nnoremap <F10> :!%:r<CR>
autocmd filetype cpp nnoremap <C-C> :s/^\(\s*\)/\1\/\/<CR> :s/^\(\s*\)\/\/\/\//\1<CR> $

Vim autocmd for FileType not working indirectly

I have a custom ~/.vimrc I made with this augroup:
augroup filetype_vim
autocmd!
autocmd! BufWritePost .vimrc* source %
autocmd FileType vim |
setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
augroup END
When I open vim directly to edit the ~/.vimrc like this: vim ~/.vimrc, the folding works as expected, I can fold {{{ marker:
:set foldmethod?
> foldmethod=marker
When I open vim without specifying a file: vim, and then trying to edit: :e ~/.vimrc, the foldmethod is different!
:set foldmethod?
> foldmethod=syntax
Which obviously comes from a different part of my vimrc.
Why doesn't it recognizes the file type when I open the file indirectly?
You've failed with VimScript syntax. Must be
autocmd FileType vim
\ setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
What you did instead is
autocmd FileType vim <nothing> | <nothing>
setlocal foo bar
Therefore setlocal applies to the current buffer only (i.e. command-line argument), not to anything else.

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.

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

How do I tidy up my vimrc file?

My Vim configuration file is getting bigger (15KB+). And I try not to make my vim launch slower by sourcing more (larger) files at startup. For the same purpose I use only essential plugins and I try to keep the number of plugin as less as possible.
So, somewhere in my .vimrc file, I have these lines:
autocmd FileType python setlocal expandtab shiftwidth=4 tabstop=4 softtabstop=4
autocmd FileType python setlocal textwidth=78
autocmd FileType python match ErrorMsg '\%>80v.\+'
autocmd FileType python inoremap <F5> <esc>:upd\|!python %<cr>
autocmd FileType python nnoremap <F5> :upd\|!python %<cr>
autocmd FileType python nnoremap <leader>8 :w\|call Flake8()<cr>
autocmd FileType python setlocal formatoptions-=t
autocmd BufWritePost *.py call Flake8()
Now I see in first 7 lines, all lines have autocmd FileType python in common. So my thinking is, if we manage to replace all those word with something less then Vim will fire up faster. But I don't know how to do that.
Can we group them? How? Anything else?
Just put
setlocal expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=78 formatoptions-=t
match ErrorMsg '\%>80v.\+'
inoremap <F5> <esc>:upd\|!python %<cr>
nnoremap <F5> :upd\|!python %<cr>
nnoremap <leader>8 :w\|call Flake8()<cr>
in ~/.vim/after/ftplugin/python.vim.
Vim will read this file only when you open a Python file.
I've been meaning to do this for some time, actually. And for the same reasons.
Filetype-specific stuff should be moved to ~/.vim/after/ftplugin/{filetype}.vim, as romainl has already pointed out.
I moved all my custom mappings and commands into ~/.vim/plugin/my{mappings,commands}.vim, and mostly only put real settings (the :set commands) and plugin customizations into .vimrc. Any mappings / commands that aren't simple one-liners and delegate to functions then use the autoload mechanism. This keeps the amount of stuff read at startup small.
TL,DR: Autoload is great; all plugins should use it.
I am using a scheme where a group of options is set in a function that is called by an auto command.
function! s:C_options()
setlocal cinoptions={0,:1s,g1s,t0,(0,=.5s
setlocal noautoindent
setlocal nosmartindent
setlocal cindent
call s:PROG_options()
endfunction
autocmd BufRead,BufNewFile *.c call s:C_options()
It is quite similar to using a filetype but you can use nested call, like having general programming options in s:PROG_options(), so you should be able to reduce further the size of your .vimrc.
The after\filetypesolution might be more efficient regarding initial load time, but I would rather have most of my customization in a single .vimrc than scattered in the .vimdirectory.
At least you can merge the first two lines with the 7th
autocmd FileType python setlocal expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=78 formatoptions-=t
autocmd FileType python match ErrorMsg '\%>80v.\+'
autocmd FileType python inoremap <F5> <esc>:upd\|!python %<cr>
autocmd FileType python nnoremap <F5> :upd\|!python %<cr>
autocmd FileType python nnoremap <leader>8 :w\|call Flake8()<cr>
autocmd BufWritePost *.py call Flake8()
But I can't imagine how you can get rid of the others. On the other hand I don't think that these autocmd commands are taking so long to execute.

Resources