0

I have been trying to paste text from one file to another in bash. I'm working in Putty.

To be more clear, I have a file hotel.txt with some lines of text that are indented, thus have tabs and spaces. When I paste a few of those indented lines into another file created using vi, they are pasted with a tab extra each.

All my text is automatically indented with each line having an extra tab as soon as I paste using the right click mouse button.

Does anyone have any solution?

I've tried :set paste, but doesn't work with me apparently.

10
  • general coding advice: It is good practice to replace tabs by spaces. Most editors have such configuration option Commented Aug 21, 2013 at 11:55
  • Do you mean while indenting my script, I must use spaces? Thanks alot! Commented Aug 21, 2013 at 11:59
  • @hek2mgl I've heard the other way around. So do most of the people here Commented Aug 21, 2013 at 11:59
  • Yes, in the moment you are coding. My editor for example (vim) inserts 4 spaces when I press tab. I have set expandtab and set ts=4 in my vimrc. This makes things easier, believe me.. :) Commented Aug 21, 2013 at 12:00
  • It's odd that :se paste doesn't work. Commented Aug 21, 2013 at 12:00

3 Answers 3

0

While in vi, enter the escape mode using Esc. Then say :set paste.

Now you should be able to paste text unmodified.

You can turn 'paste' off by saying :set nopaste.

In order to make the change permanent, you can add :set paste to your ${HOME}/.vimrc.

3
  • Just tried it exactly this way. Not working. The text is still indented unncessarily. Commented Aug 21, 2013 at 13:25
  • What about :set noautoindent? Does that work? Commented Aug 21, 2013 at 13:27
  • Yes that works perfectly fine. Thank you so very much!Thats a big relief. Commented Aug 21, 2013 at 13:44
0

This happens because you're either not copying the indentation of the first line or because you're pasting on an already indented position.

1
  • I didn't quite understand you. I create a new file, lets say by vi example and paste my copied and indented code immediately after pressing 'I'. It still creates all the unnecessary indentation. Commented Aug 21, 2013 at 11:59
0

Its not exactly an answer but you can remove the extra tab in the beginning this way

:%s/^^I//g 

where ^I is the tab character and you can type it by pressing C-v and then tab.

(or type the following sequence

gg "goto top c-v "visual selection S-G "goto end of file << " indent visual selection one to the left. 

)

You must log in to answer this question.