Free Republic
Browse · Search
General/Chat
Topics · Post Article

Skip to comments.

First steps with the Nano text editor
PragmaticLinux ^ | 18 May 2020 | PragmaticLinux

Posted on 05/20/2020 5:11:43 AM PDT by ShadowAce

Nano is a versatile, easy to use, and quick to learn text editor for the Linux terminal. This article presents the first steps with the Nano text editor. The goal is to get you comfortable with editing text files from the terminal. The article covers topics such as managing files, navigating the cursor, copy and paste, search and replace. The article includes brief exercises to try out your newly learned skills.

A general concept of Linux is that everything is a file. Especially when configuring your system and the installed applications, you typically do so by editing configuration text files. When working with Linux servers, you often only have access to the terminal through SSH. Consequently, the only way to edit files is with a terminal text editor. Luckily, there are plenty of terminal text editors available on Linux such as Nano, Vim and Emacs. I selected Nano for this article, because in my opinion it is the quickest one to learn and it gets installed by default on all major Linux distributions.

Let’s get started by creating a file with a few lines of text for testing purposes. Open the terminal in your home directory and run the command:

echo $'1. Debian\n2. Ubuntu\n3. openSUSE\n 4. Fedora\n5. Arch' >distros.txt

File management

Before diving into the actual text editing part, it is important to know how to open, close and save files. To open a file for editing with Nano, simply type nano followed by the name of the file. If the file is not in the current directory, then add the directory to the filename as well. In case your user does not have write access to the file, you can start the command with sudo to open and edit it with super user privileges. Examples:

sudo nano /etc/timezone

nano distros.txt

After opening the file in Nano, you can close it again with the Ctrl + x key combination. If you made changes to the file, Nano asks you if you want to save the changes. Press y to save the changes or n to ignore them, followed by Enter to commit.

Press the Ctrl + o keys to save changes while editing the file. Nano prompts you with the filename to save to. You can optionally change the filename, if desired. Press Enter to commit the save operation.

Exercise: Open and then close the distros.txt file that you created previously.

Shows the contents of the newly created distros.txt file in the Nano text editor.

Cursor navigation

Navigating the cursor is the same as when you edit a file with a regular word processor, such as LibreOffice Writer. This means the arrow keys move the cursor one position in the direction of the arrow. The Home key brings the cursor to the start of the line. Similarly, the End key brings the cursor to the end of the line.

The Page Up and Page Down keys move the cursor several lines up or down, respectively. This is handy for scrolling quickly through the file. To jump directly to the first line in the file, press Alt + \. The other direction is possible too by pressing Alt + /. This jumps straight to the last line in the file.

Exercise: Open the distros.txt file and navigate to the 4. Fedora line. This line starts with a space character and causes the listed Linux distributions to not be properly aligned. Fix this mistake (Backspace or Del), save the changes (Ctrl + o), and close the file (Ctrl + x).

Cut, copy and paste

Pressing the Ctrl + k keys cuts text to the internal buffer (clipboard) and the Alt + 6 key combination copies text to the buffer. To paste the contents of the buffer, starting at the current cursor position, press Ctrl + u.

If no text is marked (selected), Nano cuts or copies the entire line, where the cursor is currently located. Otherwise Nano only cuts or copies the marked text.

The Alt + a key combination enters a mode for marking text. Once entered, you can navigate the cursor to mark text starting at the current cursor location. To undo the text marking, simply exit the mode by pressing the same Alt + a keys again.

An alternative method for quickly marking text is by holding down the Shift key and then navigate the cursor. To me this is more intuitive and therefore my preferred technique for marking text.

Undo and redo

Nano keeps a history of all the operations you perform, making it possible to undo and redo changes. The Alt + u keys undo the last recorded operation. Likewise, the Alt + e keys redo the last operation you undid.

Exercise: Open the distros.txt file and use Nano’s cut and paste functionality to invert the order of the list with Linux distributions. If something goes wrong, you can rely on undo to correct it. Once you are done, save the changes and close the file. Its contents now look like:

Shows how the contents of the distros.txt file should look after completing the exercise where the entries were reordered.

Search and replace

Nano includes a search feature to quickly locate a specific string in a file. After pressing Ctrl + w, you can enter a search term. Hit Enter afterwards to start the search from the cursor position. Nano remembers the last string you searched for. It shows this string between square brackets, when starting a new search. This means that you do not have to retype the string when you want to search for the next occurrence of it.

Replacing one or all occurrences of a specific string is also supported by Nano. The Ctrl + \ key combination enters the search and replace mode. Similar to searching for text, you first enter the string to search for, followed by Enter. Right after this you can enter the string to replace it with. Hit Enter to continue with the replace operation. If Nano finds the string, it asks you if you want to replace this particular instance (y), skip it (n), or replace all instances of the string (a). This procedure is repeated, if the search string appears more than once in the text.

Exercise: Open the distros.txt file and use Nano’s replace functionality to change Arch to Manjaro. Once you are done, save the changes and close the file.

Screenshot of the Nano editor when a search and replace operation is active.

After reading through this article and completing the included exercises, you now know your way around the Nano editor and are comfortable with editing files directly from the terminal. In my experience, this is an essential skill when working with Linux. The last new Nano command to finish this article with is Ctrl + g. This opens a page in Nano with its help screen and serves as a quick reference. Press Ctrl + x to close the help page and return to the file you are editing.


TOPICS: Computers/Internet
KEYWORDS: linux
Navigation: use the links below to view more comments.
first previous 1-2021-4041 next last
To: dinodino

I also work on embedded systems, and most host computer can use a GUI. No vi needed.


21 posted on 05/20/2020 6:58:51 AM PDT by CodeToad (Arm Up! They Have!)
[ Post Reply | Private Reply | To 15 | View Replies]

To: CodeToad

Is that because you need a mouse to program?


22 posted on 05/20/2020 7:00:40 AM PDT by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 18 | View Replies]

To: CodeToad

There is no host computer. These are headless devices running through multiple intermediate specialized muxes. You either ssh in and Busybox, or nothing.


23 posted on 05/20/2020 7:03:25 AM PDT by dinodino
[ Post Reply | Private Reply | To 21 | View Replies]

To: ShadowAce

This is like going back to the stone age.


24 posted on 05/20/2020 7:24:50 AM PDT by aquila48 (Do not let them make you care! Guilting you is how they control you.)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

China uses Linux almost exclusively now.

If you use Linux, you are helping the ChiComs and their releases of bioweapons to the world.

Linux users are traitors to America.


25 posted on 05/20/2020 7:34:59 AM PDT by Lazamataz (China is the single source of evil in the entire world.)
[ Post Reply | Private Reply | To 1 | View Replies]

To: Lazamataz
China eats food. If you eat food, you are endorsing what China did in Wuhan.

If you eat, you are a traitor.

26 posted on 05/20/2020 8:15:13 AM PDT by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 25 | View Replies]

To: ShadowAce
My Linux admin duties - this being strictly a home network - are light and Konsole is sufficient to my cause. But thanks for the tip!
Started with RedHat 5.2 (? - am pretty sure), upgraded to RH 6.3 or 6.5, then eventually switched to Fedora 9, and now upgrade every couple of years.
Having to use Windows 10 at work, on my machine and two test systems, is a chore. The constant updates constantly break things. Funny, tho am supposedly running the same Windows 10 flavor (Enterprise) on all three systems, due to whatever reason - maybe minor variations in hardware (?) - the three handle the same automated test in entirely different ways. Frustrating is the polite way of putting it. Totally inefficient, however one puts it.
But Linux lets me update/upgrade on my schedule, and doesn't erase or reset my config files like Windows does. Linux seems to be a lot more civilized and mature than the kiddie stuff of Windows.
Twin monitors with multiple desktops are another think I love with Linux. Very useful for me to land a different project on each desktop. And with Compiz, can click-bump from one desktop to the next.
Anyway, am glad the world is big enough for Linux to survive. It's a spot of calm in the thrashing turbulence of technology evolution.
Cheers!
27 posted on 05/20/2020 8:32:17 AM PDT by Montana_Sam (Truth lives.)
[ Post Reply | Private Reply | To 12 | View Replies]

To: ShadowAce
China eats food. If you eat food, you are endorsing what China did in Wuhan. If you eat, you are a traitor.

Yabut, UNLIKE YOU, I admit it.

28 posted on 05/20/2020 8:44:55 AM PDT by Lazamataz (China is the single source of evil in the entire world.)
[ Post Reply | Private Reply | To 26 | View Replies]

To: Lazamataz

Thank you for that remarkably intelligent comment. My day is complete.


29 posted on 05/20/2020 8:46:17 AM PDT by Montana_Sam (Truth lives.)
[ Post Reply | Private Reply | To 25 | View Replies]

To: Montana_Sam
Thank you for that remarkably intelligent comment. My day is complete.

I live to serve!

30 posted on 05/20/2020 8:48:07 AM PDT by Lazamataz (China is the single source of evil in the entire world.)
[ Post Reply | Private Reply | To 29 | View Replies]

To: Lazamataz
I live to serve!

Much like Linux--TRAITOR!!

31 posted on 05/20/2020 9:01:25 AM PDT by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 30 | View Replies]

To: ShadowAce
"I live to serve!"

Much like Linux--TRAITOR!!

I'm so ashamed.


32 posted on 05/20/2020 9:03:19 AM PDT by Lazamataz (China is the single source of evil in the entire world.)
[ Post Reply | Private Reply | To 31 | View Replies]

To: Lazamataz

LOL!


33 posted on 05/20/2020 9:05:02 AM PDT by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 32 | View Replies]

To: Lazamataz
Linux users are traitors to America.

Don't make me come down to Atlanta and bitch-slap your dumb ass.

34 posted on 05/20/2020 9:51:15 AM PDT by usconservative (When The Ballot Box No Longer Counts, The Ammunition Box Does. (What's In Your Ammo Box?))
[ Post Reply | Private Reply | To 25 | View Replies]

To: usconservative

Your arms aren’t long enough to reach me when I would run away from you!


35 posted on 05/20/2020 10:46:03 AM PDT by Lazamataz (China is the single source of evil in the entire world.)
[ Post Reply | Private Reply | To 34 | View Replies]

To: Lazamataz

True, but as you know I know how to reach out and tap someone .....


36 posted on 05/20/2020 11:12:44 AM PDT by usconservative (When The Ballot Box No Longer Counts, The Ammunition Box Does. (What's In Your Ammo Box?))
[ Post Reply | Private Reply | To 35 | View Replies]

To: usconservative

You would never shoot me.....

.... not too often, anyways.


37 posted on 05/20/2020 11:15:10 AM PDT by Lazamataz (China is the single source of evil in the entire world.)
[ Post Reply | Private Reply | To 36 | View Replies]

To: Lazamataz
You should know better, never say never.

Hey, I'm going to be @ 3 hours away from you in a few weeks - you up for meeting me in Sevierville, TN?

38 posted on 05/20/2020 11:30:58 AM PDT by usconservative (When The Ballot Box No Longer Counts, The Ammunition Box Does. (What's In Your Ammo Box?))
[ Post Reply | Private Reply | To 37 | View Replies]

To: usconservative

I ain’t drivin no three hours!

There will be a better opportunity. :)


39 posted on 05/20/2020 12:28:14 PM PDT by Lazamataz (China is the single source of evil in the entire world.)
[ Post Reply | Private Reply | To 38 | View Replies]

To: Lazamataz

I’m driving 10 and you won’t drive 3? Bitch.


40 posted on 05/20/2020 1:49:19 PM PDT by usconservative (When The Ballot Box No Longer Counts, The Ammunition Box Does. (What's In Your Ammo Box?))
[ Post Reply | Private Reply | To 39 | View Replies]


Navigation: use the links below to view more comments.
first previous 1-2021-4041 next last

Disclaimer: Opinions posted on Free Republic are those of the individual posters and do not necessarily represent the opinion of Free Republic or its management. All materials posted herein are protected by copyright law and the exemption for fair use of copyrighted works.

Free Republic
Browse · Search
General/Chat
Topics · Post Article

FreeRepublic, LLC, PO BOX 9771, FRESNO, CA 93794
FreeRepublic.com is powered by software copyright 2000-2008 John Robinson