Text editors: nano

nano is an easy-to-use command line text editor for Unix and Linux operating systems. It includes all the basic functionality you’d expect from a regular text editor, like syntax highlighting, multiple buffers, search and replace with regular expression support, spellchecking, UTF-8 encoding, and more.

We will go through the basics of using nano, including: how to create and open a file, edit a file, save a file, search and replace text, and cut and paste text.

What is a text editor?

We sometimes call programs like Microsoft Word or LibreOffice Writer “text editors”, but we need to be a bit more careful when it comes to programming.

By default, Microsoft Word uses .docx files to store not only text, but also formatting information about fonts, headings, and so on. This extra information isn’t stored as characters, and doesn’t mean anything to tools like head: they expect input files to contain nothing but the letters, digits and punctuation on a standard computer keyboard. When editing programs, therefore, you must either use a plain text editor, or be careful to save files as plain text.

1 Starting nano

Nano text editor is pre-installed on macOS and most Linux distributions. To check if it is installed on your system, type:

nano --version

To open an existing file or to create a new file, type nano followed by the file name:

nano filename

This opens a new editor window, and you can start editing the file. nano is a “modeless” editor. All keys, with the exception of Control and Meta (Alt or Option) key sequences, will enter text into the file being edited.

At the bottom of the window, there is a list of the most basic command shortcuts to use with the nano editor. All commands are prefixed with either ^ or M characters. The caret symbol (^) represents the Ctrl key. For example, the ^J command means to press the Ctrl and J keys at the same time. The letter M represents the Alt or Option key. You can get a list of all commands by typing Ctrl + g.

To move the cursor to a specific line and character number, use the command Ctrl + _. The menu at the bottom of the screen will change. Enter the number(s) in the “Enter line number, column number:” field and hit Enter.

To open a file, you must have read permissions to the file. If you want to open a file with the cursor on a specific line and character, use the following syntax:

nano +line_number,character_number filename

If you omit the character_number, the cursor will be positioned on the first character.

2 Search and replace

To search for a text, press Ctrl + w, type in the search term, and press Enter. The cursor will move to the first match. To move to the next match, press Alt+w.

If you want to search and replace, press Ctrl + \. Enter the search term and the text to be replaced with. The editor will move to the first match and ask you whether to replace it. After hitting Y or N, it will move to the next match. Pressing A will replace all matches.

3 Copy, cut, and paste

To select text, move the cursor to the beginning of the text and press Alt + a. This will set a selection mark. Move the cursor to the end of the text you want to select using the arrow keys. The selected text will be highlighted. If you wish to cancel the selection, press Ctrl + 6.

Copy the selected text to the clipboard using the Alt + 6 command. Ctrl + k will cut the selected text.

If you want to cut whole lines, move the cursor to the line and press Ctrl + k. You can cut multiple lines by hitting Ctrl + k several times.

To paste the text, move the cursor to where you want to put the text and press Ctrl + u.

4 Save and exit

To save the changes you’ve made to the file, select Ctrl + o. If the file doesn’t exist, it will be created once you save it.

To exit nano, select Ctrl + x. If there are unsaved changes, you’ll be asked whether you want to save the changes.

To save the file, you must have write permissions to the file. If you are creating a new file, you need to have write permission to the directory where the file is created.