In vim, I have autosave to file with:
augroup write_it
autocmd!
autocmd InsertLeave * write
autocmd TextChanged * write
augroup END
It works really good. But I need to place a wait in there for when I rapidly delete characters in command mode TextChanged. Any ideas how? It is a nightmare with livereload and gulp tasks watchers for front end development.
I tried exec 'sleep 2' but vim is async and it's useless.
Check the help for CursorHold and CursorHoldI events. They are triggered after some period of inactivity according to the 'updatetime' option. You can use them.
There is also an autowrite. Anyway, it can go really complicated, so in my opinion your best alternative is to reuse code from who already thought about all this :-)
• The AutoSave Plugin: https://github.com/907th/vim-auto-save.git
Related
I use MacVim and in my .vimrc file I have map ,V :source $MYVIMRC<CR> binding that allows me to apply the newest version of .vimrc in a case it was recently modified.
However I noticed that strange things can happen, relaunch can slow down vim and some plugins can start to conflict after pressing ,V, when everything works fine if I just close and relaunch MacVim from the scratch.
I'd be very thankful if you could give me a hint on the reason of this behavior as I'd like to have a possibility to update .vimrc file that will completely clear internal vim state and grab new configuration file
The only viable way to re-apply your config to a pristine Vim is actually to restart it.
But the most likely cause of slow downs is the overuse/misuse of autocommands.
Autocommands are added without checking for existing ones. One consequence is that they tend to pile-up if you don't manage them properly and every individual autocommand corresponding to a specific event is executed when that event is triggered, leading to dreadful slow downs.
Here are the two ways you are supposed to use autocommands in your vimrc:
Method #1
" anywhere
augroup nameofthegroup
autocmd!
autocmd EventName pattern commandtoexecute
autocmd AnotherEventName anotherpattern anothercommandtoexecute
augroup END
Method #2
" near the top of your vimrc
augroup nameofthegroup
autocmd!
augroup END
" anywhere
autocmd nameofthegroup EventName pattern commandtoexecute
autocmd nameofthegroup AnotherEventName anotherpattern anothercommandtoexecute
The idea is to create a group of autocommands that clears itself whenever it is invoked and thus prevents them from piling-up.
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
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.
From vimdoc:
:checkt[ime] Check if any buffers were changed outside of Vim.
This checks and warns you if you would end up with two
versions of a file.
If this is called from an autocommand, a ":global"
command or is not typed the actual check is postponed
until a moment the side effects (reloading the file)
would be harmless.
How can I use it from an autocmd without delay?
EDIT
I somtimes use vim and an IDE to edit the same file, and I want to changes in one of them to be loaded into the other automatically.
Here is a possible solution:
autocmd CursorHold * checktime
function! Timer()
call feedkeys("f\e")
" K_IGNORE keycode does not work after version 7.2.025)
" there are numerous other keysequences that you can use
endfunction
But since checktime will be delayed, the effect may not be satisfactory.
As the quoted documentation explains, you can't, and for good reason (the reload might trigger other autocmds, the change might confuse following commands).
To work around this, you have to leave the current :autocmd, and re-trigger your code somehow. One idea is an :autocmd BufRead, which will fire if the buffer is actually reloaded. If you need to always retrigger, an :autocmd CursorHold (maybe with a temporarily reduced 'updatetime') can be used, or call feedkeys(":call Stage2()\<CR>") might be worth a try.
I ended up doing this:
autocmd CursorHold * call Timer()
function! Timer()
checktime
call feedkeys("f\e")
endfunction
It works quite well.
I have come across the awesome ctrlp.vim plugin. It is a good alternative to the Command-T plugin which I have used before. What I did not like about Command-T is that it would take about 20-30 seconds to rescan files when it is invoked for the first time after starting vim.
CtrlP works a lot faster but it does not seem to automatically rescan for newly created files. How should I trigger a rescan manually?
Thanks!
From the documentation:
<F5>
- Refresh the match window and purge the cache for the current directory.
- Remove deleted files from MRU list.
This assumes you're in ctrl-p mode already. Note that you can hit F5 in the middle of a query, i.e., you can type a few characters, find it's not matching a recently updated file, and hit F5 to refresh right then. It will automatically show you the match if the file was just added to the ctrl-p cache.
As Jeet says you can press F5 but if that doesn't work you can always run :CtrlPClearCache which is what F5 is supposed to run.
From the documentation
:CtrlPClearCache
Flush the cache for the current working directory. The same as pressing <F5>
inside CtrlP.
To enable or disable caching, use the |g:ctrlp_use_caching| option.
I added this to .vimrc which turns off ctrlp caching
g:ctrlp_use_caching = 0
If you want, you can automatically bust the cache when a save happens, so it will be forced to refresh on next use.
Put this in your vimrc (credit docwhat):
" CtrlP auto cache clearing.
" ----------------------------------------------------------------------------
function! SetupCtrlP()
if exists("g:loaded_ctrlp") && g:loaded_ctrlp
augroup CtrlPExtension
autocmd!
autocmd FocusGained * CtrlPClearCache
autocmd BufWritePost * CtrlPClearCache
augroup END
endif
endfunction
if has("autocmd")
autocmd VimEnter * :call SetupCtrlP()
endif
Unfortunately there's no way to automatically keep the cache fresh in the background.
I know this is a old question, but it's so simple that I have to comment.
Put this in your .vimrc
:nnoremap <c-p> :CtrlPClearCache<bar>CtrlP<cr>
This will refresh the cache and then call CtrlP. No more missing files.