7

How I paste now:

  1. Right-click/Copy or simply highlight.
  2. Right-click/Paste or Middle Click or CTRL+SHFT+V
  3. Indents appear at increasing intervals: 0, 2, 4, 6, +

Question at hand:

From prior research, I found that the quick and dirty solution is entering the following in my .vimrc file: set paste. However, those who suggested that command said that set paste affects other settings. (Off the top of my head, I forget.)

I would like to know what option I can set that simply pastes without extra indentation. If that ends up being 'paste', then I would like to know what exactly that command does.

I only see one difference. When in Vim, -- INSERT -- becomes -- INSERT (paste) --

11

OK, here is a "yank & put" primer…

In Vim, the primary commands for yanking (copying) and putting (pasting) are y and p.

Yanking places the yanked text in a register. That register is the unnamed register, ", by default but one can use other registers:

"ay    " yank into register a
"by    " yank into register b
"+y    " yank into clipboard register
[…]

Similarly, putting inserts text from a register. That register is the unnamed register by default but one can use other registers:

"ap    " put from register a
"+p    " put from clipboard register
"3p    " put from the register 3
[…]

Those commands are prefered to "Right-click/Paste or Middle Click or CTRL+SHFT+V" because the text is "put" into the buffer without any special treatment. With "Right-click/Paste or Middle Click or CTRL+SHFT+V", the text is inserted in the buffer as if you typed it and is thus subjected to automatic indenting/formatting.

If you can't use "+p or "*p — working over SSH without X forwarding or Vim built without clipboard support — the paste option disables automatic indenting and a bunch of other very useful options so that your typed text is not alrered.

As noted in :help 'paste, the options has potentially very bad side effects so it is important to disable it right after you pasted your text:

:set paste
(paste)
:set nopaste

That dance is obviously a pain in the ass so there is another option that lets you define a special shortcut for toggling paste:

set pastetoggle=<F12>

That slightly reduces the pain:

<F12>
(paste)
<F12>

But it is still a pain.

If you can, it is recommended to install a proper Vim with clipboard support so you can use "*p or "+p (depending on how you copied your text) to paste text the right way.

  • On Mac OS X: install MacVim,
  • on Debian-based systems: install the vim-gnome or vim-gtk package,
  • on CentOS: install the vim-enhanced package.

Reference:

:help p
:help y
:help registers
:help 'paste'
:help 'pastetoggle'
  • 1
    Describing something as "the right way" without explaining why or providing a counter-example isn't helpful. – alxndr Feb 12 '15 at 17:58
  • 1
    I agree with a @alxndr. First off, I do not have Ubuntu, which is stated in my question. Second, there is no context on how to use "*p. Do I type it while in view mode? Or add to the .vimrc? – onebree Feb 12 '15 at 18:23
  • @onebree it's like p, but for pasting from the OS clipboard. See vi.stackexchange.com/q/84/205 – muru Feb 12 '15 at 18:44
  • Please define that in your answer then. – onebree Feb 12 '15 at 18:45
  • 1
    I think you're looking for Set the paste option, but for one insertion only – Gilles 'SO- stop being evil' Feb 19 '15 at 21:42
7

VIM has another setting pastetoggle that defines the mapping for toggling paste mode. That's what you'd want to do and then you can just enable paste mode and then paste.

Although I find that a little less intuitive and have often found ]p to be a better way to paste code instead of using just p

6

If you are using a fairly modern terminal emulator, you can install the vim-bracketed-paste plugin.

From the docs:

xterm, urxvt, iTerm2, gnome-terminal (and other terminals using libvte) are known to work.

After installing, you can simply paste as normal in insert mode, and it will take care of setting and unsetting paste for you.

  • I am GNOME TERMINAL 3.8.4 – onebree Feb 12 '15 at 16:11
  • This should work for you. Gnome Terminal has supported bracketed paste for a while. – Jay Thompson Feb 12 '15 at 22:36
  • Another plugin option: Vim-unimapired provides yo / yO operators that mimic o and O operators but only set paste until insert mode is exited. – Peter Rincker Feb 19 '15 at 23:43
3

To paste text into vim without auto-indentation, you need to put Vim in Paste mode. This is useful if you want to cut or copy some text from one window and paste it in Vim. This will avoid unexpected effects. See: :help paste for more details.

To make a life easier, there is pastetoggle variable which specifies the key sequence that toggles the 'paste' option (:set invpaste).

In example if you'd like to use F2 to disable autoindenting if pasting into terminal in X, you may use the following code into your .vimrc file:

nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
1

Unimpaired.vim solves this problem in the most efficient way I've seen. It provides some mappings to enter Insert mode with 'paste' enabled, then automatically disables it when you return to Normal mode. yo opens a new line below the cursor; yO above. (Just like o and O.)

The plugin also provides a suite of other useful mappings for navigating lists, changing common settings, and some nifty text conversions. The mappings are exemplary: they feel native to Vim without stepping on existing functionality.

I only provide the details in the previous paragraph because many would be reluctant to install a plugin to solve the problem at hand. However, I think they'd be happy with the other (unrelated) things Unimpaired.vim has to offer.

1

When you are on a server or system where you can't or don't want to change the configuration, the quickest way is

:i!

This will enter insert mode with autoindent turned off, where you paste your text. When you are finished you need to type

.

on its own line to end the insert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.