Qiang Li
Qiang Li

Reputation: 10865

vi pasting messes with formatting

When I opened a vim editor on terminal, I copied the following text to clipboard from another source

    int thisVal = findMin(m);
    // System.out.println(val);
    m.add(val);

But it becomes

                int thisVal = findMin(m);
                // System.out.println(val);
                //                      m.add(val);

Why is this case and how to paste correctly with the formatting? Thank you.

Upvotes: 21

Views: 24459

Answers (5)

Michael
Michael

Reputation: 71

None of the previous answers worked for me.
What helped is paste the text via shift + insert instead of ctrl + v or smth.
This is direct paste without any enabling or disabling.
Found it here.

Upvotes: 0

Sanath Sastry
Sanath Sastry

Reputation: 157

This is just a simple hack, but it works. S1 : Create your file in <file_name>.txt format S2 : Paste your code and save S3 : Rename your file to whatever file format required

Done !

Upvotes: 0

heavyguidence
heavyguidence

Reputation: 373

In order to permanently save the defined variables:

vi ~/.vimrc

Add following lines, save & exit

:set paste #will enable the paste mode as answered above
:set nu #this will show the line numbers for editor

Some Other allowed parameters can be added from the following list:

autoindent(ai)
autowrite(aw)
exrc(ex)
errorbells
flash
ignorecase(ic)
lisp
list
magic
modelines
number(nu)
showmatch(sm)
showmode(smd)
wrapscan(ws)

Hope this helps to someone else too.

Upvotes: 1

Paul Sanwald
Paul Sanwald

Reputation: 11349

:set paste

should cause indenting to work correctly. As uku points out, you can read more information on paste mode using

:h paste

you can turn off paste mode with

:set nopaste

Upvotes: 42

alok
alok

Reputation: 1340

Use

:set paste

Or

:set noai

Noai (no auto indent) can be again disabled using :set ai

Upvotes: 3

Related Questions