In Vim, is there a way to paste text in the search line?

Vim

Vim Problem Overview


I want to search for $maximumTotalAllowedAfterFinish and replace it with $minimumTotalAllowedAfterFinish. Instead of typing the long text:

:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g

Is there a way to COPY these long variable names down into the search line, since, on the command line I can't type "p" to paste?

Vim Solutions


Solution 1 - Vim

You can insert the contents of a numbered or named register by typing CTRLR {0-9a-z"%#:-=.}. By typing CTRL-R CTRL-W you can paste the current word under the cursor. See:

:he cmdline-editing

for more information.

Solution 2 - Vim

Copy it as normal, then do CtrlR" to paste. There are lots of other CtrlR shortcuts (e.g, a calculator, current filename, clipboard contents). Type :help c_<C-R> to see the full list.

Solution 3 - Vim

Copy:

  1. v (or highlight with mouse, in visual mode)
  2. y (yank)

Paste:

  1. / (search mode)
  2. Ctrl + R + 0 (paste from yanked register)

Solution 4 - Vim

Type q: to get into history editing mode in a new buffer. Then edit the last line of the buffer and press Enter to execute it.

Solution 5 - Vim

Or create the command in a vim buffer , e.g. type it in the buffer:

s/foo/bar/gci

And copy it to a named register, with "ayy (if the cursor is on that line!).

Now you can execute the contents of the "a" register from Vim's Ex command line with:

:[OPTIONAL_RANGE]@a

I use it all the time.

Solution 6 - Vim

Typically, you would do that with mouse selecting (perhaps CtrlIns or CtrlC after selecting) and then, when in the command/search line, middle-clicking (or ShiftIns or CtrlV).

Another way, is to write your command/search line in the text buffer with all the editing available in text buffers, starting with : and all, then, on the line, do:

"add@a

which will store the whole command line in buffer a, and then execute it. It won't be stored in the command history, though.

Try creating the following line in the text buffer as an example for the key presses above:

:%s/$maximumTotalAllowedAfterFinish/$minimumTotalAllowedAfterFinish/g

Finally, you can enter q: to enter history editing in a text buffer.

Solution 7 - Vim

You can place the cursor on the word that you want to add to your pattern and then press / or : to enter either the search or the command mode, and then press CtrlRCtrlW to copy the word. Source

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward TanguayView Question on Stackoverflow
Solution 1 - VimWMRView Answer on Stackoverflow
Solution 2 - VimNedView Answer on Stackoverflow
Solution 3 - VimJasonView Answer on Stackoverflow
Solution 4 - VimJohannes HoffView Answer on Stackoverflow
Solution 5 - VimZsolt BotykaiView Answer on Stackoverflow
Solution 6 - VimtzotView Answer on Stackoverflow
Solution 7 - Vimboosted regressorView Answer on Stackoverflow