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

后端 未结 3 1159
南方客
南方客 2021-02-20 03:25

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.

相关标签:
3条回答
  • 2021-02-20 03:46

    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.

    0 讨论(0)
  • 2021-02-20 03:47

    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

    0 讨论(0)
  • 2021-02-20 03:50

    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

    0 讨论(0)
提交回复
热议问题