Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy and paste in Vim's terminal mode?

Tags:

vim

I often want to copy text from a :terminal window to a normal text buffer. At the moment I exit the shell session and copy from the history.

There must be a better way to this.

like image 483
rkta Avatar asked Feb 17 '19 14:02

rkta


People also ask

How do I copy and paste in Vim terminal?

Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.

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.

How do I copy and paste in terminal?

Alternatively, you can press Shift + Ctrl + C . Highlight the text portions you wish to copy with color and font attributes, then right click on the text portion and select Copy as HTML. Right click in the Terminal and select Paste. Alternatively, you can press Shift + Ctrl + V .

How to copy and paste in vim/vi editor?

This article shows how to copy, cut, and paste in Vim / Vi editor. When you launch the Vim editor, you’re in the normal mode. In this mode, you can run Vim commands and navigate through the file. To go back to normal mode from any other mode, just press the Esc key. Vim has its own terminology for copying, cutting, and pasting.

How to add text to a Vim file?

You will be navigated to the Normal mode of Vim. Press the “Esc” key and tap “I” to open the “Insert” mode of the Vim editor. The “yank” or “y” command is used to copy the selected text. The point to be noted is that you can open any already created file in vim. If not, you can simply start the Vim editor to add text.

How to cut or delete text in Vim?

If you are using Vim in normal mode, you can easily cut or delete text using the d command. Here are some ways you can cut content: Once you have selected text in Vim, no matter whether it is using the yank or the delete command, you can paste it in the wanted location.

How do I move the cursor around in Vim?

This is called Terminal-Job mode. Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the contents of the terminal window is under control of Vim, the job output is suspended. CTRL-\ CTRL-N does the same. [...] In Terminal-Normal mode you can move the cursor around with the usual Vim commands, Visually mark text, yank text, etc.


3 Answers

Copy

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.


Paste

To paste from a register into the terminal window you have to be in Terminal-Job ("insert") mode.

Press CTRL-W " followed by the register.


:help Terminal-mode tells us:

When the job is running the contents of the terminal is under control of the job. That includes the cursor position. Typed keys are sent to the job. The terminal contents can change at any time. This is called Terminal-Job mode.

Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the contents of the terminal window is under control of Vim, the job output is suspended. CTRL-\ CTRL-N does the same.

[...]

In Terminal-Normal mode you can move the cursor around with the usual Vim commands, Visually mark text, yank text, etc. But you cannot change the contents of the buffer. The commands that would start insert mode, such as 'i' and 'a', return to Terminal-Job mode.

See :h terminal-typing for more useful commands in terminal windows.


1Unfortunately the vim help doesn't tell you that it is a capital N, I kept the original notation

like image 101
rkta Avatar answered Sep 23 '22 13:09

rkta


Can use Shift+Insert as a shortcut to paste from the clipboard into a running terminal session. Setup the mapping like

:tmap <S-Insert> <C-W>"+

The will result in pasting from the + register. Alternatively use the * register which sometimes works better in MS Windows.

like image 26
FuzzyWuzzy Avatar answered Sep 21 '22 13:09

FuzzyWuzzy


Set the clipboard setting in your .vimrc to using system clipboard as the Vim's clipboard. Depends on your OS it may differ, assume you are using Mac OS: set clipboard=unnamedplus

Then you can use y command to copy then paste on another app just by Cmd + V, remember to exit the insert mode of the terminal by clicking any where or press Ctrl+\ Ctrl+n

like image 26
Finn Avatar answered Sep 23 '22 13:09

Finn