Mastering GNU nano: The Complete Guide, Shortcuts, and Configuration

GNU nano is a lightweight, user-friendly terminal text editor that comes pre-installed on most Linux distributions and macOS. Whether you’re editing config files, writing scripts, or taking quick notes, nano is a reliable and efficient tool that’s easy to learn yet surprisingly powerful.


๐Ÿค” 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.txt

To open multiple files:

nano file1.txt file2.py file3.conf

Navigating to Specific Lines

Go directly to line 42:

nano +42 filename.txt

Go to line 10, column 5:

nano +10,5 filename.txt

Basic 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

ShortcutAction
Ctrl+OSave current file
Ctrl+XExit nano
Ctrl+RInsert another file
Ctrl+SSave file (modern bindings)

Text Editing

ShortcutAction
Ctrl+KCut current line
Ctrl+UPaste cut line
Alt+6Copy current line
Alt+UUndo
Alt+ERedo
Ctrl+JJustify paragraph

Navigation

ShortcutAction
Ctrl+WSearch
Alt+WFind next
Ctrl+_ (Ctrl+Shift+-)Go to line
Ctrl+CShow cursor position

Selection & Marking

ShortcutAction
Alt+ASet/unset mark
Ctrl+6Set/unset mark (alternative)
Alt+6Copy marked text
Ctrl+KCut marked text

Help

ShortcutAction
Ctrl+GShow help
Ctrl+XExit help

โš™๏ธ Advanced Features & Configuration

Mouse Support

Enable mouse to click for cursor positioning and text selection:

nano -m filename.txt

Or toggle with Alt+M while editing.

Line Numbers

Toggle line numbers with Alt+N or start with:

nano -l filename.txt

Syntax Highlighting

Enable syntax highlighting for a specific language:

nano -Y python script.py

Available syntaxes include: python, html, javascript, c, bash, xml, and many more.

Multiple Buffers

Work with multiple files simultaneously:

nano -F file1.txt file2.txt

Switch between buffers with Alt+< and Alt+>.

Search & Replace with Regex

Use regular expressions in search:

  1. Press Ctrl+W to search
  2. Press Alt+R to toggle regex mode
  3. 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

# ~/.nanorc
set linenumbers
set mouse
set autoindent
set tabsize 4
set softwrap
set constantshow
set smarthome

Syntax Highlighting Configuration

# Custom syntax highlighting
syntax "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 colors
set titlecolor bold,white,blue
set statuscolor bold,white,green
set numbercolor yellow

๐Ÿ“‹ Command-Line Options Cheat Sheet

OptionDescriptionExample
-l, –linenumbersShow line numbersnano -l file.txt
-m, –mouseEnable mouse supportnano -m file.txt
-i, –autoindentAuto-indent new linesnano -i file.txt
-A, –smarthomeSmart Home key behaviornano -A file.txt
-E, –tabstospacesConvert tabs to spacesnano -E file.txt
-T, –tabsize=8Set tab widthnano -T 4 file.txt
-Y, –syntax=nameSet syntax highlightingnano -Y python file.py
-B, –backupCreate backup filesnano -B file.txt
-C dir, –backupdir=dirSet backup directorynano -C ~/backups file.txt
-v, –viewRead-only modenano -v file.txt
-R, –restrictedRestricted mode (safer)nano -R file.txt
-H, –historylogSave search historynano -H file.txt
-P, –positionlogRemember cursor positionnano -P file.txt
-S, –softwrapSoft wrap long linesnano -S file.txt
-b, –breaklonglinesHard wrap long linesnano -b file.txt
-x, –nohelpHide help lines (expert)nano -x file.txt
-0, –zeroHide all UI elementsnano -0 file.txt
–minibarUse minimal status barnano --minibar file.txt
–modernbindingsModern key bindingsnano --modernbindings file.txt

Useful Option Combinations

For Python development:

nano -li -T 4 -E -Y python script.py

For minimal distraction:

nano -0x --minibar document.txt

For 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 compile
tar -xf nano-x.y.tar.gz
cd nano-x.y
./configure
make
sudo 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

  1. Always check the bottom barโ€”it shows available shortcuts for your current context
  2. Use Ctrl+G for helpโ€”it’s comprehensive and context-sensitive
  3. Create a ~/.nanorc file to customize nano to your workflow
  4. Try --modernbindings if you’re used to GUI editor shortcuts
  5. Remember Alt+U for undoโ€”it’s a lifesaver!

Further Resources

Pro Tip: Nano automatically saves your position in files when you use -P or set 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.

Leave a Reply

Your email address will not be published. Required fields are marked *