Things I didn't know about nvi, but you probably did

scrottie on 2007-01-17T19:30:59

Like learning a language from a book (or extending your vacabulary from the 'net, where you see but don't hear words), you miss subtleties. Such has been my vi-fu. Going back into a real job for the first time in six years, and the first time in perhaps nine years where I've worked with other programmers who used vi seriously, I'm finding that my self-taught vi skills are, uh, incomplete. Finding these holes sent me on a mission to find other holes, going back and finding and reading good ex and old vi documentation, and here are the fruits of my labor. This list is just stuff that's in nvi -- I might switch to vim, but, for now, I wanted a list of things that were right under my nose for years and years.

^A -- search forward for the word under the cursor

R -- like i, except overtypes instead of inserts

A -- jump to last non-blank character in line and enter append mode (like insert mode)

I -- jump to first non-blank character on the line and enter insert mode

^Y -- staying on same line, center screen on line, then subsequently, still staying on same line, scroll screen down one line, exposing stuff above the current line

:preserve -- can't write to disc for some reason? This simulates a crash, without exiting, and saves the file for recovery with -r

:rewind -- :next moves you to the next file when you do something like vi *, :rewind moves you back to the first file. This exists entirely independantly and in parallel to :fg :bg stuff.

:set tags= -- load a ctags-formatted tags file and use it

:ta -- I knew vi had ctags support, but I didn't ever really think about the prospects of supporting Perl, or aware that it was already done -- just never occured to me -- but it is supported!

^] -- find the definition or next occurance of the tag under the cursor

^T -- return to where we were before the last jump-to-tag with ^]. nvi has a whole tag stack, but I won't get into that here.

:tagn -- jump to next occurance of a tag -- nvi supports the concept of multiple definitions of the same tag with a previous/next for them

:tagp -- jump to previous occurance of a tag

% -- as an address (eg, instead of 10p or 1,$!), means the entire file (same as 1,$)

%!sort -- sorts all of the lines in the file

> -- indents the current line by whatever tabstop is set to

.,/^}/> -- example -- indent form here to the next } starting a line

:g -- performs a global match-and-execute-command operation, eg:

:g /h3/ d -- delete all lines that match /h3/

:g! -- same but inverse match -- performs on lines that don't match

:map -- assigns a (hopefully unused) character to some other character sequence as a command mode alias

:map g blurgh -- haven't played with this much so I don't know if it lets you do things like %!sort on the right hand side

:set filec= -- enables file completion for file arguments in : mode -- sets a key (hopefully a control key or similar) that when used in : mode attempts to complete partially typed filenames -- yay!

:set cedit= -- sets a character for command mode to open up a buffer on the : command history and let you edit past commands and execute them again with

:E -- splits the current screen/buffer and loads a new file in a new buffer/screen half :Edit -- long version of same

:Bg -- background the current window -- uncertain about diff from :bg

:Fg -- add a screen half for the currently loaded buffer for

:display screens -- show which screens/buffers are loaded

:fg -- foreground an already loaded buffer over the whole screen

:bg -- background the whole screen

^W -- change which split window you're in of the those on the screen

I'm an nvi user -- I started with the *real* AT&T ex, pre-Bill Joy, so keep in mind that vim is a radical departure for me. Some of the defaults annoy the hell out of me -- syntax highlighting, automatic indenting... yes, I can turn them off, but I couldn't seem to make an rc file to do so -- the normal vi conventions for doing so are broken, and even vim users had lots of trouble doing so because of this brokeness. I'm not sure how or where, but it seems like there are lots of other pieces of brokenness. Or maybe not. But now I have incentive to find out. That'll be another article, though.

-scott


More vi

runrig on 2007-01-17T22:38:50

%!sort -- sorts all of the lines in the file

I've rarely found that useful...more often it's:

ma -- mark line as line "a" now go down to another line
mb -- mark line as line "b"
:'a,'b!sort -- sort lines "a" through "b"

Re:More vi

Aristotle on 2007-02-07T07:41:24

In vim, I use a visual selection for the job. Shift-v in normal mode takes you to visual line mode; select the line range you want to manipulate, then type :!sort. Note that vim will automatically type '<,'> for you when you go from visual mode to command mode, so the command actually executed is '<,'>!sort, which filters the selected lines through sort.

Very handy. Also nice for selectively perltidying code and such.

Config file

sigzero on 2007-01-18T13:31:18

Put a .vimrc file in your home directory and in the file put "syntax off" and you should be good to go.

another vi indent trick

awwaiid on 2007-02-03T06:34:14

All the things you mentioned apply to vim also, I believe. Here's one that I know works in vim, and might work in nvi.

    >i{ -- indent the enclosing { ... } block

--Brock