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
Related
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 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
This question already has answers here:
Vim inoremap for specific filetypes
(5 answers)
Closed 8 years ago.
I'm new to vim after switching from Notepad and Notepad++, but I've been using it exclusively for the past four months. In my .vimrc I have a command that automatically changes the command call based on file extensions. For example, if I'm editing an R file, I can press <F5> and vim executes !Rscript %:p<cr>, but if I switch to a python file and press <F5>, vim executes !python %:p<cr>. I accomplish this by putting the following in my .vimrc:
autocmd BufRead *.R noremap <F5> :!Rscript %:p<cr>
autocmd BufRead *.pl noremap <F5> :!perl %:p<cr>
autocmd BufRead *.py noremap <F5> :!python %:p<cr>
I'm wondering if this is the "proper" vim way to execute it based on the command call. I know some of python files I work with do not have a *.py extension, and so the setting is useless in this case.
You need to do 2 things:
Create a mapping local to a specific buffer by using the <buffer> option for noremap.
Load the mappings for just a specific filetype.
This can be done via an autocmd and FileType event in your .vimrc like so:
autocmd FileType perl noremap <buffer> <F5> :!perl %:p<cr>
The other way option is by creating a filetype plugin. (see :h ftplugin for more details)
A simple example is do create a file named, ~/.vim/ftplugin/perl.vim and place your mappings inside like so:
nnoremap <buffer> <F5> :!perl %:p<cr>
I personally lean more towards the ftplugin approach but having a everything in your .vimrc file can be nice.
For more help see:
:h :au
:h FileType
:h map-local
:h ftplugin
if you have BufRead *.py, the autocommand was trigger only when it is loading a *.py buffer/file.
vim has filetype detection mechanism.
Vim can detect the type of file that is edited. This is done by
checking the file name and sometimes by inspecting the contents of the
file for specific text.
you should switch :filetype on
and use FileType event in your autocmd.
I have a simple BufWritePost autocmd to automatically generate HTML from my vimwiki pages.
autocmd BufWritePost *.wiki VimwikiAll2HTML
But it spits out a lot of progress output, and a prompt at the end:
Press ENTER or type command to continue
Is there a way to suppress the output and prompt from a vim autocmd, to make it completely transparent?
Try to add silent after the pattern.
autocmd BufWritePost *.wiki silent VimwikiAll2HTML
And add your autocmd that works well to your question, it will greatly help.
You can try another option:
:h vimwiki-option-auto_export
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 %