Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra spacing in Vim when copying and pasting from Chrome [duplicate]

Tags:

vim

Whenever I copy text that has been indented into Vim from Chrome, I get a cascading indent instead of a consistent one, so:

def fn(x):
    """Takes x as an input and returns y
    if x:
        return y

becomes:

def fn(x):                                                                  
        """Takes x as an input and returns y                                
                if x:                                                       
                                return y

How do I prevent this (preferred), or how do I quickly fix the spacing using other vim commands (acceptable)?

Extra Info

  • vim 7.4, chromium 34 on Arch Linux
  • Copy method: highlight (mouse or otherwise) in Chromium, CTRL-C, switch to vim and insert mode, SHIFT-CTRL-V.
like image 977
Two-Bit Alchemist Avatar asked Apr 24 '14 17:04

Two-Bit Alchemist


People also ask

How do I stop Vim from Pasteing?

If you're in normal mode, Ctrl-C aborts the current command in progress. Then press u to undo anything that changed before you stopped it.

How do I copy and paste in Vim without comments?

Add the pastetoggle=<F2> to your . vimrc so you don't have to set it each time.

How do I paste into format in Vim?

Paste toggleStart insert mode. Press F2 (toggles the 'paste' option on). Use your terminal to paste text from the clipboard. Press F2 (toggles the 'paste' option off).


1 Answers

When you paste using any of your terminal/OS's default method (menu, contextual menu, shortcut, mouse…) the text is not pasted: it is inserted as if you typed it. Because you have enabled autoindenting, Vim indents every line and you end up with that stairway (to hell) effect.

You have two options…

  1. paste/nopaste and/or pastetoggle:

    You can do :set paste to disable autoindenting and formatting before you paste and do :set nopaste afterward.

    If you decide to take that path, I suggest you read :h pastetoggle to make the whole process a little less taxing.

  2. Vim's own commands and clipboard integration:

    If your Vim build comes with clipboard support, you can use "+p or "*p to paste from other programs without side effects.

    The default Vim that comes with most OSes is a "small" build that may lack a number of useful features. If you intend to use Vim as your primary editor for programming it is advised to install a beefier build that comes with clipboard support. On Debian-based systems, the best choice is usually "vim-gtk" or "vim-gnome". On Arch, I think you have to install the "gvim" package.

like image 153
romainl Avatar answered Sep 17 '22 07:09

romainl