How to Make the vi Editor in UNIX Even More Powerful and Friendly

Unix Tools & UtilitiesThe vi editor is the standard editor for the UNIX family of operating systems. It will be there, and as long as you get your terminal type correctly defined (VT100 is usually a safe and adequate choice), it will work. It doesn’t need graphics, and it does not rely on having the special keys defined. (arrows, page up/down, delete) It makes no sense to consider being a system administrator without being very comfortable editing files with this standard editor.

A number of improvements and extensions have been added to vi over the years, to the point that what we have today is really vim or “vi improved.” On many Linux distributions vi is simply vim, two hard links referring to the same file, or one of them is the file and the other is a symbolic link pointing to it. The program can tell how it was called, and it may disable the extended features if you asked for the simpler version. On non-Linux systems such as OpenBSD or Solaris, vi is there once you install the base system while vim is a package you may have installed later.
So, some of the time you get the improved version no matter what, but some of the time it depends. I simply type vim every time I edit a file so I always get the improved version without having to remember whether it matters in this setting. And when I get an error message that there’s no such command, that just tells me that this Solaris system needs to have another useful package added!

Your .vimrc file sets your preferences. Let’s look at ones I find helpful, the comments explain what’s going on:

" Set up a normal environment.  Not all of these may be
" necessary on a given platform, but I explicitly define
" them so they always work the expected way once I have
" this file in place.

" Default tab spacing = 8 columns
set ts=8

" Make default backspace key work like normal.
set bs=2

" Some environments set textwidth to force automated
" line wrap.  Disable this.
set textwidth=0

" Display a status-bar.
set laststatus=2

" Improve the interface

" Auto-detect the language and show its syntax with color.
syntax on

" Automatically maintain indentation, possibly using
" the syntax to increase and decrease it.
set autoindent

" Show matches for () {} [].  Type the closing one and the
" cursor will briefly jump back to its mate if it's within
" the current screen.
set showmatch

" Personal preference

" Turn off multi-level undo.  Now pressing 'u' will
" toggle the last change off and on, as opposed to
" rolling back and undoing all previous changes in
" reverse order.  Sometimes I find it useful to
" compare the last two states, but I never find it
" useful to undo a whole series of changes!
set undolevels=0

So far, this is pretty straightforward. Other than my very personal dislike of multi-level undo, I can’t imagine that anyone wouldn’t find these helpful.

Let’s go deeper!

A number of projects have required me to keep track of the number of words in a file. Yes, you can always press :w to write out the current version of the file, then press :! to start a shell escape, followed by wc and the file name. But can’t we automate this?

Yes! The current word count can be displayed in the status bar at the bottom. We have to be careful how we try to do this, tell the editor to wait at least one second after the last key press to recount the words.

" Status bar with automated word count and more
let g:word_count=""
function WordCount()
        return g:word_count
function UpdateWordCount()
        let lnum = 1
        let n = 0
        while lnum <= line('$')
                let n = n + len(split(getline(lnum)))
                let lnum = lnum + 1
        let g:word_count = n
" Update the count when cursor is idle in command or insert mode.
" Update when idle for 1000 msec (default is 4000 msec).
set updatetime=1000
augroup WordCounter
        au! CursorHold,CursorHoldI * call UpdateWordCount()
augroup END
" Set statusline, built up here a piece at a time
highlight User1 ctermbg=green guibg=green ctermfg=black guifg=black
set statusline=%1*      " Switch to User1 color highlight
set statusline+=%<%F    " file name, cut if needed at start
set statusline+=%M      " modified flag
set statusline+=%y      " file type
set statusline+=%=      " separator from left to right justified
set statusline+=\ %{WordCount()}\ words,
set statusline+=\ %l/%L\ lines,\ %P     " percentage through the file

I like this! I get the file name, followed by “+” if I have made any change since the last time I saved it with :w, then the language if detected (“html”, “cpp”, etc). Then, the number of words. Finally, the cursor’s line position out of the total number of lines and the percentage that represents. For example, I see that this file has 885 words and I’m on line 144 of 149, 98% of the way through the file.

To learn more, have a look at Learning Tree’s Tools and Utilities course which shows you more power user vi techniques.

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.