rkta
rkta

Reputation: 4589

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

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.

Upvotes: 44

Views: 37624

Answers (3)

FuzzyWuzzy
FuzzyWuzzy

Reputation: 805

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.

Upvotes: 15

Finn
Finn

Reputation: 2775

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

Upvotes: 5

rkta
rkta

Reputation: 4589

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

Upvotes: 70

Related Questions