I was working on building a regex in the command prompt. After a few iterations, my prompt looks like this:

:echom 'test_foo_bar.py' =~ '^test_.*\.py$' ? 'yes' : 'no'

Now that I have my regex ready, I want to put it in the script I'm editing. Is there an easy way to take the text from my command prompt and stick it in the current buffer? I'm specifically interested in the regex, but if I could dump the whole prompt to the buffer it would be easy enough to edit out the rest.

  • Copy it to the clipboard and "*p ? – hgiesel Jun 21 '16 at 11:51
  • 3
    The last command is in register @:, so ":p. – Antony Jun 21 '16 at 13:54

In addition to the CtrlF, which provides complete navigation on the command history, you could use CtrlR+: in insert mode (faster but works only for the latest command):

CTRL-R {0-9a-z"%#*+:.-=}                    *i_CTRL-R*
        Insert the contents of a register.  Between typing CTRL-R and
        the second character, '"' will be displayed to indicate that
        you are expected to enter the name of a register.
        The text is inserted as if you typed it, but mappings and
        abbreviations are not used.  If you have options like
        'textwidth', 'formatoptions', or 'autoindent' set, this will
        influence what will be inserted.  This is different from what
        happens with the "p" command and pasting with the mouse.
        Special registers:
            '"' the unnamed register, containing the text of
                the last delete or yank
            '%' the current file name
            '#' the alternate file name
            '*' the clipboard contents (X11: primary selection)
            '+' the clipboard contents
            '/' the last search pattern
            ':' the last command-line
            '.' the last inserted text
            '-' the last small (less than a line) delete

You could also use the equivalent in normal mode: ":p:

                            *p* *put* *E353*
["x]p           Put the text [from register x] after the cursor
            [count] times.  {Vi: no count}

["x]P           Put the text [from register x] before the cursor
            [count] times.  {Vi: no count}

Press CtrlF (or in normal mode, press q:):

OPEN                                            c_CTRL-F q: q/ q?

There are two ways to open the command-line window:
1. From Command-line mode, use the key specified with the 'cedit' option.
   The default is CTRL-F when 'compatible' is not set.
2. From Normal mode, use the "q:", "q/" or "q?" command.
   This starts editing an Ex command-line ("q:") or search string ("q/" or
   "q?").  Note that this is not possible while recording is in progress (the
   "q" stops recording then).

When the window opens it is filled with the command-line history.  The last
line contains the command as typed so far.  The left column will show a
character that indicates the type of command-line being edited, see

Vim will be in Normal mode when the editor is opened, except when 'insertmode'
is set.

Then you can just yy the required line (the current, or previous line, as the case may be), and paste it in the target window.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.