My Linux distribution of choice is CentOS so that is where I typically spend time. Recently though I have been working for a project that runs on a Gentoo Linux server. So far I really like Gentoo though there are some things that I am used to that are totally different. One minor issues I have had is how vi/vim (vi IMproved) handles text that is pasted into it. Every time I was pasting data into a file I was editing with vi. Initially I thought it must have been some odd formatting coming from the file I was copying the text from however I ruled this out quickly when I pasted into notepad first to remove all formatting and then into vi and sure enough the spaces on the left still existed. Below I explain the issue, provide an image of a shell window showing the issue, and how to resolve the problem.
Below are two images that help describe the issue. The first image is of a sample of the vi manpage that I will be copying from the shell window to paste into a file being edited with vi. The second image shows the pasting of the vi manpage into a file being edited with vi. As you can see the spacing continues to get worse as the lines go down. It looks as if the tab key was clicked once before the second line was pasted into the file, the third line had the tab button clicked twice before it was ented, etc. This continues for each line and eventually the spacing is so the text breaks a line so there is an entire line of spacing before the text is even entered.
Example Manpage Text That Will Be Copied To Be Pasted Into Vi Edited File:
- NAME
- vim - Vi IMproved, a programmers text editor
- SYNOPSIS
- vim [options] [file ..]
- vim [options] -
- vim [options] -t tag
- vim [options] -q [errorfile]
- ex
- view
- gvim gview evim eview
- rvim rview rgvim rgview
- DESCRIPTION
- Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is
- especially useful for editing programs.
Pasting Text Into Vi Adds More Spacing To The Left Of The Start Of Each Line:
- NAME
- vim - Vi IMproved, a programmers text editor
- SYNOPSIS
- vim [options] [file ..]
- vim [options] -
- vim [options] -t tag
- vim [options] -q [errorfile]
- ex
- view
- gvim gview evim eview
- rvim rview rgvim rgview
- DESCRIPTION
- Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is
- especially useful for editing programs.
As you can can see above in the second example this makes pasting into vi/vim pretty much unusable. Luckily the problem is really easy to fix. In the user(s) home directory either modify or add a file called .vimrc. This file will act similar to .bashrc which is where all the bash environment settings are stored. In this case .vimrc is where all of the VIM settings will be stored. We are only going to add one line to the file which is going to tell vi to not auto indent. What is happening is that vi is indenting an extra time for each line that is pasted into the file. In the example below we are going to add a new file by typing the below command as the root user.
Add The .vimrc Vi Configuration File For The Root User:
- hostname ~ # vi ~/.vimrc
The above will open a new file called .vimrc in the root users home directory. This is the file that vi will look at for configuration settings every time the root user launches it. Add the below line to the file by typing “i” to insert text and once completed typing the text “:wq!” to write and quit the file.
Vim Configuration Line To Not Auto Indent:
- set noai
Now when you copy text from another file and paste it into a file being edited with Vim it will not auto ident each line. So you can finally use vi without having to manually format all of the text you paste into the file. make sure that when you add the above file you do not get it confused with .viminfo which is where vi/vim information such as the list of typed command history is stored. If you attempt to add the “set noai” line vi will actually complain and let you know that you cannot add set commands to .viminfo.
Vi is a powerful text editor that can be used for all sorts of cool stuff. Check out the books below for detailed vi configurations and uses.
Also try :set paste and :set nopaste after you are done. I’ve also heard that this is only problem with the console version of vi(m) as it has no means of differentiating between typed and pasted text. Gvim should handle that fine (untested).
Hello @tkramar;
Awesome. Thanks for the info. I checked out using the “:set paste” and “:set nopaste” which worked like a charm. That rocks for working on other peoples servers where this occurs so I don’t have to go around modifying any files that they have not asked me to modify. :) I typically work remotely from a shell over SSH which is the case on this project and I don’t have another Gentoo server handy right now to test gvim. Whenever I get the chance though I am going to check it out.
Thanks again for the info. It is always really nice when I post an article and not only help a couple people but others post more information which allows me to expand my knowledge. I really apprecaite you taking the time to post expanded data from what I initially had.
If you have time and are interested check out our newly launched Engage site which allows people to ask/answer/etc. technical questions in a community based format. Would love to hear any feedback you may have and to hear if it is something you would use. I have not attempted to drive much traffic to it yet as I am letting it slowly catch on so I can improve the software as the user base grows slowly. Again I appreciate the time…
Thanks.
alex