12

I find that sometimes I write a really long ex mode command that do lots of stuff. They are sort of "mini-scripts" that I write in the text editor then I start ex mode and copy them into the ex line and execute. But then I always end up editing in the ex mode and then I find it difficult to get the changes i did in ex mode back to my text editing session.

Using the keyboard, how do you copy the last ex command you used and paste it into your text editor?

(Note: This is sort of the opposite of this question "how do I copy/paste in vim ex mode" where the user asks "how do you copy from the text editor and paste INTO the ex mode?". My question is the opposite because I wish to copy from the ex mode and paste into my text editor.)

1
  • I have a kludge that does this... but it requires the mouse to select... the gvim pull down menu to copy... and if the ex line is two lines the paste has a new line which is annoying. (but more annoying is the fact that I am using the mouse and the gui to copy/paste...). Jan 8, 2011 at 16:18

3 Answers 3

19

The : register contains the last :-command (ex command) you ran, so you can simply type the command ":p (with the double quote) to paste that register into the current buffer. See:

:help registers
:help ":

Note that there are registers for the last search, the current buffer's name, etc.

21

Try the q: command. (That's two characters, first q and then :.) This is a normal mode command that will open up your ex-command history in a special Vim buffer. You can review the history, yank anything you want just as always, and/or edit a line and press <enter> on it to execute it.

3
  • Just be careful not to type :q instead, which my muscle memory tries to force on me!
    – frabjous
    Jan 8, 2011 at 17:27
  • 9
    q: works from normal mode. If you're already in command-line (ex) mode, type Ctrl-F to open that command-line window.
    – garyjohn
    Jan 8, 2011 at 18:51
  • Hmm.... Ctrl+F does notthing but print "^F" in my ex-mode command line. Am I missing some magic sauce?
    – Stabledog
    Sep 14, 2017 at 11:16
1

I'd like to complement @Heptite's answer. To merely copy the command line into system clipboard (the register +), just type :let @+ = @:, assuming your vim support +clipboard feature, according to @Saad Malik

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .