Paste Mode: Pasting text (and indenting it) in Vim


Pasting a block of code copied from elsewhere in Vim is a nightmare, unless you know how to do it right. The autoindent (and, possibly, smartindent) features of Vim go crazy when you yank and slap a block of text. How do you do it right, then? This is not a new trick, but something I did not know about for a long time, so I suppose it is reasonable to assume many others who use Vim may not have come across it either. It is called the “paste mode“. In Vim, you can enable the paste mode by typing “:set paste” on Vim’s command interface. Once the paste mode is enabled, you can paste the block(s) of code you have yanked from elsewhere, and disable the paste mode through the command “:set nopaste” as soon as you are done with pasting text. A bit inconvenient, but it is a handy feature to get familiar with if you use Vim a lot to write code.

Python programmers would run into syntax error problems if they execute source code files in which they pasted code in the paste mode. I am not completely certain on this, but when text is pasted in paste mode, the copied lines are indented with spaces irrespective of the tab character and tab width defined for the open Vim session. If you tend to indent source files by the tab character (something probably any sane Python developer should do), this will definitely create problems. There is an easy way to fix this. Just after you turn off the paste mode, highlight the pasted text by moving the cursor to the first line pasted, pressing “shift + v” to enable “visual” mode in Vim (which is used solely for highlighting blocks of text), and dragging the highlight marker down until all the pasted text is highlighted, and pressing either the right angle bracket (>) or the left (<) to conveniently indent the block of highlighted text right and left according to the tab character and tab width set for the current Vim session. Simply indenting left once, and then right once will get the block of code back to its original indent position, but will additionally (and this is the important bit) indent the block according to the currently defined tab width and tab character settings. Pretty convenient, eh?

10 thoughts on “Paste Mode: Pasting text (and indenting it) in Vim

  1. you could also set the following code in your .vimrc

    set pastetoggle=

    that way you can turn auto indenting for pastes on and off with one press of a key. Basically pastetoggle takes care of “:set paste” “:set nopaste”

    As for a quite indenting trick. When in command mode, pres “=” followed by the amount of line numbers you want to auto indent and presto, all done.

  2. Pingback: Paste Mode: Pasting text (and indenting it) in Vim | Tea Break

  3. i was a fan of Emacs for a long time. but for many different reasons, i gave it up for Vim. i love the fact that i can very quickly edit text without having to move my fingers all down to reach Ctrl, Alt, etc keys. and i think almost all ten-finger touch typists would feel the flexibility and ease the navigation mode and the navigation jkhl keys provide.

  4. Hello I just stumbled over this via google.

    Any sane python developer should not use tabs for indentation. Your editor should be configured to convert the tab key into 4 spaces. Have a look at: http://www.python.org/dev/peps/pep-0008/

    It would be nice if you could correct this little error in the article. Even if the article is nearly 2 years old I think it will still get some visitors from google.

    Thanks, casseen

  5. Holy **** I’ve been struggling with this forever. As a tutorial addict I’m constantly pasting code and every time I have to gg V G << …………. and then reindent everything manually. Python is a killer. Thanks a million.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s