Edit content of a Vim register on the Vim command line

(… and add it to GUI selection, system clipboard etc.)

Attila Gulyas
Scientific breakthrough of the afternoon
2 min readJan 12, 2019

--

Used to edit Vim registers by pasting it into a buffer, and move it back into a register once finished, but this extraneous step always bothered me.

My usual workflow is opening up a terminal, start Vim, and use it as a terminal multiplexer via the :terminal command. (E.g., :vert term opens up a terminal in vertical split, :tab term in another window etc.) Pressing CTRL \ CTRL n in the terminal brings it to Terminal-Normal mode, and one can browse through the entire history of it. (See :h terminal.)

For example, I keep forgetting to set up history in psql and therefore I have to search the terminal (via ? ) for queries in previous psql sessions, then copy it to GUI selection ( "* register).

There are the above 3 lines (927[789]) and yanking it into a register will end up with the 3 lines separated with the ^M control character (carriage return).

The edit sequence

Where * is the GUI selection register, where I would like to put the edited command into the psql command line via SHIFT + INSERT.

At this point hit CTRL R CTRL R r (see :h c_CTRL-R_CTRL-R ) to insert the contents of the r register (i.e., the 3 yanked lines) at the cursor on the Vim command line. It should look something like this:

Press CTRL f (see :h c_CTRL-F ) to open the command line window so that the unfinished command can be edited. The command line window shows the entire command line history, and it is editable just like any other buffer.

To replace the ^M command character, it has to be matched first. The manual was too obscure for me ( :h c_CTRL-V), but this Stackoverflow thread helped.

In the command line window select the entire :let line, press : , and at

hit CTRL V and then hit Enter. The result:

From here comlete it

and add the closing ".

Hit i to get back to the interactive console, and with SHIFT + INSERT the command can be pasted.

Useful links

--

--