Mastering GNU nano: The Complete Guide, Shortcuts, and Configuration
๐ Table of Contents
๐ค What is GNU nano?
GNU nano is a free, open-source text editor designed for the command line. It was originally created as a replacement for the non-free Pico editor and has since evolved into a feature-rich editor with:
- Syntax highlighting for many programming languages
- Undo/redo functionality
- Interactive search and replace
- Multiple buffer support
- Mouse support in compatible terminals
- Spell checking
- Line numbering
- Auto-indentation
- Unicode support
Unlike modal editors like Vim, nano is modelessโyou can start typing immediately without switching between modes.
๐ Basic Usage
Opening Files
To open a file in nano:
nano filename.txtTo open multiple files:
nano file1.txt file2.py file3.confNavigating to Specific Lines
Go directly to line 42:
nano +42 filename.txtGo to line 10, column 5:
nano +10,5 filename.txtBasic Editing
- Type normallyโnano is modeless
- Use arrow keys to move around
- Backspace and Delete work as expected
- Ctrl+G brings up the help screen
๐ฏ Essential Keyboard Shortcuts
Here are the most important shortcuts you need to know:
File Operations
| Shortcut | Action |
|---|---|
| Ctrl+O | Save current file |
| Ctrl+X | Exit nano |
| Ctrl+R | Insert another file |
| Ctrl+S | Save file (modern bindings) |
Text Editing
| Shortcut | Action |
|---|---|
| Ctrl+K | Cut current line |
| Ctrl+U | Paste cut line |
| Alt+6 | Copy current line |
| Alt+U | Undo |
| Alt+E | Redo |
| Ctrl+J | Justify paragraph |
Navigation
| Shortcut | Action |
|---|---|
| Ctrl+W | Search |
| Alt+W | Find next |
| Ctrl+_ (Ctrl+Shift+-) | Go to line |
| Ctrl+C | Show cursor position |
Selection & Marking
| Shortcut | Action |
|---|---|
| Alt+A | Set/unset mark |
| Ctrl+6 | Set/unset mark (alternative) |
| Alt+6 | Copy marked text |
| Ctrl+K | Cut marked text |
Help
| Shortcut | Action |
|---|---|
| Ctrl+G | Show help |
| Ctrl+X | Exit help |
โ๏ธ Advanced Features & Configuration
Mouse Support
Enable mouse to click for cursor positioning and text selection:
nano -m filename.txtOr toggle with Alt+M while editing.
Line Numbers
Toggle line numbers with Alt+N or start with:
nano -l filename.txtSyntax Highlighting
Enable syntax highlighting for a specific language:
nano -Y python script.pyAvailable syntaxes include: python, html, javascript, c, bash, xml, and many more.
Multiple Buffers
Work with multiple files simultaneously:
nano -F file1.txt file2.txtSwitch between buffers with Alt+< and Alt+>.
Search & Replace with Regex
Use regular expressions in search:
- Press Ctrl+W to search
- Press Alt+R to toggle regex mode
- Use patterns like
^#(lines starting with #) or\s+$(trailing whitespace)
Spell Checking
Check spelling with Ctrl+T, then Ctrl+S.
๐จ Customizing with Nanorc
Create a configuration file at ~/.nanorc to customize nano permanently.
Basic Settings Example
# ~/.nanorcset linenumbersset mouseset autoindentset tabsize 4set softwrapset constantshowset smarthome
Syntax Highlighting Configuration
# Custom syntax highlightingsyntax "python" "\.py$"color green "^def.*$"color blue "^class.*$"color red "\".*\""color cyan "#.*$"
Key Rebinding
# Rebind Ctrl+S to save (like modern editors)bind ^S savefile main
Color Customization
# Custom colorsset titlecolor bold,white,blueset statuscolor bold,white,greenset numbercolor yellow
๐ Command-Line Options Cheat Sheet
| Option | Description | Example |
|---|---|---|
| -l, –linenumbers | Show line numbers | nano -l file.txt |
| -m, –mouse | Enable mouse support | nano -m file.txt |
| -i, –autoindent | Auto-indent new lines | nano -i file.txt |
| -A, –smarthome | Smart Home key behavior | nano -A file.txt |
| -E, –tabstospaces | Convert tabs to spaces | nano -E file.txt |
| -T, –tabsize=8 | Set tab width | nano -T 4 file.txt |
| -Y, –syntax=name | Set syntax highlighting | nano -Y python file.py |
| -B, –backup | Create backup files | nano -B file.txt |
| -C dir, –backupdir=dir | Set backup directory | nano -C ~/backups file.txt |
| -v, –view | Read-only mode | nano -v file.txt |
| -R, –restricted | Restricted mode (safer) | nano -R file.txt |
| -H, –historylog | Save search history | nano -H file.txt |
| -P, –positionlog | Remember cursor position | nano -P file.txt |
| -S, –softwrap | Soft wrap long lines | nano -S file.txt |
| -b, –breaklonglines | Hard wrap long lines | nano -b file.txt |
| -x, –nohelp | Hide help lines (expert) | nano -x file.txt |
| -0, –zero | Hide all UI elements | nano -0 file.txt |
| –minibar | Use minimal status bar | nano --minibar file.txt |
| –modernbindings | Modern key bindings | nano --modernbindings file.txt |
Useful Option Combinations
For Python development:
nano -li -T 4 -E -Y python script.pyFor minimal distraction:
nano -0x --minibar document.txtFor safe editing of system files:
nano -BRv /etc/config/file.conf๐ง Building from Source
If you need the latest version or custom features, build nano from source:
# Extract and compiletar -xf nano-x.y.tar.gzcd nano-x.y./configuremakesudo make install
Common Build Options
# Enable UTF-8 support./configure --enable-utf8# Disable features for minimal build./configure --enable-tiny# Enable debugging./configure --enable-debug# Disable mouse support./configure --disable-mouse# Custom configuration file name./configure --enable-altrcname=.nanoconfig
โ Conclusion
GNU nano is more than just a simple text editorโit’s a powerful tool that balances ease of use with advanced features. Whether you’re a beginner who needs to edit a configuration file or an experienced developer writing code in the terminal, nano has the features you need.
Quick Tips Summary
- Always check the bottom barโit shows available shortcuts for your current context
- Use
Ctrl+Gfor helpโit’s comprehensive and context-sensitive - Create a
~/.nanorcfile to customize nano to your workflow - Try
--modernbindingsif you’re used to GUI editor shortcuts - Remember
Alt+Ufor undoโit’s a lifesaver!
Further Resources
- Official Website: nano-editor.org
- Manual:
man nanoorinfo nano - Help within nano:
Ctrl+G - Bug Reports: GNU Nano Bug Tracker
Pro Tip: Nano automatically saves your position in files when you use
-Porset positionlog. This means you can close and reopen a file and continue right where you left offโperfect for working on large documents or code files!
Happy editing! ๐๏ธ
This guide covers GNU nano 8.7. Check your version with nano --version and consult the manual for version-specific features.