Linux Terminal Tutorial - Editing Text Files

Introduction

This section will demonstrate how to edit the contents of text files in the the Linux Terminal.

As a general first tip: Often one would like to copy text and paste it into the terminal or copy text from the terminal to paste elsewhere. To do this in the terminal use ctrl + c to copy and ctrl + v* when copying or pasting text to and from the Terminal.

Append text and output to a file using cat

While the cat command can be used to view the contents of files (as in the previous section), its main function is to concatenate text to output.

Let us demonstrate this functionality by concatenating the output of the log file nvt_expr_1.log to in Tutorial/nvt_expr_1 to a new file, nvt_expr_1.log_backup:

$ cat nvt_expr_1.log > nvt_expr_1.log_backup

It is also possible to concatenate text typed in the terminal to a file using cat.

Concatenate two lines of typed text to the backup file created in the previous step:

$ cat > nvt_expr_1.log_backup <<EOF
some text line 1
some text line 2
EOF

Attention

There should be no spaces before the final EOF

The two lines of text should now appear at the end of the file. Confirm this using the tail command:

$ tail nvt_expr_1.log_backup

The echo command is used to print text to the terminal screen. However, the text can be redirected to a new file.

Append text to a new file new_echo_file using echo:

$ echo "Add this text to the file" >> echo_file

Note

> overwrites the file on the right with the input from the left
>> appends the input from the left to the file on the right

Using vi to edit files

The editor vi is found on mostly all Linux distributions (unlike nano which is covered after this section). While not considered the most user-friendly, it is the most powerful text editor on the platform. This section will demonstrate the basic usage of vi.

First, open the text file created in the previous section (echo_file):

$ vi echo_file

The vi screen will consume the entire screen and you should see the one line of text added earlier.

There are two modes in vi: Normal mode and Insert mode. In Normal mode commands can be issued to the editor, while in Insert mode, text can be edited in the file.

The editor starts in Normal mode. To switch to Insert mode, use the i or insert keys on the keyboard. To return to Normal mode, press the esc key.

With echo_file open in vi, enter Insert mode and add some text. Next, switch to back to Normal mode.

To save the changes to the file, type :w and press enter.

To quit vi, type :q and press enter. If the file has been edited and you want to save and quite, type :wq or type :!q to quit without saving.

Using nano to edit text files

Another popular Linux Terminal text editor is nano. However, please note that nano isn't installed by default of some Linux distributions.

As with vi we will open the echo_file with nano:

$ nano echo_file

As with vi, nano takes over the entire terminal. However, unlike vi, nano does not have different modes. To edit the file, simply start editing as one would in any other text editor (except vi ;) ). Add some text to your file.

To issue commands, nano uses the ctrl key together with another key. Thus, to save the file, press ctrl + o. At the bottom of the screen you will be prompted for a file name and confirmation to save the file. Press enter to save the file.

To quit the program, press ctrl + x to be return back to the Terminal. The program will prompt you to save the file if it was unsaved.