1. Background
When pasting the code in the terminal vim, it is found that the inserted code will have extra indentation and will accumulate line by row. The reason is that the terminal puts the pasted text into the keyboard cache (Keyboard buffer), which is handled by VIM as the user's keyboard input. When a newline character is encountered, if vim turns on auto indent, it defaults to inserting the previous line indent to the beginning of the next line, eventually causing the code to become messy.
2. Resolve
- Cancel Auto Indent
In command mode, use ": Set Nosmartindent" and ": Set Noautoindent" To cancel Auto indent and then paste again. After the completion of the automatic indentation ": Set Smartindent" and ": Set Autoindent", the above commands can be used shorthand, such as ": Set Si", can be the help of vim ": Helps Smartindent" to see the corresponding instructions.
- Paste Mode
Vim's editing mode, there is also a paste mode, in this mode, you can paste the original text into vim, to avoid some formatting errors. Enter and exit the mode with ": Set Paste" and ": Set Nopaste". A more convenient way is to set a shortcut to enter and exit paste mode in Vim, add a line to "~/.VIMRC" to configure "set pastetoggle=<f12>", so that the F12 can be quickly switched in paste mode, Of course, shortcut keys can be arbitrarily specified without conflict, specifically how to specify, refer to the accompanying tutorial links.
3. Reference
- Http://vim.wikia.com/wiki/Paste_Indent_Problems
- Http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_ (part_1)
- Http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_ (part_2)
- Http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_ (Part_3)
Vim indentation confusion when pasting code