How to Edit Files in Linux with Nano Command Line Editor

Edit files in command console is a basic skill for admin to work on a Linux server or troubleshoot a broken Linux desktop.

Here I’m going to show you how to use nano, the beginner friendly text editor that is available out-of-the-box in most Linux.

GNU nano is a free open-source text editor started in 1999. It was called TIP, then renamed to nano in 2000 to avoid naming conflict with Unix utility TIP.

Nano is easy to use compare to Vim and Emacs, while it also includes powerful features. They include syntax highlighting, line numbers, regular expression search and replace, line-by-line scrolling, multiple buffers, indenting groups of lines, re-bindable key support, and undo/redo.

Edit file with nano

To edit a file using nano, simply run command:

nano your_file_name

For files not in current directory, you need to also input the PATH. For example, edit ssh server config file:

sudo nano /etc/ssh/sshd_config
Tips: 
1. For files not belong to you, add sudo at beginning to use root user permission.
2. If the file you edit does NOT exist, it will create it with empty content.
Edit /etc/ssh/sshd_config file via nano

Basic nano actions

Once file opens in command console, use up, down, left, right arrow keys to navigate around.  Just type to insert letters, and use del key to delete current character, or BackSpace to remove last one.

For long line, use Ctrl+E to go to the end of line, and Ctrl+A to go back to the beginning.

For large file, use Ctrl+V to move page down, and Ctrl+Y to move page up.

To search something, press Ctrl+W, then type your keyword, and finally hit Enter will highlight the first match. For next or preview matches, just hit Alt+W or Alt+Q to navigate.

use search in nano

Another common action for editing files is search & replace, simply press Ctrl + \ on keyboard, then type the keyword you want to search for, then hit Enter. Next, it will prompt to type the keyword to replace with.

When done typing new keyword, hit Enter, and then either type y (or n to skip) to replace one by one. Or, type a to replace all at once. Of course, you may press Ctrl+C to cancel search & replace.

Save file in Nano text editor

After making changes, either press Ctrl+S to save file. Or, press Ctrl+O to do “Save as” action, which will ask you to either hit Enter to save changes (write to file), or Ctrl+C to cancel.

Finally, press Ctrl+X to exit nano text editor.

If you pressed Ctrl+X without saving (if there’s any changes), it will ask you to either type y to save & hit Enter to exit, or n to exit without saving changes.

For more about nano text editor, see the official document.

Hi, I'm Merilyn Ne, a computer geek working on Ubuntu Linux for many years and would like to write useful tips for beginners. Forgive me for language mistakes. I'm not a native speaker of English.