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
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 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(!).
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
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 have a symlink which points my .vimrc to the one from my repo.
Vim loads that just fine, but I can't get it to auto-source upon it being changed.
I have the typical:
if has("autocmd")
autocmd! BufWritePost .vimrc source $MYVIMRC
endif
Which works if the vimrc is not symlinked.
I have a similar setup where ~/.vimrc is just a symlink to a git repository. The following autocommand works for me:
autocmd! bufwritepost .vimrc source %
I don't like symlinks in general and Vim doesn't really like them either.
The layout I use is probably similar to yours:
~/.vimrc
~/.vim/vimrc
with a big difference: ~/.vimrc is a real file, not a symlink, and it contains only one line:
runtime vimrc
that executes my real ~/.vim/vimrc. Because it is a Vim command and it doesn't use a file path, that line can be the same on every system.
Because $MYVIMRC points to a real file, :so $MYVIMRC always works.
I solved that problem that way that all my configuration I am keeping in dotfiles folder
https://github.com/lis2/dotfiles
Then I have small and simple ruby script which I running when I am changing something in configuration
#!/usr/bin/env ruby
require "fileutils"
config_hash = { "tmux.conf" => ".tmux.conf", "vimrc" => ".vimrc", "vim" => ".vim", "gitconfig" => ".gitconfig", "gitignore" => ".gitignore"}
config_hash.each do |k,v|
FileUtils.rm_rf(File.dirname(__FILE__) + "/../#{v}")
FileUtils.ln_s(File.dirname(__FILE__) + "/#{k}", File.dirname(__FILE__) + "/../#{v}")
end
I recommend you to built same configuration. On all computers (private/work) I just clone my repo, run symlink.rb and my simple environment is ready for work.
cheers!
I have both .vimrc and .gvimrc symlinked to a git repo like you are describing. I'm running MacVim macOS.
I found a great answer to the question progressively-slower-reloading-time-of-vimrc.
I modified it a bit to reload both vimrc and gvimrc when any of them changes. I have been using it for months now without issues.
Here it is. You need just this and just in .vimrc. You don't have to add anything to .gvimrc.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Autoreload vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
augroup configsGroup
autocmd!
" Make changes effective after saving .vimrc. Beware that autocommands are
" duplicated if .vimrc gets sourced again, unless they are wrapped in an
" augroup and the autocommands are cleared first using 'autocmd!'
autocmd! bufwritepost *\<vimrc\> call OnSavingConfigs()
autocmd! bufwritepost *\<gvimrc\> call OnSavingConfigs()
augroup END
" Avoid infinite loops
if !exists("*OnSavingConfigs")
function! OnSavingConfigs()
" Clear previous mappings, they don't go away automatically when
" sourcing vimrc.
mapclear
source $MYGVIMRC
source $MYVIMRC
redraw
echo "Reloaded " . $MYVIMRC . " and " . $MYGVIMRC . "."
endfunction
endif