When you paste code into my document from the clipboard, you get extra spaces at the start of each new line:
line line line
Figure: Example of issues issues with auto-indent
To turn off auto indent when pasting code into vim, there’s a special “paste” mode.
Then paste your code. Note that the text in the tooltip now says
-- INSERT (paste) --.
After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.
However, I always found that hard to remember. That’s why I map
<F3> such that it can switch between paste and nopaste modes while editing the text! I add this to
:set noaifollowed by
:set ai. You can also do
Better and longer answer
While setting the paste mode with
paste/nopaste/pastetoggle is perfectly fine, to turn off auto indent when pasting code into vim you still have to manually enable paste mode before pasting and disable paste mode after pasting. Below is the best solution which automatically toggles the paste mode when you paste.
Here’s a little trick that uses terminal’s bracketed paste mode to automatically set/unset Vim’s paste mode when you paste. Put following in your .vimrc:
let &t_SI .= "\<Esc>[?2004h" let &t_EI .= "\<Esc>[?2004l" inoremap <special> <expr> <Esc>[200~ XTermPasteBegin() function! XTermPasteBegin() set pastetoggle=<Esc>[201~ set paste return "" endfunction
Now you can paste without explicitly turning paste mode on/off – it is handled automatically for you.
Note: This solution doesn’t work in WSL (Windows 10 Subsystem for Linux). If anyone has a solution for WSL, please update this answer or add it in the comments.
Tmux If using tmux, then the declarations need to be double escaped. The code for this is also in Coderwall
Check more details at stackoverflow case