Using Vim

The vi (and vim) editor is available on every Unix system. It is a very efficient and lightweight text editor that is easy to use after learning a few basic commands, which that you can learn by running though the vimtutor tutorial.

vim is particularly useful when working remotely, ssh’ed into a machine.

Vi (vim) also has a lot of advanced features that are very nice, and is very configurable, often through the use of a .vimrc file. However, just a few basic commands is enough to use vim to edit files on our system.

Learning vim (and vi)

Vim operates in two modes:

  1. insert mode: keystrokes are interpreted as inserts into the file contents at the point of the cursor.

  2. command or escape mode: keystrokes are insterpreted as vim commands, which allow a user to do such things as saving, exiting, searching, or moving around in the file.

In insert mode to switch to command mode by hit the ESC key. In comand mode there are many ways to switch to insert mode, one is to hit the i key.

To learn the vim editor, run vimtutor:

  1. ssh into our system and run vimtutor ( more info on remote access, and more info on ssh):

    home$  ssh yourusername@lab.cs.swarthmore.edu
    
    cs$  cd cs40
    cs$  pwd
    cs$  vimtutor           # start the vim tutorial
  2. Go through the sections listed below of vimtutor (the other sections cover more obscure features that are not necessary). It will take about 30 minutes to run through these lessons.

    • all of Lesson 1 (moving around, x, i, A, :wq)

    • Lesson 2.6 (dd)

    • Lesson 2.7 (undo)

    • Lesson 3.1 (p) and 3.2 (r)

    • Lesson 4.1 (G) and 4.2 (searching)

    • Lesson 6.2 (a), 6.3 ( R ), and 6.4 (y and p)

Configuring vim

You do not need to configure vim in any way to use it on our system. However, you may want to based on foreground and background terminal colors, and set some other options

In your home directory you can add a configuration file for vim, named .vimrc. On start-up, vim examines this file to set different configuration options for the vim session, including setting a color scheme, and default window size.

To see if you have one already, run ls -a in your home directory to list all your dot files:

cd
ls -a

If so, you can open it in vim and edit it like any other file:

vim .vimrc

If not, it is often easiest to start with someone else’s .vimrc file and then edit what you want. From your home directory, you can copy someone else’s. Here is one you can copy over into your home directory that uses a color scheme that shows up well on dark background windows:

cd
cp ~newhall/.vimrc .

At the bottom of this .vimrc file are some settings to give you a visual cue about not line wrapping source code in files. One sets the widow size to 80 (or 81) chars when you start vim, others can be used to set visual reminders when a line exceeds 80 chars:

tail -20 .vimrc   # dump the last 20 lines of the file .vimrc
                  # note lines that start with double quote are comments

set colorcolumn=80
highlight ColorColumn ctermbg=darkblue
" resize to 81 to more easily see colorcolumn (set to 80 if don't use colorcolumn)

set columns=81
autocmd VimResized * if (&columns > 81) | set columns=81 | endif
set wrap
set linebreak

" visually shows +++ when go past columns limit
set showbreak=+++

You can copy this into your .vimrc file if you’d like this functionality