Ubuntu 13.04 – Command Line Basics: Editing Files

This tutorial is for Ubuntu 13.04.

It is probably the same for other versions of Ubuntu and (probably) most other Linux distributions, but no guarantee is made.

Detailed instructions for opening a command line / terminal can be found here.

When you work at the command line, from time to time, it may be necessary to edit files.

You can use an editor like gedit (the GUI based editor that comes with Ubuntu), however, by default, it will not permit you to edit system configuration files (which are likely the files you are trying to edit if you happen to be at the command line).

Starting gedit with Administrator Privileges

Enter the following command to start gedit with administrator privileges:

sudo gedit

Note: when you type your password, nothing will happen on the screen. This is normal. As a security feature, Linux does not echo back any indication of the characters you are typing in.

You should only be prompted to enter a password if you have not used the sudo command in the last 15 minutes or so.

You can also append the file you wish to open to the command. This will cause gedit to automatically open the file. For example:

sudo gedit history.log

Note: this will only work if the file is in the current directory. However, if you are at the command line, then you probably have navigated to the directory in question.

Editing with nano

Sometimes, you cannot use gedit because you have no graphical interface (GUI) – for example, you need to edit some x-server settings because you can’t get a graphical display, so you are exclusively at a command line.

While vi or one of its clones (vim, elvis, nvi, and vile to name some) tend to be the preferred text editor among Unix / Linux geeks – it is not the friendliest for casual (or one-off) users. The other major editor is emacs, but it is not installed by default in Ubuntu and it is not user friendly either.

nano is a clone of the pico editor.

To launch nano enter the command:

sudo nano

Note: It will also launch if you type in pico

As with gedit you can append the name of the file you want to edit to the command.

Using nano

nano is a simple editor that allows you to navigate through the text using the arrow keys on your keyboard.

At the bottom of the screen, there are two lines (highlighted in blue) that display a number of common commands.

The caret (^) symbol means Ctrl, as in, Ctrl + X. This means, “while holding down the Ctrl key, you press the other key as though you intend to type it”.

DO NOT press the Shift key because Ctrl + Shift + X is different from Ctrl + X.

It does NOT matter if your Caps Lock is on or off.

The most common commands you are likely to use are:

  • ^X – (Ctrl + X) exit the editor
  • ^O – (Ctrl + O) save / write the contents of the editor
  • ^R – (Ctrl + R) open / read another file

If you decide to browse through the help (Ctrl + G), then be aware of the following:

  • ^^ means Ctrl + ^ not Ctrl + Ctrl
  • M- means the Alt key. The Alt key is used the same way the Ctrl key is: it is held down while another key is typed. For example: M-\ means: while holding down the Alt key, press the \ key as though you intend to type it.
  • Some commands have parentheses around them, the parentheses are not to be typed. For example, (M-X) simply means Alt + X – the parentheses have nothing to do with it and should be ignored.