Edit content of a Vim register on the Vim command line
(… and add it to GUI selection, system clipboard etc.)
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).
9277 aquir_eventstore_dev=# SELECT stream_id, stream_events.event_id, event_type, causation_id, correlation_id, 9278 convert_from(data,'UTF8'),
convert_from(metadata,'UTF8'), created_at, stream_version FROM events, stream_events WHERE events.event_id = stream_events.event_id
9279 and stream_events.stream_id != 0;
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
:let @*="
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:
let @*="aquir_eventstore_dev=# SELECT stream_id, stream_events.event_id, event_type, causation_id, correlation_id, convert_from(data,'UTF8'),^Mconvert_from(metadata,'UTF8'), created_at, stream_version FROM events, stream_events WHERE events.event_id = stream_events.event_id ^Mand stream_events.stream_id != 0;^M
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
:'<,'>s/
hit CTRL V
and then hit Enter. The result:
:'<,'>s/^M
From here comlete it
:'<,'>s/^M//g
and add the closing "
.
Hit i
to get back to the interactive console, and with SHIFT + INSERT
the command can be pasted.