Why :redraw! not take effect in augroup? - vim

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.

Related

Vim autocmd for FileType not working indirectly

I have a custom ~/.vimrc I made with this augroup:
augroup filetype_vim
autocmd!
autocmd! BufWritePost .vimrc* source %
autocmd FileType vim |
setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
augroup END
When I open vim directly to edit the ~/.vimrc like this: vim ~/.vimrc, the folding works as expected, I can fold {{{ marker:
:set foldmethod?
> foldmethod=marker
When I open vim without specifying a file: vim, and then trying to edit: :e ~/.vimrc, the foldmethod is different!
:set foldmethod?
> foldmethod=syntax
Which obviously comes from a different part of my vimrc.
Why doesn't it recognizes the file type when I open the file indirectly?
You've failed with VimScript syntax. Must be
autocmd FileType vim
\ setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
What you did instead is
autocmd FileType vim <nothing> | <nothing>
setlocal foo bar
Therefore setlocal applies to the current buffer only (i.e. command-line argument), not to anything else.

How to run VirtualEnvActivate from inside .vimrc

I am able to run it inside vim from the colon prompt.
When I put the same inside .vimrc, I get:
E492: Not an editor command: VirtualEnvActivate black
Plugins are loaded after .vimrc (:help startup). Use autocmd VimEnter * in .vimrc to set up commands to run later (:help :autocmd, :help :augroup, :help VimEnter):
augroup SetUpVirtualEnv
au!
au VimEnter * VirtualEnvActivate black
augroup END

How to add :autocmd to vim modeline?

I am trying to add :autocmd BufWrite * make to modeline in vim, but it is not triggered. I see that the help says only set <options> can be used in modelines. Is there a way to achieve this effect, where I write a file to run make.
Set your autocmd in your ~/.vimrc with a pattern that will match your file name. I would also suggest using augroup so it can be made to be re-sourced.
augroup my_project
autocmd!
autocmd BufWrite /project/*.c make
augroup END
For more help see:
:h :au
:h :aug

autocmd function always executed twice

I put together the following pile of awesomeness:
if !exists("g:AsciidocAutoSave")
let g:AsciidocAutoSave = 0
endif
fun! AsciidocRefresh()
if g:AsciidocAutoSave == 1
execute 'write'
execute 'silent !asciidoc -b html5 -a icons "%"'
endif
endf
fun! AsciidocFocusLost()
augroup asciidocFocusLost
autocmd FocusLost <buffer> call AsciidocRefresh()
augroup END
endfun
augroup asciidocFileType
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
The only problem is: It saves twice every time vim loses focus when in an asciidoc file.
When I put :autocmd asciidocFileType into the command line, it shows:
---Auto-Commands---
asciidocFileType FileType
asciidoc :call AsciidocFocusLost()
:call AsciidocFocusLost()
The same with :autocmd asciidocFocusLost and AsciidocRefresh().
Why this duplication?
I can't tell you for sure why you got those duplicates (multiple sourcing?), but the canonical way to prevent this is clearing the augroup first:
augroup asciidocFileType
autocmd!
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
Since you only have one definition, you can also do this in one command:
augroup asciidocFileType
autocmd! FileType asciidoc :call AsciidocFocusLost()
augroup END
#ingo-karkat's answer is great, but I think I know why you get twice as many autocmds and how to prevent it naturally:
Some distributions of vim / Neovim make sure by default that filetype plugin indent is on. Test it for neovim with env XDG_CONFIG_HOME=/dev/null nvim -c 'filetype' or for vim with env HOME=/dev/null vim -c filetype
If the output is
filetype detection:ON plugin:ON indent:ON
Then You don't need to add filetype * on to your vimrc.

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