How to run VirtualEnvActivate from inside .vimrc - vim

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

Related

Why :redraw! not take effect in augroup?

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.

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(!).

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.

Vim: Saving on FocusLost and executing auto command

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 %

Resources