Monday, January 4, 2010

Vim copy paste indentation issue


While pasting something into a file opened in vim editor, sometime you will see kind of a "staircase" effect where each line is progressively spaced farther outward.

e.g.

I was doing to copy paste the following lines into a file opened in vim



And it appeared like this



This happens because Vim assumes that I am actually typing and not pasting, so it indents the lines again (I already had autoindent set in my .vimrc), and this results in additional white spaces at front.

To avoid this, I ran the following command in vim command mode (before pasting)

:set paste

Then pressed 'i' to switch to insert mode; pasted the lines. Worked!!

One may disable this by using:

:set nopaste

Vim paste:

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. Setting this option is useful when using Vim in a terminal, where Vim cannot distinguish between typed text and pasted text.

This can also be achieved by setting:

:set noai

i.e. set 'no auto-indent'. And 'auto-indent' can be set by

:set ai

You might also like:

- Using tabs in vim editor, a small tutorial

11 comments:

voyeg3r said...

what font ttf is used in this post?

Bart Trojanowski said...

You can also use :a which lets you paste text w/o smart indents.

James said...

Since I seemingly am copy-pasting outside the buffer I ended up putting the following line in my .vimrc:

set pastetoggle= " Turn on/off formating for pasting from clipboard

Works like a charm!

Taufik Zukhan F said...

Hi, i need your help.

I have a file let say animal.txt

i11d2v3182121281,horse, miam
11d2v3182121281,horse, utah
11d2v3182121281,horse, new york
3483hh43943934m,eagle, philadelphia
3483hh43943934m,eagle, tennese
3483hh43943934m,eagle, auckland
23dshjfd0923898,cat,cal
23dshjfd0923898,cat,melb
23dshjfd0923898,cat,africa

then i want the output of this file is.

11d2v3182121281,horse, utah
3483hh43943934m,eagle, tennese
23dshjfd0923898,cat,melb

So i just need one of the each group to be taken by awk or maybe sed. Thnx

Jadu Saikia said...

@Taufik, thanks for the comment. Is this going to help ?

http://unstableme.blogspot.com/2008/03/remove-duplicates-based-on-fields-awk.html

Please let me know.

Taufik Zukhan F said...

Hi Jadu,

Same as your link recommend..

[mild@mildseven]$ awk '!x[$1]++' FS=":" animal.txt
awk: syntax error near line 1
awk: bailing out near line 1

How do u think ? Thnx

Jadu Saikia said...

@Taufik,
the following two should work for you

$ awk '!x[$1]++' FS="," animal.txt
$ awk -F "," '!x[$1]++' animal.txt

Also try using nawk or /usr/xpg4/bin/awk for solaris (instead of awk). Please let me know if that works.

Taufik Zukhan F said...

Hai Jadu,

Yes, its works... i am used solaris.

Very well Thanxs. Jadu

Adithya Kiran said...

If you can access the clipboard through the * register, then you can paste the text without indentation using CTRL-R CTRL-O * in insert mode.
Similarly, you can also toggle the paste option using:
:set paste!

rafiks said...

that worked! thanks a lot!

abhijit pai said...

very useful.. thanks!

© Jadu Saikia www.UNIXCL.com