Linux 101 - Editing text files in the Terminal

Introduction

One of the common ways to pass parameters to programs (especially in the terminal) is to use text files. Thus, I will inevitably be useful to know how to edit text files while in the terminal (even if you prepare this files in a GUI beforehand - for a list of recommended GUI text editors, see here).

There are terminal commands that provide limited text editing capabilities, and fully featured text editors. Both of these options will be covered in this section.

Append text and output to a file using cat

In a previous section the cat command was used to display the contents of a file. However, the main function is to concatenate text to output, as the abbreviated name of the command implies. Here we will use the GROMACS log file (download here) to demonstrate the versatility of the command.

Concatenate the contents of a file to another file

Basic Syntax

$ cat *text file path* > *new text file path*

Example

Copy the entire contents of the file gromacs.log to a new file gromacs_backup.log :

edit_linux_1

Concatenate text in the Terminal to a new text file

Basic Syntax

$ cat > *text file path* <<EOF
   some text line 1
   some text line 2
   EOF

Example

Concatenate a student name and department name to the file user.info:

edit_linux_2

Redirect a message in the terminal to a file using echo

The echo command is generally used to output a message to the terminal.

Common Use

Basic Syntax

$ echo "message to print to terminal"

Example

Print the message 'Hello World!' to the terminal :

edit_linux_3

Redirect a message in the terminal to a new file

Basic Syntax

$ echo "message to print to terminal" > *new text file path*

Example

Output the message 'Hello World!' to helloworld.txt:

edit_linux_4

For more advanced editing of files, a full text editor is required. Fortunately, there are many options for this task in the Terminal. The two most common editors are vi(m) (henceforth referred to as vi) and nano.

The nano editor is the more user friendly option for new users, however, it is not installed by default on all Linux distributions. For example, while nano is installed on the login node of the UFS HPC, it is not installed on the compute nodes. However, vi is installed on all nodes and is also generally a default terminal editor in most Linux distributions.

Please note that the content below does not come close to explaining or showing the full potential and power of these text editors (especially for vi). The goal is show new users how to open a text file, make changes, and to save those changes. Additional resources will be provided at the bottom of this section if there is an interest in delving deeper into these editors.

Editing files with nano

Because nano is more user-friendly for new users, it is recommended that new users use it as far as possible in the Terminal.

Basic Syntax

$ nano *file path*

Note: If nano is given a file name that does not exist, it will create a new file by that name.

nano_1

Once nano is opened, it takes over the terminal screen. Notice some feature in the screen-shot above:

  • At the top, the current file name is shown in the middle, with the term Modified on the far right of the screen, which indicates that the file has been changed.
  • The middle part of the screen is occupied by the contents of the text file.
  • At the bottom of the screen is a command reference. The ^ is a placeholder for the Ctrl key on your keyboard. In nano all commands are combinations starting with the "Ctrl" key. Pressing the Ctrl key with the letter indicated will perform the action associated with that combination.

Once in the editor, the user may perform the following actions:

  • Navigate within the file using the arrow keys on the keyboard
  • Delete text, by using the backspace key.
  • Edit text as one would in any text editor.

To write changes to the text file, press the combination: Ctrl * + O* (the letter O, not zero). The bottom of the screen will change - allowing the user to change the file name:

nano_2

If the file name is acceptable, you can simply press Enter to save the file. At the bottom of the screen nano will indicate that it has written lines to the file:

nano_3

To exit the editor, press Ctrl + X on the keyboard and be returned back to the main terminal.
The more command will show that newfile was successfully created and edited:

nano_4

Editing files with vi

Because vi is present on most Linux systems, it is important for users to know how to perform basic editing in the application.

Basic Syntax

$ vi *file path*

Note: If vi is given a file name that does not exist, it will create a new file by that name.

vi_1

Like nano, the vi editor replaces the terminal screen on execution.

The layout of the editor screen is quite minimal:

  • The majority of the screen is covered by the text file contents.
  • At the bottom left, the file name is given in double quotations (""), together with some stats on the file (it shows [newfile] if the file didn't exist before).

vi operates in two modes: Normal mode and Insert mode. In normal mode vi commands can be executed. This is also the mode the editor is in upon invocation of the editor (See the screen-shot above).In insert mode, text can be edited as one would normally expect.

There are many commands in vi - for a quick reference, see here. To enter into insert mode press the i or insert on the keyboard. The term --INSERT-- should appear at the bottom of the terminal:

vi_2

In this mode, normal editing can be performed, with the arrow keys on the keyboard being used for controlling the cursor in the file. Once edits have been performed, the esc key can be pressed to change back to normal mode. In this mode, entering the command :w and pressing the Enter key will write the edited text to the file (a message at the bottom of the screen will indicate that changes have been written to the file):

vi_4

To exit the program, while still in normal mode, type the command :q and press Enter. You may use more to verify that the file have been successfully created and edited:

vi_5

Please note the following:

  • To quit the program without writing changes or receiving prompts, type :q! and press Enter while in normal mode.
  • To write changes to the file and exit the program, type :wq and press Enter while in normal mode.

Outcomes for this section

After completion of this section, you need to be able to do the following tasks in a Linux terminal:

  • Perform basic editing tasks on a text file using the terminal commands used in this section.
  • Create and edit text files in the Terminal using both the nano and vi text editors.