Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

Introduction to vi/vim

We have it pretty good these days. For decades, graphic user interfaces (GUI) have blessed our computer screens, allowing us to perform amazing tasks with relative ease.

Screen_001
Well, maybe not amazing tasks.

IC725016
And maybe not so much ease.

You see, there is one thing that the GUI can never truly improve: writing. Every time you take your hands off the home row to manipulate your mouse or cursor keys, you break the flow of your work. Honestly, it can be easier to erase and retype lines of code than it is to copy and paste them in a conventional text editor.

This is where vim comes into play.

WHAT IS VIM

Vi is a text editor that operates in a terminal setting. This means that we can do every form of text manipulation using the standard keyboard layout, without all this mucking about with mice and menus. For the average programmer-slash-writer, vi is a Godsend.

alpha
So…majestic…

Alright, so vi doesn’t look like much. In fact, vi is a tool of surprisingly limited power. That is why God gave us vim, an extension of vi with bonus features.

delta
Check out the colors and line numbers

If you’re working in Linux (AS YOU SHOULD BE, PEDESTRIAN), you can install vim using your preferred installation method. The apt command is simple enough:

[code language=”sh”] sudo apt-get install vim
[/code]

To run your newly installed vim, you can call either vi or vim (it doesn’t really matter).

[code language=”sh”] vi <file-name>
[/code]

Note that we pass a file name to vi. This is the easiest method to edit an old file or create a new one.

LEARNING BY EXAMPLE – VI COMMANDS

For an example, we will create a file named in our current directory using the following sequence:

[code language=”language=”] vi vi_test.py
[/code]

We now have a blank file with which to go nuts. But how do we start actually typing in vi?

The command: i

By typing the letter I, we are put in INSERT mode, which inputs all keyboard strokes as text in our file.

To stop typing, we hit the ESC key.

Below is a sample Python script you can copy into vi. Note that the editor automatically highlights keywords and comments for us.

Epsilon

In order to enter commands, we first must ensure we are out of INSERT mode (hit ESC). From here, we type :w to save our file, :q to quit, or ūüė° to exit (save and quit).

USEFUL VI COMMANDS

For a more complete list, see the vi cheat sheet at . Here are the commands I use most often.

:w        Save
:q        Quit
:wq        Save and Quit (similar to :x)
i        Insert

ma        Create a mark at the current line with label a
‘a¬†¬† ¬†¬†¬† ¬†Go back to the line we marked with label a

:n        Go to next file
:prev        Go to previous file
G        Go to end of file
gg        Go to start of file
%        Go to matching brace (works for {}, [], <>, () )
$        Go to end of line

dd        Delete current line
yy        Yank (copy) current line
p¬†¬† ¬†¬†¬† ¬†Insert all “yanked” text on new line below current location
y’a¬†¬† ¬†¬†¬† ¬†Yank everything from cursor location to the line marked a (inclusive)

h        Go one character left
j        Go one line down
k        Go one line up
l        Go one line right

.         Repeat everything you just did at current marker

:set number          Turn on line numbers

:set nonumber      Turn off line numbers

CONCLUSION

VI/VIM is my text editor of choice. With this tool, I can easily write, edit, and copy large quantities of code. It’s the best.

Obligatory salute to EMACS, another text editor. People use either vi or emacs, and it really doesn’t matter.

I’m a vi guy, though.