← Back to Upcase


(Upcase ) #1
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

(mbrinkme) #2

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.

(Chris Toomey) #3

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.

(mbrinkme) #4

It works fine. Thanks a lot.

(Wu Qing) #5

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.

(Chris Toomey) #6

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)

(Wu Qing) #7

@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.

(Chris Toomey) #8

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.

(thedanotto) #9


What is the t-comment you speak of?

(Chris Toomey) #10

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

(Norman Snyder) #11

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.

(swyx) #12

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

(Chris Toomey) #13

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!