Computer Science & Computer Engineering

Introduction

You will often need to edit text files using a plain-text editing program. There are many options for this, but you must be sure that you are using a plain text editor, rather than an editor that adds formatting metadata. The technique that we recommend depends on the situation in which you are trying to edit a file. Our recommendations fall into two categories:

  • if you want to edit within an ssh session
  • you’ve mounted the network drive and want to use an editor you’ve installed on your personal machine (i.e. your laptop or desktop).

Editing within an SSH session

Once you’ve connected to the department machine with ssh from within a terminal window, you can then edit files using one of the following three command-line text editors. By far the easiest to use (but least powerful) is nano. Two other programs, vim and emacs are far more popular among developers because they are more powerful; but they are also more difficult to learn. There is a holy war between these two user groups as to which is the best; but you should just pick one or the other, and become an expert user as it will make you a far better programmer in the long run. Be cautioned though that neither is easy to learn very few people knows all of the features of either because they are both so powerful.

nano

To use nano, navigate to the directory that holds the file that you want to edit, and issue the nano command followed by the file name of the file you want to edit. Suppose you wanted to edit a file named main.c, then you would use:

nano main.c

You will be able to move the cursor around in the file using the arrow keys on your keyboard. At the bottom of the screen you will see reminders of the available commands; they be shown as a caret (ˆ) followed by a letter. The caret (ˆ)represents the Ctrl key on your keyboard. So, for example ˆX means that you should press Ctrl-X on your keyboard (which is the command to exit the editor).

vim

To edit a file with vim, navigate to the directory that has the file you want to modify, then issue the vim command. If you wanted to edit main.c, for example, you would issue:

vim main.c

The premise of vim is that your hands should never leave the home row keys of your keyboard. You do not even use the arrow keys to navigate the cursor, nor do you typically use Ctrl or Alt key combinations. Instead, vim has two modes, one that is used to enter commands, and another that is used to enter text into your file. To put yourself into command mode, you press the Esc key on your keyboard (the only key that requires you to move your hands away from the home row). Once in command mode, you can navigate with j/k for up and down movement, and h/l for left and right movement. There are other commands that allow you to move by more than one row/character at-a-time.

To put yourself in text-editing mode, you press the i key, and then you can enter text into your file. To get back to command mode, press Esc. There are alternative ways to put yourself into text editing mode.

When you want to save your file, ensure that you are in command mode, and then type :w (colon, then w) and press enter. When you want to exit the editor completely, ensure that you are in command mode and type :q (colon, then q) then press enter. If you want to discard changes that you made to your file when you quit, then you need to use :q! instead. Again, there are alternative ways to save, quit, save-and-quit, and so on.

To learn more, type vimtutor at the command-line or search the internet for vim tutorials to behold the awesome power of the other features of vim.

emacs

To edit a file with emacs, simply navigate to the directory with the file you want to edit, and issue the emacs command. For example, if you wanted to edit a file named main.c, then you would:

emacs main.c

You can use the arrow keys on your keyboard to navigate through the file and simply type to modify the file. To exit, you can Ctrl-x then Ctrl-c. It will ask you if you want to save, and once you responded, it will exit.

Editing from your Computer

If you’ve mounted a remote directory, then you can use whatever plain text editor that you have installed on your machine. There are many options to choose from, we have only listed several popular options.