Before you start
Objectives: Learn which text editors are typically available in Linux and how to use them (vi and nano).
Prerequisites: no prerequisites.
Key terms: file, text, vi, command, nano, mode, enter, use, editor, awk, close, commands, ctrl
vi Text Editor
vi is a bit different then most other editors. The thing is, the vi editor can work in different modes. Different modes in vi allow us to do different things. Most other editors don’t have support for modes. To execute vi, we simply type in the “vi” and then the file name that we want to edit. In our case we will edit the file “Text_File”. Note that our current working directory is “work”.
vi Command
When the command is executed we will see the content of our sample Text_File.
File Opened
When we start the vi for the first time it puts us in the so-called “command mode”. In this mode the vi waits for the commands. In this mode we can move the cursor left or right. Once we get the cursor to the right place, we have to go to the “insert mode” in order to enter some text. To go to the “insert mode” we can simply type “i”. When we use the “i” mode, the text will be entered before our cursor. The other option is to use “a”, which stands for “append”. This way the text we enter will be placed after the cursor. Once we are in the “insert mode” we can type our text, use returns, and other actions suitable for text editing. To get out of the insert mode we can simply hit the Escape key.
To save the edited file we have to be in the “command mode”, then enter the colon sign ( : ), and “w” after the colon. This command will write changes to the file.
Write File
To close the file on which we are working we would enter “:q” in command mode.
Close File
If we try to close the file which is not saved, we will get a warning.
Warning
To close this file anyway we can enter the “:q!” command.
Close File 2
To save the file and then quit we can enter the “:wq” command.
Nano Text Editor
Nano is another text editor usually available in Linux. Nano is actually the clone of the pico editor. In contrast to vi, nano doesn’t work in modes. To open some file in nano, we simply type “nano” and then the name of the file. We will use our Text_File again.
File Opened in nano
Notice that we can see some nano commands on the bottom of the screen. When we open file in nano, we are mediately in edit mode. We can start typing our text right away. To send commands like “copy”, “paste”, “close”, “open”, etc., we can do this by using the CTRL key and an appropriate letter. As we said, some of those commands are displayed on the screen. For example, to see the help screen we would enter CTRL+G, or to save the file (write it) we would enter CTRL+O. When we write the file it will ask us for the file name, but we can simply hit enter to use the existing file name.
Savin File in nano
To open some file and append its content to the screen we can use the CTRL+R key combination. To see all available commands we can refer to the help page by entering CTRL+G. To exit current window we can enter the CTRL+X.
Nano is much easier to use when compared to vi, however vi is basically always available on any Linux system, while the nano editor is not.
sed and awk
sed and awk are editing utilities which can take text or commands from the terminal and modify text files named in the command line. For example, sed can be used for bulk operations like replacing text in the document, adding margins, etc. For example, to replace the word “Cian” with the work “Cyan”, we can use the command: “sed s/Cian/Cyan originalfile > newfile”. The “s” in the command indicates the substitute option.
awk is similar to sed, but it is more powerful. awk also allow us to build databases, perform mathematical operations, and to create reports from the retrieved data. The syntax for an awk command is: “awk <pattern> {actions} filename”.