Sometimes, copy text (especiallyCode) To vim, the format will be disordered. It seems that automatic indentation is a curse. This article does not go into the cause and provides a solution.
1. paste Mode
Run the following command to enter the paste mode:
:SetPaste
After entering the paste mode, press the I key to enter the insert mode, and then paste the post. The text format will not be disordered. After pasting the post, press <ESC> to enter normal mode and run the following command to end the paste mode:
:SetNopaste
Obviously, this is very troublesome. Next we will make improvements.
2. Shortcut Key 1
We can simplify the preceding two commands by setting shortcuts. In ~ /. Add the following two lines to vimrc:
Map<F10>:SetPaste<Cr>Map<F11>:SetNopaste<Cr>
In normal mode, press F10 (you can also press your favorite button) to enter the paste mode. Press I to enter the insert mode and paste the post, press <ESC> to return to normal mode, and then press F11 to end paste mode.
This is convenient, but takes up two buttons. Next we will continue to improve.
3. Shortcut Key 2
In ~ /. Add the following content to vimrc:
SetPastetoggle= <F11>
In this way, you can combine the above F10 and F11 into a shortcut key to switch the paste mode.
Is there a better way? Of course!
4. Vim register 1
For details about the vim register, run the command ": Help registers" to view details.
For the concept of "Clipboard", there are two things in Linux: one is called the selection buffer (X11 selection buffer), and the other is the clipboard (Clipboard ). When the mouse or keyboard selects the content, the content enters the buffer in real time, and then inserts the content into the cursor position by pressing the middle button; after the mouse or keyboard selects the content, press ctrl-C (or shift-Ctrl-C) or "copy" in the menu, enter the clipboard, and then press Ctrl-V (or shift-Ctrl-V) or click "Paste" in the menu to insert the content into the position of the cursor. For details about the two, please search by yourself.
The Vim register "* Corresponds to the System Buffer, register" + corresponds to the system clipboard.
If you paste the selected content (in the system buffer) to VIM, you only need to execute the following command in Normal Mode:
"* P
If you paste the copied content (in the system clipboard) to VIM, you only need to execute the following command in Normal Mode:
"+ P
How is it? The format is not disordered, right?
This is much easier, but you still need to enter three letters, and the three letters must be entered with the right thumb, and even the two letters need shift keys!
There are also simpler methods.
5. Vim register 2
In ~ /. Add the following content to vimrc:
SetClipboard=Unnamed
Then, you only need to press P in normal mode to paste the content in the system buffer to VIM, just like the content obtained by using the YY command, and the format is not bad.
If you replace the preceding line with the following content:
SetClipboard=Unnamedplus
In normal mode, the content pasted by P is the content in the system clipboard.
This is easy enough. While searching for this problem, I also saw some other methods.
6. Shortcut Key 3
In ~ /. Add the following content to vimrc:
Inoremap<S-Insert><ESC>:SETl Paste<Cr>Gi<C-R> + <ESC>:SETl nopaste<Cr>Gi
In this way, you can use the shift-insert shortcut to paste the content in the clipboard of the system in insert mode.
Replace the above line with the following content to achieve the same effect:
Inoremap<S-Insert><ESC>"+ P']
Note: This is in insert mode! Compared with the method mentioned in section 5th, this method saves one <ESC> key to normal mode and one I key to insert again.
So far, all you need to do is:
- A. Copy the content you want;
- B. Return to vim and press the shift-insert shortcut key;
- C. Edit other content.
(Original address: http://www.cbug.org/2012/07/23/vim-copy-paste.html)