Vim how to autosave and get autocmds to run - vim

I have the following in my .vimrc to autoformat my python code on save.
autocmd BufWritePost *.py silent !autopep8 --in-place --aggressive %
However I am using a plugin vim-auto-save that automatically saves my code for me. However the BufWritePost event doesn't seem to get called. I have to manually run :w for my autocmd to work.

Vim-auto-save runs a
silent! wa
command, which should save all files, and that should trigger the BufWritePost event.
Did you enable vim-auto-cmd on startup?
let g:auto_save = 1 " enable AutoSave on Vim startup

Related

Indent code using vim autocommand

I want use the indent linux utility to indent the current file on saving. I tried
autocmd BufWritePre *.[ch] :!indent -kr -nut %
in my .vimrc file (~/.vim).
Manually I tried that command
:!indent -kr -nut %
it works only after prompting me to load the file, as below,
See ":help W12" for more info.
[O]K, (L)oad File:
You don't want BufWritePre because that modifies your file before saving it. Instead, try doing BufWritePost:
autocmd BufWritePost *.[ch] !indent -kr -nut %
This will still ask you to hit enter, but it doesn't prompt to load the file when I test it. If you don't want to have to hit enter after saving, you can change this to:
autocmd BufWritePost *.[ch] exec "!indent -kr -nut %" | redraw
Also, note how I removed the : from your command. This is because autocmd looks for an ex command, so the : is unneeded.

Force compilation and diagnostics on save with YouCompleteMe plugin

YouCopmleteMe is pretty awesome, however there is one behaviour I would like to change, and thats the ability to force compilation and diagnostics to be executed when you save the file. Currently one can put the following in their vimrc to press F5 to force compile:
nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>
You need to trigger that command on the event of saving using an autocmd:
autocmd BufWritePost * YcmForceCompileAndDiagnostics
Or mabye just for certain file extensions:
autocmd BufWritePost *.c YcmForceCompileAndDiagnostics
See :help autocommand and :help BufWritePost.
HTH

vim autocmd silent execution

I have my .vimrc file configured in such a way that each time I save a Python file or enter a python buffer (also for Matlab and Latex but it is all the same) it silently executes exuberant ctags:
set tags+=./tags
autocmd BufWritePost *.py silent !ctags *.py
autocmd BufEnter *.py silent !ctags *.py
autocmd BufWritePost *.m silent !ctags *.m
autocmd BufEnter *.m silent !ctags *.m
autocmd BufWritePost *.tex silent !ctags *.tex
autocmd BufEnter *.tex silent !ctags *.tex
I then sometimes use this information to navigate my files. This works nicely in Mac OS X and Linux. The last month or so, I've had to use Windows 7. When I add these lines to my .vimrc it flashes a cmd prompt in front of me and it is very obnoxious. I also tried !start instead of silent, and that brings a host of other issues, chief among them having to press enter each time after :w.
What am I missing? Is it possible to duplicate my OS X configuration, where the command gets executed, the tags file gets updated, everything gets reloaded without flashing the cmd prompt and without further issues?
You may want to check
autocmd BufEnter *.tex silent! !start /min ctags *.tex
Alternatively, try /b /wait.
See also https://serverfault.com/questions/31608/can-i-run-a-gui-program-in-the-background-on-the-windows-command-line/31621#31621
You may also check the shell.vim plugin:
enables execution of external commands in the background without opening a
command prompt window on Windows.
It also provides some interesting additional features.

Vim: Saving on FocusLost and executing auto command

I've setup my vim editor (I use MacVim) to save files automatically when the focus is lost:
autocmd FocusLost * silent! wall
I also automatically strip trailing whitespace from python files using this auto command:
autocmd BufWritePre *.py :%s/\s\+$//e
This auto command works perfectly when I save the file manually (either by typing :w or by pressing ⌘s) but it is not executed (i.e. the whitespace is not stripped) when I switch to another application and the buffer is automatically written.
How can I modify these auto commands to make them work together?
You need to change your FocusLost autocommand to:
autocmd FocusLost * nested silent! wall
See :h autocmd-nested for details.
I can't test this in a graphic Vim, but you can try some options:
Join some events in the same autocommand autocmd BufWritePre,FocusLost *.py ...
Execute an autocommand from an event, something like:
autocmd BufWritePre *.py :execute "%s/\s\+$//e" | doautocmd FocusLost %

Is it possible to apply vim configurations without restarting?

I want to edit .vimrc file from Vim and apply them without restarting Vim.
Yes, just use the :so % command while editing your .vimrc.
If you want vim to auto-reload your configuration, you must add the following commands :
augroup myvimrchooks
au!
autocmd bufwritepost .vimrc source $MYVIMRC
augroup END
the grouping of autocommand is here to avoid "exponential" reloading if you save several times your configuration.
Here's a more cross-platform compatible version if you run on Mac/Windows/Linux and gvimrc:
augroup myvimrc
au!
au BufWritePost .vimrc,_vimrc,vimrc,.gvimrc,_gvimrc,gvimrc so $MYVIMRC | if has('gui_running') | so $MYGVIMRC | endif
augroup END
The autocmd watches all potential *vimrc files and when one changes, it reloads the vimrc file followed by gvimrc if the GUI is running.
source your vimrc file :source ~/.vimrc
" Quickly edit/reload this configuration file
nnoremap gev :e $MYVIMRC<CR>
nnoremap gsv :so $MYVIMRC<CR>
To automatically reload upon save, add the following to your $MYVIMRC:
if has ('autocmd') " Remain compatible with earlier versions
augroup vimrc " Source vim configuration upon save
autocmd! BufWritePost $MYVIMRC source % | echom "Reloaded " . $MYVIMRC | redraw
autocmd! BufWritePost $MYGVIMRC if has('gui_running') | so % | echom "Reloaded " . $MYGVIMRC | endif | redraw
augroup END
endif " has autocmd
and then for the last time, type:
:so %
The next time you save your vimrc, it will be automatically reloaded.
Features:
Tells the user what has happened (also logging to :messages)
Handles various names for the configuration files
Ensures that it wil only match the actual configuration file (ignores copies in other directories, or a fugitive:// diff)
Won't generate an error if using vim-tiny
Of course, the automatic reload will only happen if you edit your vimrc in vim.
autocmd! bufwritepost _vimrc source %
this will automatic reload all config in _vimrc file when you save

Resources