Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim Register Use in Ex Mode

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.

like image 678
Peck Avatar asked Mar 09 '10 00:03

Peck


People also ask

How do I use vim registers?

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 .

What is ex command in vim?

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.

What is Ctrl-R in vim?

#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!

How many registers does vim have?

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.


2 Answers

There are two approaches to doing this, but probably neither are exactly what you want.

  1. Use <CTRL-R>a to insert the contents in the current command line. See :help c_CTRL-R for more info.

  2. Use exe to allow insertion of variables into the expression. See :help :exe and :help 41.3.

    :exe 'w testfile.' . @a
    
like image 113
DrAl Avatar answered Nov 05 '22 10:11

DrAl


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.

like image 26
Trent Davies Avatar answered Nov 05 '22 08:11

Trent Davies