You can turn on automatic indenting in vim by setting the indentexpr option. You can turn it on by adding the below line to your ~/.vimrc.
filetype indent on
While automatic indentation is almost the next best thing to baked bread, especially when you code on a remote server, it’s also painful when you want to paste something from your local machine’s clipboard, vim goes overboard and indents code like this:
In order to be able to paste stuff from clipboard into a file open in vim via a terminal without vim auto indenting it badly, you need to enable the paste mode.
you can enable the paste mode, which will let you paste stuff without vim indenting it multiple times
you can go back to the auto-indent mode.
This can also be toggled easily by using the following command:
Now, it can be a little bit of a pain to remember to toggle between the paste mode on and off. A good samaritan has written a vim pathogen bundle that makes this automatic.
Vim Bracketed Paste plugin lets you easily paste stuff without having to toggle paste modes.
Installing vim bracketed paste plugin is easy:
- Install pathogen first.12mkdir -p ~/.vim/autoload ~/.vim/bundle && \curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
- Install vim bracketed paste12cd ~/.vim/bundlegit clone https://github.com/ConradIrwin/vim-bracketed-paste
Hope this helps!