## 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

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...

I have a file let say animal.txt

i11d2v3182121281,horse, miam
11d2v3182121281,horse, utah
11d2v3182121281,horse, new york
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

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

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

Taufik Zukhan F said...

[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

@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...

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

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!
{"extraction_info": {"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8597539663314819, "perplexity": 13193.181712503765}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-41/segments/1412037663372.35/warc/CC-MAIN-20140930004103-00426-ip-10-234-18-248.ec2.internal.warc.gz"}