Potentially 2 questions in one. I would like to know how to reference a register in Ex mode.
For instance, I'm editing a file and I want to save the file with a timestamp (or just datestamp really) appended to it.
I know I can set register to the value of a shell commands output using:
:let @a = system("date +\"%Y-%m-%d\"")
Is there any to dereference this register and insert its value into an Ex command? Something like:
:w testfile.<value of "a register>
Copying to the system clipboard and pasting would be nice, but doing it in a more generic/programitic way for building on other commands in the future would be nice.
You can also access the registers in insert/command mode with Ctrl-r + register name, like in Ctrl-r r . It will just paste the text in your current buffer. You can use the :reg command to see all the registers and their content, or filter just the ones that you are interested with :reg a b c .
The Ex mode is similar to the command line mode as it also allows you to enter Ex commands. Unlike the command-line mode you won't return to normal mode automatically. You can enter an Ex command by typing a Q in normal mode and leave it again with the :visual command.
#vim. Use CTRL-R " when entering a command in command mode to paste the current paste buffer contents. Substitute " for a buffer name, % for current filename, / for last search term, + for the X clipboard or a host of other substitutions. CTRL-R also works in insert mode, no more skipping back to normal mode to paste!
Vim has ten different types of registers: 26 Named registers “a to “z (or “A to“Z) The small delete register “- 10 numbered registers “0 to “9.
There are two approaches to doing this, but probably neither are exactly what you want.
Use <CTRL-R>a
to insert the contents in the current command line. See :help c_CTRL-R
for more info.
Use exe
to allow insertion of variables into the expression. See :help :exe
and :help 41.3
.
:exe 'w testfile.' . @a
Assuming register "a", you can type:
<CTRL-R>a
which will input the value of register a inline at that point. This can be used in insert mode as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With