I have a question about code folding in Vim. Let's say I open a file and fold some code, then I save it and quit. Later I open the same file, and my folds are gone. Any idea what's going on?
You can use :mkview to save folds and such when you close a file - but you have to use :loadview next time you use the file.
Further, you can automate this with .vimrc file. Add following to your vimrc.
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview
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.
This question already has answers here:
Progressively slower reloading time of .vimrc
(2 answers)
Closed 9 years ago.
I have the following in my .vimrc so that it processes after saving
" source as soon as we write the vimrc file
if has("autocmd")
autocmd bufwritepost .vimrc source $MYVIMRC
endif
However, I have a more involved file, and it appears that the time to post the vimrc file after save gets longer and longer and longer to where I have to quit out of my gvim session and start over.
Is there a 'clear workspace' or a better way to resolve this issue, so that I may stay within the comforts of my single gvim session all day?
Every time the autocmd is sourced it is added to the autocmd list. So you might be sourcing your vimrc thousands of times when you save because everytime you source you add the autocmd to the list. (You can see that the autocmd is added multiple time by doing :au bufwritepost)
To fix this you just need to wrap the autocmd in a augroup and clear the group when it is loaded.
augroup NAME_OF_GROUP
autocmd!
autocmd bufwritepost .vimrc source $MYVIMRC
augroup end
The autocmd! removes all autocmds from the group.
Help pages to read :h autocmd-define and :h autocmd-groups
Maybe you encountered a phenomenon that it outlined in
:h autocommand
The help sais:
When your .vimrc file is sourced twice, the autocommands will appear twice.
To avoid this, put this command in your .vimrc file, before defining
autocommands:
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a variable
to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
I get used to grouping of my autocommands like this
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
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
This question already has answers here:
Progressively slower reloading time of .vimrc
(2 answers)
Closed 9 years ago.
I have the following in my .vimrc so that it processes after saving
" source as soon as we write the vimrc file
if has("autocmd")
autocmd bufwritepost .vimrc source $MYVIMRC
endif
However, I have a more involved file, and it appears that the time to post the vimrc file after save gets longer and longer and longer to where I have to quit out of my gvim session and start over.
Is there a 'clear workspace' or a better way to resolve this issue, so that I may stay within the comforts of my single gvim session all day?
Every time the autocmd is sourced it is added to the autocmd list. So you might be sourcing your vimrc thousands of times when you save because everytime you source you add the autocmd to the list. (You can see that the autocmd is added multiple time by doing :au bufwritepost)
To fix this you just need to wrap the autocmd in a augroup and clear the group when it is loaded.
augroup NAME_OF_GROUP
autocmd!
autocmd bufwritepost .vimrc source $MYVIMRC
augroup end
The autocmd! removes all autocmds from the group.
Help pages to read :h autocmd-define and :h autocmd-groups
Maybe you encountered a phenomenon that it outlined in
:h autocommand
The help sais:
When your .vimrc file is sourced twice, the autocommands will appear twice.
To avoid this, put this command in your .vimrc file, before defining
autocommands:
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a variable
to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
I get used to grouping of my autocommands like this
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
I've added the following line to my vimrc which makes vim run latexmk on tex file I open in latex:
au BufWinEnter *.tex :Latexmk
(:Latexmk comes from the Latex Box vim plugin)
How can I add something similar that will close the pdf file and kill latexmk after I close vim? Thanks!
:au BufDelete <buffer> :!killall evince
Would work if you only use evince for preview purposes. Also, it may kill other stuff named evince. This actually kills it when you close the file. Use
:au VimLeave :!killall evince
if you want to close the apps with vim.