Edit Configuration Files






Edit Configuration Files

When you need to edit a configuration file from the command line in a pinch, use these tips for the ins and (especially) outs of vim.

If one thing is for sure about Linux, it's that it has no shortage of text editors. This reflects the largely text-based nature of Linux, from the command line, to the source code, to the configuration files that programs refer to. When you are in a desktop environment, you can use one of many graphical tools to edit text files; however, in an emergency situation, or when logged into a machine remotely, you may need to fall back on a command-line text editor. Under Ubuntu, the default text editor is vim, and this hack provides you with the basic information you need to make changes to configuration files using vim.

The vi editor has a rather mixed reputation. Some people love how quickly you can edit files with it, and others hate its steep learning curve. The vim editor (short for "Vi IMproved") takes vi and adds not only a number of powerful improvements, but also a number of user-friendly changes such as a complete, integrated help environment. Ubuntu includes vim by default, and even if you run vi, you are actually running vim in a vi-like emulation mode.

So, to edit a file, run vim on the command line followed by the filename as an argument. For instance, to edit the filesystem table (/etc/fstab), type:

$ sudo vim /etc/fstab
         

Unlike most other editors you may be used to, vim operates via a number of modes. Different keys perform different functions depending on which mode you are in. (This can be rather confusing the first time you use vim, since you will start typing only to see all sorts of strange and undesirable behavior.) For starters, there are two main modes you will be operating in: Normal mode (for navigating the file) and Insert mode (for editing text).

Navigate Through the File

Normal mode is the main mode you default to when you start vim. In this mode, you use a number of different keys to move through the file. Figure lists some of the main navigation keys and their functions.

Table VIM Normal Mode keybindings
Navigation keyFunction
h, j, k, lMove left, up, down, or right, respectively. The arrow keys will also move the cursor.
w, bMove forward or backword one word at a time, respectively. Useful when you want to skip through a file quickly.
^, $Move to the beginning or the end of the current line, respectively.
gg, GMove to the beginning or the end of the file, respectively.
: number Move to the specified line number in the file.
/ keyword Search for the specified keyword; hit n to move to the next keyword.


Edit a Line

Once you have moved to where you wish to edit, you have a number of different options to edit a line. If you simply need to delete some text, the x key will delete the character under the current cursor. To add or change text, you will need to switch to Insert mode. Once in Insert mode, vim behaves a lot like other text editors; when you type a particular key, it will appear in the file. There are a number of ways to get into Insert mode, depending on where you want to start editing, but the simplest is to hit i, which will let you edit at the current position of the cursor.

Once in Insert mode, you can make any changes you need to a particular file. You can hit the Backspace or Delete key to delete characters and can still use the arrow keys to move around in the file. Once you are finished, hit the Esc key to exit Insert mode.

Copy and Paste

Sometimes when editing a file it is useful to copy and paste one line or a section of a line to another line. There are a number of ways to do so, some faster than others, but for new vim users, one of the easiest ways is through vim's Visual mode. To select a portion of text, type Esc until you are in Normal mode, move to the beginning of the text you want to copy, and then hit the v key to switch to Visual mode. Now move the cursor as normal, and you will notice that characters will be selected as it moves. When you have selected all of the characters you want, hit the y key to copy (or yank) the text. Then move to where you want to paste it and hit p.

If you know you want to copy full lines at a time, enter Visual mode with capital V instead of v. Now move up or down to select a number of lines and type y to copy and then p to paste your lines.


Undo

Once nice feature that vim has that vi doesn't is unlimited undos. If you make a mistake and want to undo it, hit Esc until you are in Normal mode, and then simply type the u key to step back through each edit. If you find you need to redo a change, type Ctrl-R.

Save and Exit

After you have made your changes and are ready to save, you need to switch to Command mode. Command mode lets you run special vim commands, including the commands you need to save changes and exit. To save (or write) your changes, type :w and press Enter. When you are ready to quit, type :q and press Enter. You can also combine these two commands into a single command, and type :wq and press Enter.

Search and Replace

A common need when editing files is to substitute one word for another throughout the file. To do so in vim, type:

:s/word/replacement/g

to change all instances of the word on a particular line. To change all instances of the word within a file, type:

:%s/word/replacement/g

If you want to give confirmation before making each change, add a c to the end of the command:

:%s/word/replacement/gc

and vim will ask you before it makes any changes.

One particularly handy use of the search-and-replace feature when editing configuration files is for commenting or uncommenting multiple lines in the file. To do so, enter Visual mode, select each line you want to comment, and then type:

:s/^/#/

to comment the lines out with the # character. Replace # with whatever character that file uses for comments. If you instead want to uncomment a number of lines, select them in Visual mode and then type:

:s/^#//

or replace # with the character your file uses for comments.

Help

If you find you are lost, or you just want some help with vim's options, type:

:help

to enter vim's help screen. You can also follow :help with a particular keyword, so if you want to find out what the w key does, for instance, type:

:help w

Where to Learn More

This hack is by no means an exhaustive tutorial on how to use vim. vim has a number of advanced and powerful features that, once learned, allow you to make rapid changes to files. If you want to know more about vim's features and how to use them, type:

$ vimtutor
            

at the command line to try vim's built-in interactive tutorial program.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows