This lesson is in the early stages of development (Alpha version)

Common Linux CLI Text Editors

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • What are some of the most common Linux text editors

Objectives
  • Identify Linux text editors available on Hawk

  • Basic commands to edit text files

This section describes three of the most common Command Line Interface text editors for Linux: Nano, Gedit, Vim and Emacs. Text editors can be divided in two main groups, those friendly with new users and with basic features and those feature-rich with a steep learning curve. Nano and Gedit belong to the first group and are recommended for users who only need to perform minor editions in text files, while Vim and Emacs are suggested for users who plan to spend a long time working on the command line and with text files (e.g. programmers).

Nano

Nano Logo A basic and dependable Command Line Interface (CLI) text editor, Nano is arguably the simplest one of the ones covered in this lesson. To start Nano on Hawk:

$ nano 

This should display a “new buffer” in the terminal waiting for text input. The terminal window should look something like this: Nano home With a top line showing Nano’s version and current filename, a white rectangle showing the position of the cursor, and a bottom menu with common commands.

The caret symbol “^” represents the control key Ctrl. So “^X” means that you should press Ctrl+X to quit nano. As you type commands, the menu displayed at the bottom of your screen will update with the currently available options. To cancel a command use Ctrl+C.

Sort the rows…

Lets try nano with an example.

Open the file called nano-rows.txt and try sorting the rows. Nano only rows Save in a new file nano-sorted-rows.txt

Gedit

Gedit Logo The GNOME desktop text editor. Gedit is another basic Linux text editor, the main difference with Nano is that it is a graphical editor so you will need to use X11 (see lesson 1) to work with it. When opening Gedit it will open a new window that resembles Windows’ notepad. Gedit home

Sort the rows… again

Lets try Gedit with the same example.

Open the file called unsorted-rows.txt and try sorting the rows. Gedit rows Save in a new file gedit-sorted-rows.txt. How was different from Nano? Did you get any odd warnings in your terminal while working with gedit?

Vim

Vim logo VI iMproved is a powerful and nearly omnipresent text editor. To open vim:

$ vim 

Vim home This will display a black screen with Vim’s version, Authors, and few basic commands, including how to quit Vim (:+q+Return) and how to get help (:+help+Return). Vim power becomes apparent when the task at hand involves slightly more complex or repetitive text operations. Vim has two main modes of operation “Normal” and “Editing” mode. Normal mode is the default when you open Vim. In this mode Vim can accept commands, like quit. To edit a file, press i (Vim will let you know you are in edit mode with an “–INSERT–” message at the bottom of the screen). To return to normal mode, press Esc.

Copy and paste

Open unsorted-rows.txt. Make sure you are on normal mode and type v to enter the “Visual” mode and use your keyboard arrows to select text. Then type y to copy or c to cut. Move the cursor to the position where you want to paste the text and type p.

Try doing this again, but this time enter the Visual Block mode with Ctrl+v. What did you notice?

Short summary of Vim commands.

Command Description Command Description
:+q+Return Quit u undo
:+q+!+Return Quit without saving Esc Return to normal mode
:+help+Return Get help v Enter visual mode
:+w+Return Save Ctrl+v Enter Visual Block mode
0 Move to beginning of line g+g Move to beginning of file
$ Move to end of line Shift+g Move to end of file

Sort the rows… with a twist

Lets try Vim with yet again the same example.

Open the file called unsorted-rows.txt and try sorting the rows but this time, try moving the numbers in front of the word ‘job’. Gedit rows Save in a new file vim-sorted-rows.txt. Would this have been easier with Nano? What if the file contained 100s of lines?

A tutor for you …

If Vim is available in your platform, it is most likely that vimtutor is also available. Vimtutor is at its core a text file with some useful exercises designed to help you master Vim. To start it:

$ vimtutor 

Emacs

Emacs logo Is another powerful text editor. While Vim has the fastest start-up time of the two advanced text editor discussed in this lesson, and is more widespread, Emacs has, arguably, a more feature-rich environment extending beyond simply word processing and potentially wrapping around everything you do (with some people going as far as stating that Emacs is a “way of life”).

To try Emacs, open it in the command line with:

$ Emacs 

Emacs home This will display a black screen with some useful information including its version, licence and some basic commands. More importantly, Emacs let us know about its command grammar where ‘C-‘ means use the Ctrl key and ‘M-‘ is the Alt key. This is specially helpful to know when searching for help online about Emacs commands.

Try opening again “unsorted-rows.txt” and do some changes.

Short summary of Emacs commands.

Command Description Command Description
Ctrl+x+Ctrl+c Exit Emacs Ctrl+s Search string
Ctrl+x+u undo changes Ctrl+g Cancel command
Ctrl+h Get help v Enter visual mode
Ctrl+x+s Save Ctrl+v Enter Visual Block mode
Ctrl+a Move to beginning of line Esc+< Move to beginning of file
Ctrl+e Move to end of line Esc+> Move to end of file

Key Points

  • Nano, Vim and Emacs are the most common CLI Linux text editors

  • Gedit is also a common text editor with a graphical interface

  • Command line text editors might have a steep learning curve but are powerful

  • If you plan to spend a lot of time working on text files on Linux, it is worth mastering a CLI text editor.