Editing configuration files is a routine for Linux system administrators & the equivalent advanced users. Even normal users will at times need to edit configuration files to troubleshoot any problem or tweak occasionally. For those using GUI, Kword or Openoffice Writer can come handy but what if you are on a server system with no GUI or your display is gone bonkers? Fret not guys, let me introduce you to my dear friend, Vi.

Vi is basically a command line editor available for BSD & Linux systems. Vi is designed to quickly edit configuration files which are primarily text files at a shell. The original Vi program was written by Bill Joy in 1976 for BSD Unix system. Since then there have been many clones of Vi namely ViM, Elvis, Vile etc. We will be taking ViM into account as it is bundled with most of the modern linux distros. ViM in theory is an improved version of Vi (the reason for name Vi IMproved) with new features like syntax highlighting, mouse support, new commands etc.

To run Vi, simply input vi or optionally attach a file path after a space at a shell. For example if you need to open a file by name services from the /etc directory, then input vi /etc/services. You should now see a similar screen. Don’t let the sparse interface of Vi scare you away, it’s actually quite user friendly.

/etc/services file in Vi

Vi has two modes – Command mode & Insert mode. By default when you run Vi it opens up under command mode. To edit the file you will need to enter insert mode which can be done by simply pressing ‘i’ on your keyboard. Once in insert mode you can go ahead & edit the file as you would under any word processor.

Now after you’re done editing the file it’s now time to save. As I wrote earlier, Vi uses two modes. It’s now time for command mode to come under picture. Hit the ‘Esc’ key & you should be now taken out of the insert mode. To save the modified file, press :wq & hit enter. The file is now saved. If you don’t want to save the file then insert :q instead. An !(Exclamation) can be used to force Vi to save or discard changes if it fails to do with the above commands. This is somewhat different approach but you will start loving it once you spend considerable time with Vi.

Here are some basic Vi keyboard short-cuts which you should memorize to make your Vi experience far more comfortable.

J – Move the cursor one line down.

K – Move the cursor one line up.

H – Move the cursor to the left one character position.

L – Move the cursor to the right one character position.

X/Del – To Delete character in command mode.

R followed by the character to be replaced – To replace the current character.

Shift + G – To go to the end of the document in command mode.

Double G Tap – To go to the beginning/top of document

: (Semicolon) + Line Number – Directly jump to the line.

Ctrl + F – Clear the entire document.

: (Semicolon) + q + ! - Quit Vi without saving the current file. ! is used as a force measure.

: (Semicolon) + w + q + ! – Save the current file & quit Vi. Here again ! makes Vi to forcibly save the document.

I strongly recommend you learn Vi since it is going to become an indispensable tool once you graduate from a novice to a semi-pro Linux user.

About these ads