How to Get Started with Nano
If you want to be a Linux engineer, you need to get comfortable with the command line. A lot of work is done in Bash, including editing configuration files. Like many other things in the world of Linux, there are a variety of ways to edit configuration files. Linux has a bunch of command-line text editors for attacking this job like Nano, Emacs, and Vim. In this post, we are going to focus on how to use Nano.
Why Do Some SysAdmins Prefer Nano?
There's a debate in the Linux world over which command-line text editor is the best one to use. That argument is about as old as Linux itself and is as hotly debated as Windows vs. Apple or Nintendo vs. Sega. Though many people prefer Emacs or Vim, I prefer Nano.
There's an argument to be made for using Emacs and Vim. Both command-line text editors are powerful and feature-packed. If you want to do something specific with either of those editors, you can probably do it.
For instance, Vim has three modes that it can work in:
Normal: the default mode for Vim, used for navigation and simple editing
Insert: explicitly for inserting and modifying text
Command-line: for operations like saving, exiting, etc…
In order to perform functions, like saving, you need to switch which mode you're using in Vim. Likewise, performing operations is done using pseudo-command-line commands instead of simple keyboard combinations.
There's an old Vim joke. How do you generate a random string of text? Tell someone to exit Vim.
Someone made this joke for a reason. Though Vim can be powerful, it is also overly complex. You must understand what each mode in Vim does, how it can be used, how to switch between them, and how to identify which mode you are currently in. Without a GUI interface that can display proper user prompts, this can be tedious.
That's why I prefer Nano. Nano is a no-frills, simple, beginner-friendly Linux command-line text editor. It has one job (editing text) and it does it well. Nano is also the default text editor for Linux distributions like Ubuntu, Debian, and Gentoo.
So, let's explain how to use Nano.
How do You Open a File in Nano?
Opening a file in Nano is easy. Use the Nano command followed by the name of the file to open:
If that file isn't in the same directory that you are currently in within Bash, include the path to the file in front of the file name:
If you are editing a file that needs elevated privileges to save changes to it, make sure to elevate privileges in the command line before using Nano. This can be done with either the Sudo or SU command depending on the distribution of Linux that you are using:
Sudo nano /home/user/documents/helloWorld.txt
If you want to create a new file, use the same command. In this case, though, the filename will be the name of the new file.
How to Navigate a File in Nano
You will not have mouse access in the command-line like you will in a GUI environment. That means you are stuck using the keyboard. Thankfully, Nano is easy to navigate with. Use the arrow keys on your keyboard to move the cursor around the document.
If you need to jump large spaces at a time, use the 'Page Up' or 'Page Down' buttons on your keyboard. That will move your cursor an entire page of screen text. If you know the exact line in a document you want to jump to, use the 'Control+_" (underscore) keyboard combination. You'll then be prompted for the line number to move to.
How to Copy and Paste in Nano
Because you can't highlight text with a mouse cursor, copying and pasting in a command-line editor works a bit differently. You'll use another set of keyboard combinations for both of these operations.
To highlight text, press Control+A on your keyboard first. That will set a marker in the text. Then use the arrow keys to highlight your selection of text. Once you have the text highlighted, press Alt+6 on the keyboard to copy that text. You can also use the Control+K keyboard combination to cut that text as well. If you want to cancel highlighting text, press Control+6 on your keyboard again.
Once you cut or copied text, press Control+U on your keyboard to paste it.
How to Search a Document in Nano
Searching a document in Nano is easy. Use the Control+W keyboard combination on your keyboard. After pressing that keyboard combination, a prompt will appear at the bottom of the Nano screen. Input your search string in that prompt and press enter.
If you want to search for the next instance of that string, press the same Control+W keyboard combination followed by Enter again. You can keep doing this until you find the instance of that search string that you need in that file.
Get comfortable with the Control+W keyboard combination. It is one of the most useful commands in Nano for editing configuration files. Though short files aren't hard to find text in, you'll wish you had remembered this command when you're in the middle of editing an Apache or PHP configuration file.
There's a good chance that you will be following some sort of documentation when you edit configuration files and that documentation will have specific keywords to look for. This Control+W keyboard combination is a lifesaver for editing these long configuration files.
How to Undo or Redo Edits in Nano
Press Alt+U on your keyboard to undo the last bit of text you changed in Nano. Press Alt+E on your keyboard to redo changes as well.
How to Save Files in Nano
There are two ways to save files in Nano. There's an option to save and keep working as well as an option to save and close the file you are working in. Nano does not save changes automatically as Microsoft Word would. So, it's important to make sure you save every once in a while.
Use the Control+O keyboard combination to save your file without exiting Nano. If you are done working on a file in Nano, press Control+X on your keyboard instead. That will prompt you to save your file and then exit Nano.
What Else Should I Know About Nano?
If you forget any of these commands or need to see if Nano has a command for something else, use the 'Man' command from the command line to read the Nano manual. You'll use the following command for that:
If you are already working in Nano, use the Control+G keyboard combination to access the manual for Nano within Nano itself.
Nano also has a helpful hint area at the bottom of its screen. This area will display common keyboard combinations for various tasks. This area also dynamically changes depending on what you are doing, too.
Why Should You Use Nano?
As you can see Nano is very straightforward and easy to use. This is the reason I prefer Nano. Others might prefer the power of Emacs or Vim, but personally, if I am working on developing software, I prefer to use a tool better suited for that job like Visual Studio Code or another IDE.
The brilliance of Nano is its simplicity. It makes editing configuration files and managing a Linux install quick and easy. When I want to change configuration files, I want to make those changes and restart an application without wasting time figuring out how to save and close those files.
delivered to your inbox.