Master the Linux ‘nano’ Command: A Comprehensive Guide

Peter Hou
5 min readMay 21, 2023

--

This article introduced how to use the nano command in Linux, including its history, why and when to use it, how to use it, common parameters and other supported parameters, the most common use cases, unique techniques, and issues to pay attention to during use. We first learned about the origin of nano, which was created to simplify Linux text editing and is therefore very suitable for beginners. We then went into detail about how to use the nano command, as well as common parameters and options that can help you better use nano to edit text. Additionally, we presented some advanced techniques for nano, including how to use search, cut and paste, undo and redo, and other functions, so you can work more efficiently when editing text. Finally, we emphasized some issues to note when using nano, such as the fact that nano does not save files automatically by default, so you must remember to save your changes before exiting the editor. All in all, by mastering the knowledge and techniques presented in this article, you can use nano more effectively for your text editing tasks.

Instructions

This article will help beginners grasp the ‘nano’ command, one of the most straightforward text editors in the Linux command line environment. We will cover its history, common uses, syntax, parameters, and some advanced uses.

History

‘Nano’ is a text editor for Unix-like computing systems or operating environments using a command-line interface. It emulates the Pico text editor, part of the Pine email client, and also provides additional functionality. ‘Nano’ was first created in 1999, with the intention of replacing Pico, which lacked an open-source license.

When and why to use it

‘Nano’ was created to be an easy-to-use text editor, unlike other editors like ‘vim’ or ‘emacs’, which are powerful but can be overwhelming for beginners. You would use ‘nano’ when you want to edit text files directly from your command line, whether you’re adjusting configuration files, writing programming scripts, or creating text files.

How to use it

The basic usage of the ‘nano’ command is very straightforward. To open a file, simply type nano followed by the filename. If the file does not exist, 'nano' will create it.

$ nano myfile.txt

The commonly used parameters

  • -B (Backup): This makes a backup of the existing file before it is edited. The backup file has the same name as the original file with a tilde (~) appended to it.
$ nano -B myfile.txt
  • -I (autoIndent): This allows for automatic indenting of new lines to the position of the previous lines.
$ nano -I myfile.txt

Other supported parameters

  • -B (or --backup): Automatically back up the original file when saving.
  • -C (or --backupdir): Specify the directory of backup files.
  • -D (or --boldtext): Use bold rather than reverse to display selected text.
  • -E (or --tabstospaces): Convert input tabs to spaces.
  • -F (or --multibuffer): Enable multi-buffer support.
  • -G (or --locking): Use vim-style file locking when reading files.
  • -H (or --historylog): Record search/replace string history.
  • -I (or --ignorercfiles): Do not view .nanorc files and global nanorc files.
  • -K (or --rebindkeypad): Solve the numeric keypad issue (e.g., '1', '2', '3', etc., correspond to 'End', 'Down', 'Page Down', etc.).
  • -N (or --noconvert): Do not convert DOS/Mac to Unix newline.
  • -O (or --morespace): Provide more space at the top of the editing window.
  • -P (or --positionlog): Record the last view position of the file.
  • -Q (or --quotestr): Set the value of the quote string.
  • -R (or --restricted): Enable restricted mode.
  • -S (or --smooth): Use smooth scrolling.
  • -T (or --quickblank): Quickly erase the cursor.
  • -U (or --quick): Enable quick mode.
  • -V (or --version): Display version information.
  • -W (or --wordbounds): More accurately find the beginning and end of words.
  • -Y (or --syntax): Set syntax highlighting for a specified language.
  • -c (or --constantshow): Continuously show the position of the cursor in the status line.
  • -d (or --rebinddelete): Solve the Backspace and Delete key issue.
  • -i (or --autoindent): Auto-indent new lines.
  • -k (or --cut): Toggle cut text to clipboard.
  • -m (or --mouse): Enable mouse support.
  • -n (or --noread): Do not read the file (write only).
  • -o (or --operatingdir): Set the working directory.
  • -p (or --preserve): Preserve the x and y position of the cursor.
  • -r (or --fill): Set the text width for auto line wrapping.
  • -s (or --speller): Look for spelling errors in the text.
  • -t (or --tempfile): Automatically save changes upon exit.
  • -u (or --unix): Save files in Unix format.
  • -v (or --view): View (read-only) mode.
  • -x (or --nohelp): Do not display help text.
  • -y (or --afterends): Allow cursor after line ends.
  • -z (or --suspend): Enable suspend capability.
  • -A (or --smarthome): Enable smart "Home" key functionality.
  • -J (or --justifytrim): Trim trailing spaces when justifying paragraphs.

Common Use Cases

Nano, due to its simple and intuitive user interface, is widely used for basic text editing needs in Linux systems. Here are some of its common uses:

  1. Editing system configuration files: For example, using sudo nano /etc/ssh/sshd_config to edit the SSH configuration file.
  2. Writing simple scripts or programs: For example, using nano script.sh to create a new bash script.
  3. For Git commit messages: When performing a commit operation in Git, if no commit message is specified on the command line, nano is used as the default text editor.

Tips

For advanced use of Nano, here are some tips:

  1. Using Alt+$ can wrap text lines to window width, which is very useful for editing long line text that exceeds the window width.
  2. Ctrl+W can be used to find strings in the text.
  3. Ctrl+K can cut the entire line of text, then Ctrl+U can paste the cut text back.
  4. Alt+U and Alt+E can be used to undo and redo operations.

Points to note

When using nano, note the following:

  1. By default, nano does not automatically save files. Therefore, remember to save your changes with Ctrl+O before exiting the editor.
  2. For large files, nano may run slower. In this case, consider using a more powerful editor like vim or emacs.

Conclusion

Whether you are a system administrator or an everyday user, nano is a powerful and easy-to-use text editor. By mastering its basic use and advanced techniques, you can handle text editing tasks more effectively.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.