Vimrc file takes longer and longer to reload [duplicate] - vim

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

Related

Why vim returns E488: Trailling characters to my function?

I am trying to execute a yapf command to format my python file when I save it, so I created a function to call this command:
function Format_python_file()
silent :!yapf --style="{based_on_style: pep8, indent_width: 4}" -i %
silent :e %
endfunction
autocmd BufWritePost *.py call Format_python_file() <afile>
The problem is with your autocmd line, you have a trailing <afile> there.
In fact, the message I see is quite explicit about that:
Error detected while processing BufWritePost Autocommands for "*.py":
E488: Trailing characters: <afile>
You should just drop the <afile>, the function itself already works on the current buffer, doesn't need any argument or other reference to the current file.
Also note that it's a good practice to put your autocmds inside an augroup which gets cleared first. That way, if you reload your source file (vimrc or otherwise), it won't create duplicated autocmds.
The cleaner way to set up this autocmd would be:
augroup python_yapf
autocmd!
autocmd BufWritePost *.py call Format_python_file()
augroup END

Autoload netrw when starting vim

I want netrw to autoload when I launch vim using the terminal. Completely new to linux/ubuntu. Is there any way of doing that?
Adding the following to your .vimrc (Vim's configuration file, located in the root of your home directory) will cause Vim to automatically load Netrw after starting up.
" Open Netrw after Vim starts up
augroup InitNetrw
autocmd!
autocmd VimEnter * :silent! Explore
augroup END
A problem with the preceding approach, as implemented, is that Netrw will also load when you use Vim with an argument to open a specific file. A workaround is to use the following modification, based on the suggested approach in Netrw's documentation (:help netrw-activate).
" Checks if there is a file open after Vim starts up,
" and if not, open the current working directory in Netrw.
augroup InitNetrw
autocmd!
autocmd VimEnter * if expand("%") == "" | edit . | endif
augroup END
The following pages have more details on autocommands and the .vimrc configuration file.
https://learnvimscriptthehardway.stevelosh.com/chapters/12.html
https://learnvimscriptthehardway.stevelosh.com/chapters/14.html
https://learnvimscriptthehardway.stevelosh.com/chapters/07.html
And the following code block in your vimrc:
set autochdir
let g:netrw_browse_split=4
augroup InitNetrw
autocmd!
autocmd VimEnter * if argc() == 0 | Lexplore! | endif
augroupend
Kind of does what #dannyadam suggested. But opens the netrw pane as a side bar on the right. If you want to be on the right use Lexplore without the bang(!).

Vim: How to keep folds on save?

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.

Vim is more and more slow each time I save (:w) [duplicate]

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

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