5

I've searched online for this a while, but I couldn't find an answer.

What I want to do is to edit the previously entered search text in vim.

For example, say I just searched for "FooClass". Then I want to search for "FooMethod", but rather than typing the whole word from scratch, I would like to backspace 5 times to remove "Class", and just type "Method" after "Foo".

Thanks!

steve
  • 51
  • 1

2 Answers2

14

Press / then Up, then backspace five times. (Probably assumes nocp is set).


Stop reading now unless you want to learn some fancy new tricks...

If you want to get clever, you can also use Ctrl-R to pull things onto the search box (e.g. put the cursor on a word, press /, then press and hold Ctrl and press R then W and it will pull the word under the cursor onto the search line: similar to pressing *, but editable and without the word boundaries).

If you want to get really clever, you can use all of vimscript's cleverness to set @/ to be whatever you want to search for and then press n:

:let @/ = 'Foo' . 'Method'
n

Probably more than you wanted to know though...

See:

:help 'nocp'
:help c_CTRL-R
:help c_CTRL-R_CTRL-W
:help *
:help quote/
:help :let-@
DrAl
  • 70,428
  • 10
  • 106
  • 108
  • Al -- Thanks, some nice little tidbits in there. . . – Herbert Sitz Feb 11 '11 at 18:31
  • 1
    The ctrl+r trick sounds cool but for some reason I get /<80>kb^[ rather than the word under the cursor. Any idea what might cause that? – Marcus Feb 13 '11 at 02:14
  • 2
    Probably because it's Ctrl+R , Ctrl+W instead of Ctrl+R , W . This is well documented in :help c_Ctrl-R – Drasill Feb 13 '11 at 20:33
  • Yes, sorry, I wasn't very clear on that. I should have said "hold ctrl and press r then w". I'll edit the answer to make it clearer. For information, Ctrl-R then release Ctrl and press W pulls the contents of the 'w' register onto the command line (into which you can put stuff with e.g. "wyy). – DrAl Feb 14 '11 at 07:48
10

I find editing the history is best for complicated searches. You can edit the search history with q/. Just edit the line you want to use and press enter to execute the line.

There is also a great vimcast on refining search patterns using this technique.

:h q/
ib.
  • 27,830
  • 11
  • 80
  • 100
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101