[CS3190unix-list] Dr. Eggen's question about vi and pasting text

Berna Massingill bmassing at cs.trinity.edu
Wed Mar 24 20:29:29 CST 2004


Several weeks ago Dr. Eggen asked a question about how to get vim to
do something reasonable when you're pasting text into a vim session
using the mouse.  Here's a bit of a follow-up.

Asked to clarify what he's doing, Dr. Eggen writes:

>>  you have two windows open, and you want to quickly grab some text
>>  from one window and paste it into the other. on the left is the text
>>  you wish to grab. on the right is a vim session in input mode. you
>>  sweep (left) mouse through the text on the left, you go to the right
>>  window and paste with middle mouse. the text with autoindent goes
>>  scattering across the window and ... well, not good. 

>>  i simply unalias vi (to get old vi) and in .exrc set noautoindent.
>>  that works but deprives me of some of the features of vim that i
>>  like. 

A semi-solution to the problem is to unset autoindent before doing the
paste.  Before going into insert mode to paste, ":set noai".  Then do
the paste.  The indentation should be pretty much like whatever you
grabbed from the other window.  Then ":set ai" to turn autoindent back
on.  An alternative is ":set paste" before pasting and ":set nopaste"
after.  This resets autoindent and some other options (":help paste"
to read more).

For editing code, though, I like vim's "automatic code indentation"
feature (":help C-indenting" for possibly more than you wanted
to know).  With this feature on, vim figures out how much to indent
each line based on the programming language it thinks you're writing
(C for .c files, Java for .java files, etc.).  Here are the options
I use to get behavior I like (4-space "tab stops", all indenting done
with spaces):

set nocompatible
filetype on
filetype indent on
set textwidth=80
set softtabstop=4
set expandtab
set cindent shiftwidth=4
set ruler
set backspace=2
set formatoptions=tcql
syntax on

Put these lines in a file, say .vimrc-code, and then start vim by
typing "vim -u .vimrc-code filenametoedit".  If you want these options
always, just name the file .vimrc in your home directory and start vim
in the normal way.  

With this setup, you just type as if you were starting each line at
the left margin (don't try to indent/unindent), and vim indents it
in a way that's usually reasonable.  If you don't like a particular
line, you can go back and change it manually.

This doesn't really solve the problem with pasting, but it does provide
a reasonable recovery: To "reformat" a line that has been messed up,
you put the cursor on the line and type "==" (two equals signs).
You can reformat whole sections too.  The easiest way is to select
the section with the "visual mode" feature and then type a single
equals sign.

Hope this helps someone.

-- blm


More information about the CS3190unix-list mailing list