When I have some text indented as follows (like a and b variables assignment):

    a = 1;
    b = 2;

and I want to paste a and b block somewhere outside of the if block, i.e just after it, vim adds the indentation starting from the second line of visual selection, resulting:

a = 1;
    b = 1;

Does exist such an option that don't expand the indentation (better said that maintains relative column alignment for each line) when text is pasted, without using a custom function ?

EDIT: I mainly use gvim.

EDIT2: I have already tried set paste and set nopaste without success.

  • Do you paste text from the OS clipboard or from a Vim register (p or P)? May 15 '19 at 21:59
  • I paste from Vim register using "+P. I paste the text into same file where I copy from.
    – simo-zz
    May 15 '19 at 22:10
  • @simo-zz Check register type: echo getregtype('+'). Also describe how did you copy.
    – dedowsdi
    May 16 '19 at 9:09
  • @dedowsdi, I copy using "+ygv and paste using "+P". The getregtype('+') ofcourse returns the yanked text. Don't you experience the same behaviour ?
    – simo-zz
    May 16 '19 at 9:35
  • @simo-zz It's getregtype('+'), not getreg('+'). I think it's result it's result might be v. How did you complete visual selection before you call gv? And shouldn't it be gv"+y ?
    – dedowsdi
    May 16 '19 at 9:39

AFAIK, there is no direct way to do that in Vim.

Here is a workaround:

First of all I have the following mapping in my vimrc (From Vim Tips Wiki, Tip 759):

nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'

With this you can visually select the text you have just pasted (or changed).

Note that gp is already used by Vim for

Just like "p", but leave the cursor just after the new text.

So the mapping shadows the default functionality of gp. Maybe you want to change the mapping.

Then I do

  1. Copy the text
  2. Paste it with p (or P)
  3. Reselect the pasted text with gp (the mapping above)
  4. Reformat with = (or un-indent it with <)

Or short: pgp=

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.