Vim Primer for Network Engineers

Vim Primer for Network Engineers

Vi is arguably the best text editing software in the world. There, I said it… deal with it! It should be noted that while many people continue to refer to Vi simply as such, Vim (Vi improved) has been the standard since around 1998. Vi? Old and busted. Vim? New hotness. Vi isn’t an editor designed to hold its users’ hands. It is a tool, the use of which must be learned. It isn’t a word processor. Although it can display text with various forms of highlighting and formatting. If you just had to use Vi to edit a file, there would only be a couple commands you would have to remember.. But that wouldn’t be enough, I hope to give you a basis to realize the true potential of the software, enough to encourage you to buy a book, or search for more tutorials to supplement your new found knowledge.
The Basics

Before we cover a larger list of commands, I want to show you the basics. Vi is a modal editor, it operates in either insert mode or normal mode, where you can insert commands. For example, typing i while in normal mode switches the editor to insert mode, but typing i again at this point places an “i” character in the document. From insert mode, pressing the escape key switches the editor back to normal mode. In normal mode you can quit using :q or if you’ve made changes you wish to save :wq or if you do not wish to save the changes :q! — while these commands will get you buy, but in order to really get vi — you have to experience a bit more…
Cursor Movements

It may seem a bit silly to learn to use the cursor like this when you can simply use the arrow keys, or page up/down.. but these commands can be combined with others to make them more powerful.

h - left one character
l - right one character
j - down one line
k - up one line
w - right one word
b - back one word
$ - to the end of line
0 - to the beginning of the line
) - right one sentence
( - left one sentence
} - right one paragraph
{ - left one paragraph
<Ctrl>+F - forward one page
<Ctrl>+B - back one page
G - go to (without arguments, go to end of file)

Deleting

As I mentioned above, some of these commands use d+cursor movement to accomplish rapid editing. For example dw deletes the next word, d+w for delete + right one word.

d$ or D - delete to end of line
d0 - delete to the beginning of the line
d} - delete to the end of paragraph
dd - delete delete (delete the whole line)
dw - deletes a word
db - deletes the word to the left
dd - deletes the entire line
x - delete character cursor is on

Text Input / Change

All of these commands also require “Esc” to return to Normal mode.

i - insert text before the character cursor is on
I - insert text at the beginning of the line
a - append (insert text after the character cursor is on)
A - append text to the end of the line
cw - change the current word
cb - change backwards
C or c$ - change from the cursor to the end of the current line
R - start overwriting text
o - start entering text at the beginning of the new line below the cursor
O - start entering text at the beginning of the new line above the cursor

Other Basic Commands

r - replace one character - can also be used with "cursor commands"
ZZ - save and exit (hold down shift and press "z" twice)
yy - yank line cursor is on
p - paste below cursor line (deleted or copied text)
P - paste above cursor line
u - undo last editing command
U - undo all changes
. (period) - Repeat the last text changing command
J - Join, combines the current line, and the next line into one.

Advanced Functions

Any command can take numeric argument to run a command multiple times.

5dd - delete 5 lines beginning with cursor line
2dw - delete two words
c3w - change 3 words, this can also be run as above.. 3cw
3<Ctrl>+B - move up three pages
1G - go to the first line

Misc Manipulation

These commands are handy for making rapid changes.

~ - Change case of individual character
<Ctrl>+a - Increment number under the cursor
<Ctrl>-x - Decrement number under the cursor

Search and Replace

What text editor would be complete without having search / replace functionality!? Vi includes an advanced search / replace feature set that supports regex.

/search_string - Search for search_string
?search_string - Search backwards (up in file) for search_string
n - Find next occurrence
N - Find previous occurrence
:s/pattern/string/flags - Replace pattern with string according to flags. (This is done in ex mode)
g - Flag - Replace all occurences of pattern
c - Flag - Confirm replaces
& - Repeat last :s command

Command Line Mode

Command Line mode has more functionality than I could include in several blog posts. These are some basic examples of usage.

: - enter ex mode
:18,24 del - delete from line 18 to line 24
:23,48 copy 17 - block from line 23 to 48 copy to line 17
:2,17 move 92 - block from line 2 to 17 move to line 92
:r somefile - read in "somefile"
:wq - write and quit (same as above)
:w - write (save) if the file permissions allow it
:w! - save file even if it is read-only as long as we own it
:w somefile - save this file as "somefile"
:q - quit without saving
:q! - quit without saving if changes were made

Map / Macro

A map allows you to define strings of commands. When defining a map it is helpful to take advantage of the unused keys in vi (K V g q v * = F1-F12), you also need to be mindful of any special characters such as Esc, Enter, Tab, or any Ctrl characters, use a +v before each special character.

An example of setting up a map would be :map v /I +v cwWe +v which would search for the letter “I” and replace it (using change word) with “We” when the v key is pressed.

:map - Displays all created macros on status line
:unmap key - Removes macro definition for key
:ab - Displays all abbreviations
:ab str string - When str is input, replaces it with string
:una str - Unabbreviates str
q[lower case letter] - Start/Stop recording a Macro
@[lower case letter] - "Play" recorded macro
10@[lower case letter] - "Play" recorded macro 10x

Fun Tricks

I’ve compiled a list of useful tips to pique your interest.
Revese lines on buffer

This command will reverse all lines in the current buffer. This is really handy when you’re working on ACLs on an ASA. (Removing them in reverse) — :g/^/m0 Broken down, this operates using the following commands…

: - start ex / command-line mode
g - means you'll take an action on any lines where a regular expression matches
/ - begins the regular expression (could have used any valid delimiter)
^ - matches the start of a line (which matches all lines in the buffer)
the second / ends the regular expression (the rest is an Ex command to execute on all matched lines)
m - means move
0 - is the destination line number (beginning)

If you do not want to reverse the entire file, you can supply the range of lines to reverse to the command and adjust the destination accordingly. For example, to reverse only lines 100-150:

:100,150g/^/m99

Shift a block of code left or right

Enter visual mode by typing the letter “v” at the top (or bottom) of a block of text, move the cursor to the bottom (or top) of the block of text. Using > or < you can now shift the text 1 tab right or left.
Strip blanks at end of line

:%s/s+$//

Delete all lines beginning with or matching a pattern

:1,$ /^#/d - Delete all (first to last line: 1,$ or g) comments lines in file. Delete all lines beginning (^) with "#" (specify text pattern).
:g/#/d - Delete all lines (g) containing comments (comments follow "#") in file. Delete all lines containing "#".
:g!/^#/d - Delete all lines except (g! or v) comment lines beginning (^) with "#".

Strip DOS ^M

:1,$ s/<Ctrl>+V<Ctrl>+M//

Editor Customization

What text editor would be complete without an endless sea of options!? I’ve highlighted a few here that may come in handy. Obvious there are many many others, but these are the ones I use regularly.

:set all - Shows the state of all options
:set nu (number) - Displays the screen numbers of all lines - Disabled using :set nonu
:set ai - Turns on auto indentation - Disabled using :set noai
:set eb - Precedes error messages with a bell - Disabled using :set noeb
:set ic - Ignores case when searching - Disabled using :set noic
:set sm - Indicates input or replace mode at bottom - Disabled using :set nosm

Conclusions

While I understand that this is a large amount of data to absorb, I sincerely hope that I’ve given you not only enough information to interest you in learning Vi, but also enough of a reference to use as you start learning. The only downside I’ve ever really thought about, is not having it during those critical hours during your lab.

This article was originally posted on the PacketPushers Blog. I am cross posting it here now for posterity, with a 6 months after the original article.

comments powered by Disqus

Related Posts

Networking Field Day 15 – A new delegate emerges

Networking Field Day 15 – A new delegate emerges

Yet again I find myself honored, and questioning their selection methods, by being selected for a Networking Field Day event. Networking Field Day 15 kicks off April 6 and 7th in …

Read More
Using Deny ACEs in your PBR ACL on your Nexus 7k

Using Deny ACEs in your PBR ACL on your Nexus 7k

Quite a while ago I had a need for some network duct tape… Policy Based Routing while useful should only IMHO be used as a temporary fix. But as you know, temporary things soon …

Read More
AS-Path Filtering

AS-Path Filtering

Before we get into the how, let’s talk about the why. According to the CIDR Report, the global IPv4 routing table sits at about 525,000 routes, it has doubled in size since mid …

Read More