2

I'd like a vi command that would replace from the location of the cursor to a given token. For example starting with

 word  blah blah     =(

I'd like if I locate the cursor at the space after word to be able to replace everything between the cursor and the equality sign with, say, #, obtaining

  word#(

I'm sure if I could understand this thread, I could figure it out, but that thread is much more complicated than what I need.

Community
  • 1
  • 1
Leo Simon
  • 186
  • 1
  • 9

2 Answers2

4

Try the following:

cf=#<esc>

The c command is "change", which deletes text and puts you in insert mode. It takes a motion just like every other operator. f= is a motion that jumps your cursor to the next occurrence of '='. This works with any other character to, for example, fa will jump to the next 'a', fb will jump the the next 'b', etc. After that, you end up in insert mode, and can enter #. Then just hit <esc> to return to normal mode.

You can do the same thing with more advanced "tokens" by using /foo instead of f=. For example, if you'd like to change until the next occurrence of =( instead of the next occurrence of =, you could do

c/=(<cr>#<esc>
Randy Morris
  • 39,631
  • 8
  • 69
  • 76
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
  • Might be worth noting that `f`/`F` and `t`/`T` only work on the current line. To do this across multiple lines you need to use a search (`/`). – Randy Morris Oct 30 '16 at 01:19
3

Unless there are other = chars between the cursor and the desired =, you can use cf=#<Esc>.

cf= deletes from the cursor until the first = (inclusive), and places you in insert mode.

Nikita Kouevda
  • 5,508
  • 3
  • 27
  • 43