← Back to Upcase

Modes


(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

@christoomey

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.