I am trying to add :autocmd BufWrite * make to modeline in vim, but it is not triggered. I see that the help says only set <options> can be used in modelines. Is there a way to achieve this effect, where I write a file to run make.
Set your autocmd in your ~/.vimrc with a pattern that will match your file name. I would also suggest using augroup so it can be made to be re-sourced.
augroup my_project
autocmd!
autocmd BufWrite /project/*.c make
augroup END
For more help see:
:h :au
:h :aug
Related
I use Vim-Airline plug in Vim, but statuline incomplete display after source .vimrc, so I want use :redraw! in augroup but it not take effect.
augroup Vim
autocmd!
autocmd BufWritePost $MYVIMRC ++nested source $MYVIMRC ":redraw!"
augroup End
I can only exec :redraw! in command line.
How to improve my code?
I am not sure where those quotes around :redraw! come from or what you expect them to do since " introduces a comment.
In Vim, you use a "bar", |, to separate Ex commands like :source and :redraw:
augroup Vim
autocmd!
autocmd BufWritePost $MYVIMRC ++nested source $MYVIMRC | redraw!
augroup END
See :help :bar.
I am able to run it inside vim from the colon prompt.
When I put the same inside .vimrc, I get:
E492: Not an editor command: VirtualEnvActivate black
Plugins are loaded after .vimrc (:help startup). Use autocmd VimEnter * in .vimrc to set up commands to run later (:help :autocmd, :help :augroup, :help VimEnter):
augroup SetUpVirtualEnv
au!
au VimEnter * VirtualEnvActivate black
augroup END
In my current vim setup I have set foldmethod=syntax , however whenever I save my file it refolds anything I had opened. Any ideas?
FWIW this is my current vimrc
This behavior is normal. Vim's default is not to remember which code you had folded vs. unfolded from one session to the next. You can save your current folds; when you finish editing a file, before exiting vim, enter the command :mkview. When you next open the file, if you enter :loadview, it will restore your folds. If you want this to happen automatically, add this code to your vimrc
augroup remember_folds
autocmd!
autocmd BufWinLeave * mkview
autocmd BufWinEnter * silent! loadview
augroup END
If you want more features, this plugin does the same thing http://www.vim.org/scripts/script.php?script_id=4021.
Update: sorry, my original code didn't work. It should work now.
I am not a vim config ninja, but I hacked various solutions to achieve this, which works for me on nvim/Neo Vim without throwing errors.
augroup remember_folds
autocmd!
au BufWinLeave ?* mkview 1
au BufWinEnter ?* silent! loadview 1
augroup END
Saving a file should definitely not cause Vim to reset folding. However, some autocmd on e.g. BufWritePost actions might trigger such behavior.
More specifically vim-go has an open bug which causes this issue with golang files. The bug's comments lists a couple of workarounds. Setting g:go_fmt_experimental = 1 works for me.
augroup remember_folds
autocmd!
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent! loadview
augroup END
if you're having issues getting the folding to work with something like Telescope or other plugins that create buffers (windows, dialogs, etc), the above seems to work for me so far.
It basically requires a period in the name of the buffer (foo.sh, script.py, server.js, etc) to trigger. The dynamic buffers from things like Telescope don't seem to match that pattern.
I got autocmd BufWrite *.rb :RuboCop -a in my ~/.vimrc
How to modify it to process *.jbuilder files as well?
You can either add another file-glob, separated by ,:
autocmd BufWrite *.rb,*.jbuilder :RuboCop -a
This is documented under :help autocmd-patterns. Alternatively, of course, define a separate autocmd:
autocmd BufWrite *.rb :RuboCop -a
autocmd BufWrite *.jbuilder :RuboCop -a
If all these file globs already are detected to a single filetype inside Vim, you can also leverage that, and define a buffer-local trigger on that:
autocmd FileType ruby autocmd BufWrite <buffer> RuboCop -a
Use a comma:
autocmd BufWrite *.rb,*.jbuilder :RuboCop -a
For more information see :h autocommand-pattern.
I put together the following pile of awesomeness:
if !exists("g:AsciidocAutoSave")
let g:AsciidocAutoSave = 0
endif
fun! AsciidocRefresh()
if g:AsciidocAutoSave == 1
execute 'write'
execute 'silent !asciidoc -b html5 -a icons "%"'
endif
endf
fun! AsciidocFocusLost()
augroup asciidocFocusLost
autocmd FocusLost <buffer> call AsciidocRefresh()
augroup END
endfun
augroup asciidocFileType
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
The only problem is: It saves twice every time vim loses focus when in an asciidoc file.
When I put :autocmd asciidocFileType into the command line, it shows:
---Auto-Commands---
asciidocFileType FileType
asciidoc :call AsciidocFocusLost()
:call AsciidocFocusLost()
The same with :autocmd asciidocFocusLost and AsciidocRefresh().
Why this duplication?
I can't tell you for sure why you got those duplicates (multiple sourcing?), but the canonical way to prevent this is clearing the augroup first:
augroup asciidocFileType
autocmd!
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
Since you only have one definition, you can also do this in one command:
augroup asciidocFileType
autocmd! FileType asciidoc :call AsciidocFocusLost()
augroup END
#ingo-karkat's answer is great, but I think I know why you get twice as many autocmds and how to prevent it naturally:
Some distributions of vim / Neovim make sure by default that filetype plugin indent is on. Test it for neovim with env XDG_CONFIG_HOME=/dev/null nvim -c 'filetype' or for vim with env HOME=/dev/null vim -c filetype
If the output is
filetype detection:ON plugin:ON indent:ON
Then You don't need to add filetype * on to your vimrc.