Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim indents every line of code when copying-pasting

When I copy and past a block of code in Vim, every line gets indented by one. For example, I have this source:

    print "Hello"
    print "World"
    print "I'm copying"
    print "and pasting"

Which gets jumbled when pasting into Vim:

print "Hello"
        print "World"
            print "I'm copying"
                print "and pasting"

For copying long lines of code, it's very frustrating because everything gets out of alignment (not good for python).

Here is my vimrc. It currently auto-indents on newlines, and replaces tabs with the standard 4 spaces.

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab

While this config works, something is causing the copy-paste problem. How can this be fixed, but still retain the behaviors that I've defined?

like image 256
vuvume Avatar asked Dec 12 '16 16:12

vuvume


People also ask

How do I stop Vim from auto tabbing?

vim" in 'runtimepath'. This disables auto-indenting for files you will open. It will keep working in already opened files. Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

Why does Vim auto indent?

autoindent essentially tells vim to apply the indentation of the current line to the next (created by pressing enter in insert mode or with O or o in normal mode. smartindent reacts to the syntax/style of the code you are editing (especially for C). When having it on you also should have autoindent on.

How do you paste without indent?

vimrc so pressing F2 toggles paste on and off.


1 Answers

Use :set paste to switch to paste mode.

This article explains paste mode

It was made specifically for pasting text into vim so it doesn't trigger any input mappings. Remember to :set nopaste when you are done to get your mappings back.

like image 109
Explosion Pills Avatar answered Nov 15 '22 21:11

Explosion Pills