Modes

Modes Vim is a "modal" editor meaning it has various modes which change its behavior in response to your key presses. This is in contrast to a more traditional modeless editor where each key combination always triggers the same behavior. This mo...
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.

I use ReplaceWithRegister which works well for me. yiw will copy a word, then griw (ā€œgo replaceā€ or ) will replace. Repeatable with . as long as you have repeat.vim installed.

1 Like

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:

  1. Use visual selection to target the word to replace. So ciw on word to copy, then viwp on word to replace (visually select inner word, paste).
  2. 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.

@christoomey

What is the t-comment you speak of?

t-comment is the plugin, although we recommend tpopeā€™s commentary.vim in Onramp to Vim.

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 To switch back to normal mode automatically after inaction | Vim Tips Wiki | Fandom 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!