This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/onramp-to-vim-modes
Do you know a command to replace a word and it’s also repeatable? I use yw/yiw to yank than vep to replace the “wrong word”. But it’s not repeatable.
It works fine. Thanks a lot.
When a word is copied by
yw/yiw, it is saved in the unnamed register
". So you can move to the word you want to replace, and use
ce to change the word, then use
Ctrl+r " sequence to insert the content of register
" (which is the correct word) at current location. This would be repeatable and do not require any plugin.
Hi @wiserfirst, I don’t believe the sequence you describe would work as intended. This is due to the fact that
ce will also copy the word into the
" register (I believe this is the default behavior, although I might have something configured that causes this).
In this past I’ve solved this by either of:
- Use visual selection to target the word to replace. So
ciwon word to copy, then
viwpon word to replace (visually select inner word, paste).
- Now I user the ReplaceWithRegister plugin to accomplish this sort of pasting (and more)
@christoomey Sorry, I made a mistake. The correct register to use should be
0 instead of
", since by default register
0 would contain the text from the most recent yank and it wouldn’t be overwritten by deletion or change. So in addition to your solutions, this one should work as well:
copy a word by
yw/yiw, move to the word you want to replace, use
ce to change the word, then use
Ctrl+r 0 sequence to paste.
Yep, that should work. Nice thinking! I personally find juggling registers to be too complicated to keep in my mind while actually working, but mastering registers is another great way to level up you Vim game.
I’ve been using Vim for a year, and did not know about visual block mode. Fantastic. In order to comment out several consecutive lines, I had been inserting the # character into each line, a line at a time. Now I can use the Ctrl-V, I sequence. Thank you.
anyone know what that plugin is for setting vim back to normal mode after a few seconds? i found this http://vim.wikia.com/wiki/To_switch_back_to_normal_mode_automatically_after_inaction but wonder if this is recommended
I’m not familiar with any specific plugins, although Vim has most of the needed functionality built in around the waiting, so from the link you posted:
" set 'updatetime' to 15 seconds when in insert mode au InsertEnter * let updaterestore=&updatetime | set updatetime=15000 au InsertLeave * let &updatetime=updaterestore
should work just fine.
That said, I’ve never felt a need for this functionality myself. I could see this as perhaps a good training mode / encouragement to get into the habit of being in normal mode when at rest, but beyond that I’d recommend building the habit of manually returning to normal mode whenever your insert work is done. If you find yourself moving around in insert mode, for the most part that will be sub-optimal, so even for portions of an edit I’ll back back to normal mode regularly.
One thing I do see folks do a lot is try out different escape key sequences. Many use:
imap jk <esc> imap kj <esc>
This lets you just smack the two keys together and one of those will fire, and since they’re on the home row, it’s nice and easy. Personally I have CapsLock mapped to Control, and I then use
Ctrl-space as my escape sequence as I like way it rolls / “chords”.
Hope that helps!