How to edit a text in Linux using Nano – A beginner’s guide

Many new Linux users feel intimidated by the classical Linux text editors, i.e. mainly VIM and EMACS since they require a prior knowledge of basic shortcuts and some Linux basics knowhow. Fortunately for beginners there is a simple yet powerful newbie friendly text editor called NANO. NANO comes pre-installed with Ubuntu and other similar distros. It is a versatile and reliable text editor that offers all the basic functionalities a user would expect like editing operations (search, replace, cut and paste), syntax highlighting as well as spell checking and many more features. In this article we will take you through the steps of using the NANO text editor. Let’s get started.

NANO Installation

As NANO is installed by default , you can make sure it is available by checking its version with the help of the command :

nano –version

If nano is not installed on your system, you can use the following command to install it using the package manager utility of your current distribution.

sudo apt install nano

File handling – open and create operations

In order to create a new file, just type in the following command :

nano new_file

As you can see, since the file did not exist before, NANO will create a new buffer with the filename you provided in the current directory. You could however have specified a directory where the new file will be created, for example : nano Public/new_file

To open an existing file, just type in nano followed by the name of the file, as you did above with a new file (remember that you must have read permissions to the file).

NANO Text editor description

Now when you created a new file above, the following editor will show up :

As you can see, the name and the version of the program that you used along with the file name you are currently editing are located at the top of the window. You will also notice at the third line from the bottom the text NEW FILE which is a system message that shows some information which is relevant to the current operation of the NANO program. In the snapshot above, you can see that the file has not been modified before and that it is new. If for example you make some modification and try to exit, this area (third line from the bottom) will show the text below :

In the middle, the body (or the content) of the text is located. This is where you will be able to edit the text that you will be inserting.

Finally, the bottom most two rows contain the shortcut lines. This is what makes this utility very newbie-friendly.

The NANO editor is a “what you see is what you get” (WYSIWYG) text processing utility. Whatever text you type in will show up directly into the text input, unless you use an extra key control. You can go ahead and type in any text you want. You can also copy (Shift + c) and paste (Shift + Cntrl + V) using the keys or via the top menu of the terminal :

Now back to the shortcuts that we mentioned above, all the shortcut commands shown are prefixed with either the M character or the caret ^ symbol which represents the Ctrl key. For instance, the ^W command instructs you to press the Ctrl and W keys simultaneously ( Ctrl+W is NOT Ctrl+Shift+W ). The M letter points to the Alt key. By typing Ctrl+G, you can bring up the help documentation and by scrolling down you will be able to see a list of relevant shortcuts .

Now press CTRL+X to exit the help documentation above.

If for example you have a long file with many sections in it, you can directly open up the file with the cursor positioned on a specific line number and character number by running the command as follows :

nano +line_number,column_number file_name

If the column_number is omitted, the cursor will be located on the first column.

File editing

NANO is a modeless editor, unlike vi, which enables you to start typing and editing as soon as the file is opened.

In order to displace the cursor to a line and column number, use the Ctrl+_ command.

As you can see above, the message line on the bottom will change. You can enter the numbers in the then hit Enter.

Search and replace

To search for a text, press Ctrl+W,

Now type in the text you want to look up and hit Enter. If there are many matches, use Alt+W to move on to the next match.

If you want to search in order to replace, press Ctrl+\.

Now fill in the search term , hit enter and then type in the text you want to replace with.

The cursor will then move to the first match and prompts for replacement:


Once you hit Y or N it will go to the following match. If you click on A, as shown above, it will replace all matches.

If you have opened a new file (or buffer), new_file1 for example and started editing it. Now if you want to save it, you will need to ‘write it out’ . This operation is executed by pressing Ctrl+O.

As you can see above, you’ll be asked for a file name to write and you can also notice that the shortcuts have changed in order to guide you through to complete this command.

Copy, paste and cut

In order to make a text selection, move the cursor to the position of the first character of the text you want to select and press the SHIFT key. This will set a selection mark.

Now, maintain the SHIFT key and have the cursor move to the end of the block you want to mark for selection using the arrow keys. You will notice that the text has been highlighted.

Pressing CTRL+6 will cancel the selection.

You can also use the combination CTRL+^. Just move the cursor to where the marking should start and then press CTRL+^ in order to set it. All characters between the starting point and the cursor position will be marked (cursor not included in the marking).

In order to copy the text you have selected to the clipboard, use the Alt+6 command. If you want to cut the selected text, press CTRL+K.

CTRL+K will also cut whole lines. To do this, move the cursor to the specific line and then press Ctrl+k. Hitting Ctrl+k several times will cut multiple lines .

In order to paste the text you have copied, move the cursor to the target position and press CTRL+U.

NOTE:

Any text that was copied or cut within nano editor can only be pasted back into the same nano session. You will not be able to paste it back into other windows or programs.

Save and Exit

As mentioned above, pressing CTRL+O will allow you to save the changes you’ve made to the file and if the file is new it will be created after you save it.

To exit nano, simply press Ctrl+X. You’ll be prompted to save the changes had you made modifications that were unsaved.

Creating a new file will require write permission to its parent folder.

Nano as the default text editor

On most Linux distros, the default text editor for some commands like visudo or crontab is set to vi. In order to set nano as the default, you would need to modify the environment variables VISUAL and EDITOR.

Bash users can simply export these variables in the file ~/.bashrc:

~/.bashrc

export VISUAL=nano

export EDITOR=”$VISUAL”

Nano command options

In order to know other options for the nano editor, just look up the man page related to nano :

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu
Show Buttons
Hide Buttons