Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy text from command-line mode in Vim?

Tags:

vim

Say, I just ran this command in Vim:

:nmap <CR> <C-]>

And now I want to copy this line and put it into my .vimrc. How can I select and copy the whole line in command-line mode?

like image 445
McBear Holden Avatar asked Aug 13 '11 00:08

McBear Holden


People also ask

How do I copy text from Vim?

In vim command mode press v , this will switch you to VISUAL mode. Move the cursor around to select the text or lines you need to copy. Press y , this will copy the selected text to clipboard.

How do I copy text from command prompt?

Now you can select text using your mouse or the keyboard (hold down the Shift key and use the left or right arrows to select words). Press CTRL + C to copy it, and press CTRL + V to paste it in the window. You can also easily paste text you've copied from another program into the command prompt using the same shortcut.

How do I copy and paste in Vim terminals?

To copy from a terminal window press CTRL-W N (This is a capital N)1 or CTRL-\ CTRL-N (this is not a capital N) to get into normal mode. From there you can use all usual vim commands to copy and paste stuff. Entering insert mode will drop you back to your shell.

How do I enable copy and paste in Vim?

You can use :set mouse& in the vim command line to enable copy/paste of text selected using the mouse. You can then simply use the middle mouse button or shift insert to paste it.


1 Answers

The fastest way is to run the command, switch to the destination buffer (with .vimrc loaded, in this case) and paste the whole command from the : register by typing

":p

in Normal mode.

If the command is further back in time, one can first recall it from history (e.g., by typing the first few letters and pressing the up arrow key ), rerun it, and then use the above method.

When these shortcuts are unhandy, one can resort to the general approach of using the command-line window (see :help cmdwin). To open it, either type q: in Normal mode, or press the key combination set by the cedit option (Ctrl+F, by default) in Command-line mode.

like image 181
ib. Avatar answered Sep 23 '22 23:09

ib.