As you know, there are (sadly) no VisualLeave or VisualEnter autocmds in vim. Is there a way to emulate it?
(Sidenote: Having such events would add even more power to vim)
So far, the best solution is here: Run command when vim enters visual mode
It is a workaround to not having the Visual* autocmd events.
Related
I want to execute system("cp /home/currently_opened_file.txt /somewhere/else") when I exit vim with :wq. Is there an autocmd event for that? Or any other way to do it?
Update:
The OP noted in comments that this combination did exactly what was wanted (execute the command only on :wq).
:autocmd BufWritePost * :autocmd VimLeave * :!cp % /somewhere/else
Original answer:
You can hook the BufWritePost event. This will run the command on every write, not only when you use :wq to leave the file.
:autocmd BufWritePost * :!cp % /somewhere/else
I suppose you could try hooking the BufDelete event (before deleting a buffer from the buffer list), but that seems like it would be problematic, as buffers are used for more than file editors. They are also used for things like quicklists, the help viewer, etc.
There are some events that take place when you are quitting, which could be an option.
QuitPre when using :quit, before deciding whether to quit
VimLeavePre before exiting Vim, before writing the viminfo file
VimLeave before exiting Vim, after writing the viminfo file
You can see the full list using :help autocmd-events.
Also note that you can restrict what matches the event. For instance, if you only want this to happen for HTML files and CSS files, you could use this:
:autocmd QuitPre *.html,*.css :!cp % /somewhere/else
I suspect you will need to experiment and see what works for you.
It looks like you need to automatically cascade the writing of a file to another location. My DuplicateWrite plugin provides comfortable commands to set up such. (The plugin page has links to alternative plugins.)
:DuplicateWrite /somewhere/else
I have previously asked this in comp.editors, but without getting any replies.
This ought to be simple: I want to configure vim to set number whenever the editor is in ex mode (to get visible line numbers), but never have that option set in visual mode. How?
If the solution involves having to start ex instead of vim, that's fine, but the solution should ideally also cover the case of entering ex mode from visual mode (using Q in vim visual mode, for example).
A solution that also works in nvi would be nice, but not necessary.
You can try something like this:
let &number = mode(1) ==# 'ce'
nnoremap <silent> Q :set number<CR>Q
This will set number when you run vim -e and when you enter ex mode with Q, but it won't clear it when you go back to visual mode. As far as I can tell there is no way to detect the actual event of switching modes. shrug
Vim's Autocmd seemed like your best bet, since it has event listeners. An example would be
:autocmd InsertLeave * :set nonumber
:autocmd InsertEnter * :set number
which shows/hides line numbers
However, I couldn't find any events for Ex mode when I looked.
my question is similar to this one https://superuser.com/questions/277051/how-can-i-emulate-key-presses-on-vim-startup
I'm using the plugin netrw in my vim instead of nerdtree in that question. I add the following line to enable netrw automatically.
autocmd BufWinEnter * :Ve
While open vim, I need to press ^W^W each time because the active buffer is the left one, where the netrw is located.
How can I make vim emulate these key presses on startup? In fact, I've got it works before, but I didn't save my .vimrc in a recently re-installation of OS.
The answer of that question doesn't work for me.
My environment:
Mac OS X 10.9.5
MacVim Snapshot 73
Any answers appreciated, thanks a lot :)
Best way is by just appending the command to the existing :autocmd (separated by |):
autocmd BufWinEnter * Vexplore | wincmd w
Notes
By using BufWinEnter, this will open a netrw split on each displaying of a buffer. If you just want to trigger this once at Vim startup, VimEnter is the right event to use.
Don't abbreviate commands inside plugins and configuration; this is just to save keys on interactive use. It's easier to understand this way, and you don't have the risk of having to adapt all instances when you install / define another command with the same initial letters (e.g. :VerticalSplit). Also, you don't need the : in autocmds.
For <C-W> window commands, there's the special :wincmd Ex command to trigger those. For all else, you would use :execute 'normal! \<C-w>\<C-w>'.
How can I disable autocomplete in Vim when writing a comment or docstring? I tried au FileType * setl fo-=cro and set fo-=c fo-=o fo-=r but that didn't help. I use Neocomplete.
My OnSyntaxChange plugin may help with this.
First, you need to generate events when entering / leaving comments:
call OnSyntaxChange#Install('Comment', '^Comment$', 0, 'i')
Then, hook commands to enable / disable the auto completion to the events. Fortunately, Neocomplete provides those:
autocmd User SyntaxCommentEnterI silent! NeoCompleteLock
autocmd User SyntaxCommentLeaveI silent! NeoCompleteUnlock
See also this question, which does something similar for the AutoComplPop plugin.
"autowrite" doesn't seem to work when switching tab in vim.
Is there a way to enable it?
(I could remap 'gt' and 'gT' but I'd like to find a cleaner way).
Maybe use an autocmd to run w on BufWinLeave