When writing scripts, don’t you use a text editor? Each text editor has its pros and cons. You can use text editors from the Linux terminal or command line. They are fast, flexible, and secure. There are a bunch of command-line text editors in Linux, nano is one of them. Emacs and Vims are other very popular command-line text editors, but they have a steep learning curve. Linux nano is beginner-friendly and common in every Unix like OS. The best part of it is that this is the one you’ll most likely be using when remote controlling servers. In this blog, you’ll learn everything you need to know about the nano text editor in Linux. So open your terminal using Ctrl+Alt+T and get started!
Key points to remember before you start
- nano text editor uses commands to perform different tasks. They are also referred to as shortcuts
- nano commands never include the use of SHIFT key
- The Control key is represented by the carat (^) symbol. For example, ^X means Control+X, where you don’t use the SHIFT key to write X
- The Meta key or Alt key is represented by M. For example, M-A represents the Alt+A command.
Opening or Creating a file in Linux nano editor
The structure of nano command is:
$ nano path/to/file
If the file already exists, then nano will open the file. Otherwise, it’ll create a blank file with the given name. If the path is the same as where your terminal currently at, then just use the filename as a path. Let’s create and open a file named script.txt in the directory ~/MLwiki. Note that, after running the command, your terminal will get a new and different screen.
$ nano MLwiki/script.txt
This is how it should look like:
Commands or Shortcuts of Linux nano Editor
Frequently used commands
The bottom 2 lines of the nano text editor show some frequently used commands. The number of shown commands depends on the screen size. That means, if you work in full-screen mode, you’ll see the most amount of commands. Let’s see what they are used for.
^G – Get Help
In the working screen, you see the most frequently used commands. But there are lots more. To see them all, use this command, which is Control+G. You can see, there are around 80 commands listed with a brief description. They are self-explanatory, but I’ll describe the ones you’ll use regularly.
To exit this screen and go back to the editing screen, press Q. As you can see, the F1 key does the same job as ^G.
Navigating Large File
When navigating inside a large text file, the Right, Left, Up, Down arrow keys are used. But there are some other commands that can increase your writing speed.
- Move Forward one character – ^F. Same as using right arrow key
- Move Backward one character – ^B. Same as using left arrow key
- Move to the Previous line – ^P. Same as using up arrow key
- Move to the Next line – ^N. Same as using down arrow key
- Move to the next word – ^right arrow
- Move to the previous word – ^left arrow
- Go at the beginning of the line – home
- Go at the end of a line – end
- Move to the previous block – ^up arrow
- Move to the next block – ^down arrow
Specially for coding:
- Indent current line – Tab
- Unindent current line – Shift+Tab or Backspace
- Line number enable/disable – M-#
- Whitespace display enable/disable – M-P
- Syntax highlighting enable/disable – M-Y
- Auto indent enable/disable – M-I
- Mouse support enable/disable – M-M
Almost all of these commands have their alternatives. You’ll find them in ^G command. I only quoted the ones that might be easier for you to follow.
SURPRIZE!
The first list of commands above is universal, i.e. they work even outside the nano editor! Open a Word file, or any text file, and use the commands. Isn’t it surprising? Doesn’t it increase your work efficiency? Even when I’m writing this blog, I’m using those commands. Now your turn, when reading this blog, try using these keys from your keyboard: home, end, pg up, pg dn. Aren’t they help a lot! Have you ever used them before? I don’t think you need your mouse anymore when writing something. Soon, I’ll write about all the necessary keyboard shortcuts so that you don’t need your mouse anymore!
Search inside the text file using Linux nano
Sometimes when reading through your text file, you might need to find a particular part of text or string. The best way to find a text is to use the command Control+W (^W). It’ll prompt a line at the bottom as Search: where you have to write the text you want to find. Hit enter to see the first match. To see the next matches, keep pressing the command Alt+W (M-W). You don’t need to do anything to exit the search.
Try the search command again. Did you notice that the bottom 2 lines that show shortcuts are now showing a different set of nano commands? These are the commands that can be applied inside the search query.
^T – Go To Line
You can navigate to either any line or column or both of them. After running the command ^W, now run the command ^T. Now the prompt will ask for the line number and column number. If you just enter one number, say 3, your cursor will be taken to the start of line 3. If you enter two numbers using a comma, say ‘3, 5’, then your cursor will be taken to the 5th character of the 3rd line. And if you enter one number but in this way, say ‘,5‘, then your cursor will end up at the 5th character of last working line.
M-B – Backwards
Starts matching the last match first. Use Alt+W to match the next one, but still in backwards.
M-C – Case Sensitive
In the nano text editor, by default, the match is case insensitive. So if you search for ‘mlwiki’, it’ll also find a match for ‘MLwiki’. If you want to be case sensitive while searching, use the command M-C.
M-R – Regex Search
You’ll learn about regex in Python. In short, when you want to match a set of strings that has a common pattern, you use regex. This is an amazing skill in searching for something. If you know some regex, then run the command M-R and write down your expression.
^R – Replace text
This is the search-and-replace operation you might have known earlier. Inside the search prompt, run the command ^R and the prompt will ask you for what to replace. After hitting enter, it will ask you for what to replace. So, first, you need to enter the present text, then the future text. Next, you can choose which match to replace or replace all at once.
Select, Copy, Cut, Paste operations in Linux nano editor
Move at the beginning or at the end of the text you want. Use the command Alt+A (M-A) and use the right, left, up, down arrow keys to Select the text. Press M-A again to unselect it.
To Copy the selected text, use Alt+6. To Cut the selected text, use Control+K or F9. Paste the text, use Control+U or F10.
Spell checking in Linux nano editor
You might have noticed the command ^T To Spell a lot of time in almost every nano command. This is used to check the spelling. But to use this feature, you need to have a spell checker. So before using the command, install a command-line spell checker using this code (don’t exit the current screen without saving it):
$ sudo apt install spell
After you’re done writing your contents, just run the command Control+T (^T). Then all of your spelling mistakes will be shown one by one and you’ll see a prompt that’s asking for the right spelling. Write the right spelling, or hit enter to skip the selected word, or enter ^C to exit spell checking.
The best part of it is that if you’ve made multiple same mistakes, you can overwrite all the spellings at once! Just select A for All on the next screen.
Saving a file
There’re 2 ways of saving a file. One is save and rename, the other one is save without prompt. Both of them are useful.
^O – Save and rename
As the title goes, when you run this command, a prompt will ask for the file name. If you’ve named it earlier, just hit enter. This is particularly necessary when you’ve opened the nano text editor without a file name. For example, if you run only this command from any directory:
$ nano
Then an empty new file will be created and opened here. So before closing this file, you must rename it. So ^U is a must here.
^S – Save without prompting
This is very common for all text editors. When you want to save your progress but don’t want to rename it every time, just use this command.
Exit the nano editor
Use ^X to exit the working screen and get back to the command-line screen.
Conclusion
Try reading these posts:
- If you’re new to Linux or Bash or Command-line – Introduction to Unix Shell
- To understand how paths work: paths and directories
- Learn how to handle the text files you just created – organizing files
- Keep your terminal clean with the clear command
- Other ways to create empty files