For many of us, most of our programming happens in a dedicated code editor (e.g. VSCode). When we’re doing system administrative work via command line, however, we often find ourselves needing to create/edit text files directly in the terminal. This can be done with command line editors such as Vim and Emacs, but they have a steep learning curve. As an alternative, Nano is a simple to learn editor that will cover all your basic needs for simple sysadmin-related text editing.
Installation
Nano is available by default on Mac and many Linux operating systems. If you’re using Git Bash on Windows as your command line program (suggested), you’ll also find it installed there.
If you invoke the nano
command and are told it’s not found, search online for “install nano [your operating system here]” for further instructions.
Creating/Opening files
To get started, change into a directory where you can create a practice text file. In my example, I’ll change to my computer’s Desktop.
> cd ~/Desktop
Once there, use Nano to create a new file called example.txt using the following command:
> nano -m example.txt
As part of this command, we include the -m
(mouse) flag which will enable mouse support while using Nano. Without this, you can only navigate through the file using your arrow keys or special commands, which can be tedious.
Once Nano opens your new file, it should look like this:
To practice working in Nano, paste in the following text then edit it to fill in your own name next to Created by:.
Hello!
This is a practice file demonstrating how to use Nano.
Created by: <your name here>
Some benefits of Nano:
+ Simple to learn
+ Available on many systems by default
+ Includes a lot of the functionality you’d expect from a regular text editor:
+ Syntax highlighting
+ Search/replace
+ Spell check
+ etc.
Saving changes
Nano uses keyboard shortcuts that all start with the ctrl key. In documentation, the shortcuts are always prefixed with a ^
character.
For example, if you see ^X
it means hit ctrl
+ X
; this command allows you to save and exit the file you’re currently editing.
After hitting ^X
it will ask you if you want to save. Type the letter y
for yes and then hit Enter.
If you want to just save changes without exiting, use ctrl
+ O
.
To re-open your file, use the same command you used to create the file:
> nano -m example.txt
Other commands
For a reference list of all available commands use ^G
(Help). You can also refer to Nano’s shortcut documentation.
Here are some commands I find I use most frequently:
-
^K
Cut/delete a line -
^A
Go to the start of a line -
^E
Go to the end of a line -
^W
Search
Edit as admin
Most of the time we use Nano to work with config related files, and these files often require administrative privileges to edit. You can examine the permissions of a file to find out if it does need admin privileges, or you’ll simply find out when you go to edit a file as a regular user and you’re told you don’t have access.
On Mac and Linux systems, you can prefix your nano
command with sudo to run the command as an administrator. This will prompt for a password to confirm you have admin access to the file.
> sudo nano example.txt
On Windows, if you right click and choose “Run as administrator” when opening your command line program, any commands you run should be run as an admin.
Conclusion
That’s the basics of working with Nano. You could spend more time learning about every feature available, but it’s not really necessary. We typically only use Nano for quick edits to config files so we just need to know the essentials. Any “heavy lifting” code editing is probably happening in your favorite dedicated code editor.