When I paste some text into a Vim buffer from another application,
the alignment (indentation) of the new text is messed up. How do I fix this? When you paste text into a GUI Vim using the mouse, Vim is able to detect that you are pasting text. So all the indentation related settings (like autoindent, smartindent, cindent, etc.) are ignored and the text is pasted literally. When pasting text into a Vim running in a terminal (like xterm) using the mouse, Vim may not be able to detect that you are pasting text. This depends on several things: the capability of the terminal to pass the mouse events to Vim, Vim is compiled to handle mouse events and access the clipboard, the DISPLAY variable is set properly, the Vim ‘mouse’ option is set correctly. If Vim is able to detect that you are pasting text using the mouse, then the pasted text will be inserted literally. If Vim is not able to detect that you are pasting using the mouse, then it will see the pasted text as though you literally typed the text. After the first line from the pasted text is inserted, when Vim encounters the newline character, because of the indentation settings, the next line will start indented. The spaces at the beginning of the second line in the pasted text will be inserted leading to additional indentation. This will be repeated for subsequent lines. So the pasted text will be inserted with stair case indentation. You can fix this problem in a terminal Vim in several ways: 1. Build Vim with the +mouse and +xterm_clipboard compile-time options. The normal or big or huge build of Vim includes these options. Set the ‘mouse’ option to either ‘a’ or include ‘i’. When pasting text using the mouse, don’t press the Shift key. This will work only if Vim can access the X display. xterm-clipboard 1.1 Some Linux distributions build their terminal vim packages without X support. This makes no sense and leaves many users with the impression that Vim in terminal mode doesn’t support some operations such as properly pasting text with a mouse.
If your distribution includes gvim, which it almost certainly does these days, the solutions to this include the following.
a) Start Vim as
b) Put this alias in your shell’s configuration file, e.g. ~/.bashrc:
alias vim='gvim -v'
c) Put the following command in a file named ‘vim’ and put that file in your ~/bin directory:
gvim -v "[email protected]"
d) Link the distribution’s gvim to ~/bin/vim with the following command, which needs to be executed only once.
ln -s $(which gvim) ~/bin/vim
For c) and d), make sure that ~/bin precedes /usr/bin in your PATH. 2. Paste the text using the CTRL-R CTRL-O * command. This will paste the text literally without any automatic indentation. If you want to paste the text and then fix the indentation, then you can use CTRL-R