2016年11月19日 星期六

[neovim, copy & paste] How to install neovim and enable copy & paste function

How to install neovim and enable copy & paste function




Google doc: This document.


Purpose

從 vim copy 到 system clipboard. 基本上, 你要下達難記的 vim 指令. 但是你可以用 visual mode mapping 方式, 用巨集映射到難記的 vim 指令.
  • Ctrl + C 對應到 Copy
  • Ctrl + V 對應到 Paste


Quick

把下面的指令貼到  ~/.config/nvim/init.vim 上面, 立即擁有 Ctrl+C, Ctrl+V 的功能

~/.config/nvim/init.vim
" CTRL-X is Cut
noremap "+x

" CTRL-C is Copy
noremap "+y

" CTRL-V is Paste in [Normal mode]
map       "+gP

" CTRL-V is Paste in [Insert mode]: switch to normal mode -> paste text -> switch back to append mode
imap      "+gPa


" Cannot use Ctrl + Shift + C
" vnoremap "+ya    
"



Detail



Installation

// Install neovim
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:neovim-ppa/unstable
sudo apt-get update
sudo apt-get install neovim

sudo apt-get install python-dev python-pip python3-dev python3-pip


// Associate to your default editor
sudo update-alternatives --install /usr/bin/vi vi /usr/bin/nvim 60
sudo update-alternatives --config vi
sudo update-alternatives --install /usr/bin/vim vim /usr/bin/nvim 60
sudo update-alternatives --config vim
sudo update-alternatives --install /usr/bin/editor editor /usr/bin/nvim 60
sudo update-alternatives --config editor

// Enable Copy and Paste
sudo apt-get install xclip xsel



1. 利用 vnormap 指令, 快速設定 vim copy-paster 快速鍵

Step 1: 在 vim [Normal Mode] 下輸入 [:], 進入指令模式
e.g.
Step 2: 輸入巨集: ["] + [+] + [y]
Command
vnoremap "+y        // 使用者按下 Ctrl + C, 即可將選擇的文字, 複製到 system clipboard

其中
  • vnoremap: vim 巨集指令
  • : Ctrl + C
  • "+y: 是 vim 將選擇文字複製到 system clipboard 的指令

E.g.

Verification

Step 1: Select the text that you want to copy

Step 2: Type [Ctrl + C]

Step 3: Paste to the gedit
Open the gedit and [Ctrl + V] to paste the text.





2. 把巨集指令, 放到設定檔 以後就不用再輸入了

~/.config/nvim/init.vim
" CTRL-X and SHIFT-Del are Cut
vnoremap "+x
vnoremap "+x

" CTRL-C and CTRL-Insert are Copy
vnoremap "+y
vnoremap "+y





3. 原始難記的 vim 動用 Register 指令

先選擇你要複製的文字, 然後按下三個 vim 指令: ["] + [+] + [y]

Note:

  1. " => 切換到可以存取 vim register 的狀態
  2. + => 指定 register +
  3. y  => 將選擇的文字, 寫入 + register

Detail


Step 1: Enter the Visual Mode by selecting the text that you want to copy

Step 2: Type ", to use the vim register


Step 3: Type [+] + [y], to yank the hight text into the vim + register (system clipboard)




Verification

Try to paster the text to the gedit editor.

Command

[Ctrl] + V or [Command] + V


Misc
  • List all map key:
    • :map

References


  1. help map-modes