24
Sep 11

Quick vim tip #1 (and I am not dead)

Sorry for the summer hiatus. Work, vacation, and mountain biking put a serious squeeze on my free time hacking and blogging activities. I have been working on some cool stuff lately and the cold weather is starting to arrive here in Spokane, so I should have plenty of blog posts coming up.

Anyway, quick vim tip #1. If I have a chunk of a code with a string:

var blah = "Some string that I want to replace" + previouslyDefinedVariable

I want to replace it with “new string”. In vim, do:

  1. Put your cursor on the first character inside the quotes of the string you want to end up with, in our case, the n from “new string”.
  2. Type yi” – yank inbetween quotes
  3. Navigate to the string you want to replace
  4. Type di” – delete inbetween quotes
  5. “0P – paste content from yank buffer
  6. Marvel at your vim prowess.

Got any great vim tips? I would love to here about them. Send them to vimtips@fzysqr.com. Check out my full vim config fork of scrooloose’s config on github (here)[https://github.com/jslatts/vimfiles].

Updated Cory Schmitt wrote in with a helpful suggestion that saves two keystrokes!

  1. Put your cursor on the first character inside the quotes of the string you want to end up with, in our case, the n from “new string”.
  2. Type yi” – yank inbetween quotes
  3. Navigate to the string you want to replace
  4. Type vi”p – visual mode select inbetween quotes and paste over
  5. Congratulate yourself for being awesome.