While advanced developers often debate the merits of Vim versus Emacs, the reality is that not everyone wants to spend weeks learning a complex text editor just to change a single line in a configuration file.
If you are new to Linux, or simply prefer an editor that “just works” without confusing modes, Nano is your best friend.
Nano is pre-installed on almost every Linux distribution. It is incredibly lightweight, user-friendly, and displays all of its keyboard shortcuts right on the screen. In this complete beginner’s guide, we will walk you through exactly how to master Nano.
Why Choose Nano?
Nano is designed for simplicity. You should use Nano if:
- You are relatively new to Linux and the terminal.
- You only need to occasionally edit configuration files (like
/etc/nginx/nginx.confor/etc/fstab). - You hate the idea of “Modal Editing” (like Vim) where typing a letter might delete a line instead of appearing on the screen.
- You want an editor that shows you exactly how to save and quit at all times.
Starting Nano
Opening a file in Nano is as simple as typing nano followed by the filename in your terminal:
nano my-config.txt
If the file does not exist, Nano will create a new, empty buffer for you. If you need to edit a system file that requires administrator privileges, just use sudo:
sudo nano /etc/ssh/sshd_config
Understanding the Interface
When Nano opens, you’ll see your text in the middle of the screen. At the very bottom, you will see two rows of shortcuts:
^G Get Help ^O WriteOut ^W Where Is ^K Cut Text
^X Exit ^J Justify ^C Cur Pos ^U Uncut Text
The caret symbol (^) represents the Ctrl key on your keyboard. So, ^X means press Ctrl + X.
Basic File Operations
Unlike Vim, Nano acts like a normal text editor. When you open it, just start typing. Your text will appear immediately.
Saving a File (WriteOut)
To save your changes, look at the bottom menu for ^O WriteOut.
- Press Ctrl + O.
- Nano will ask you what filename you want to save it as (it defaults to the current filename).
- Press Enter to confirm.
Exiting Nano
To close the editor, use ^X Exit.
- Press Ctrl + X.
- If you have unsaved changes, Nano will ask:
Save modified buffer? (Y/N/^C) - Press Y to save, or N to discard your changes.
- Press Enter to confirm.
Navigating the File
You can use the arrow keys on your keyboard to move around perfectly fine in Nano. However, for faster navigation, you can use these shortcuts:
- Ctrl + A: Jump to the beginning of the line.
- Ctrl + E: Jump to the end of the line.
- Ctrl + Y: Page Up (Scroll up one page).
- Ctrl + V: Page Down (Scroll down one page).
- Ctrl + _ (Ctrl + Underscore): Go to a specific line number. Nano will ask for the number, type it, and hit Enter.
Copying, Cutting, and Pasting
Nano uses slightly different terminology for copy/paste, referring to it as “Cut” and “Uncut”.
- Ctrl + K (Cut Text): This will cut (delete) the entire current line and save it to Nano’s memory buffer.
- Ctrl + U (Uncut Text): This will paste whatever you just cut at your current cursor position.
Pro-tip: If you want to copy a line without deleting it, simply press Ctrl + K to cut it, and immediately press Ctrl + U to paste it right back. Then move your cursor to the new destination and press Ctrl + U again!
Searching and Replacing Text
Searching (Where Is)
To find a specific word in your document:
- Press Ctrl + W (Where Is).
- Type the word you are looking for and press Enter.
- To find the next occurrence of that same word, press Alt + W.
Replacing Text
To find and replace a word:
- Press Ctrl + \ (Ctrl + Backslash).
- Type the word you want to find and press Enter.
- Type the word you want to replace it with and press Enter.
- Nano will highlight the first match and ask if you want to replace it. Press Y (Yes), N (No), or A (All occurrences).
Unlimited Undo and Redo
Made a terrible mistake? Don’t panic. Modern versions of Nano (2.3.0 and newer) support unlimited undo!
- Alt + U: Undo the last action.
- Alt + E: Redo the action.
Customizing Nano (.nanorc)
Did you know Nano supports syntax highlighting and mouse clicks? You can permanently configure Nano to your liking by creating a .nanorc file in your home directory.
nano ~/.nanorc
Paste the following helpful configurations into the file and save it (Ctrl+O, Enter, Ctrl+X):
# Show line numbers on the left side
set linenumbers
# Enable mouse support (clicking to move the cursor)
set mouse
# Smooth scrolling instead of jumping half a page at a time
set smooth
# Auto-indent new lines to match the previous line
set autoindent
# Show your current cursor position at the bottom of the screen
set constantshow
Conclusion
Nano may not have the extreme efficiency of Vim or the massive plugin ecosystem of VS Code, but its simplicity is its greatest strength. It is the perfect tool for making quick edits to configuration files or writing simple scripts without having to memorize a cheat sheet of complex commands.
If you ever decide you want to learn something a bit more advanced, you can always check out our Complete Guide to Mastering Vim later. But for now, enjoy the straightforward simplicity of Nano!
Discussion
Loading comments...