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 1-2021-4041 next last

1 posted on 05/20/2020 5:11:43 AM PDT by ShadowAce
[ Post Reply | Private Reply | View Replies]

To: rdb3; JosephW; Only1choice____Freedom; martin_fierro; Still Thinking; zeugma; Vinnie; SW6906; ...

Tech Ping


2 posted on 05/20/2020 5:12:04 AM PDT by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

It’s so sad that these people don’t acknowledge the innate superiority of vi. All other text editors pale before it. Especially emacs.

(trying to start a flame war ;-)


3 posted on 05/20/2020 5:36:58 AM PDT by glorgau
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

Stuff like this scares people away from linux. There are plenty of gui text editors for linux. Mousepad and leafpad are two that are equivalent to notepad but unlike notepad, they can be used to write code too.


4 posted on 05/20/2020 5:38:17 AM PDT by Pollard (whatever)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce
Ping good article. Nice review. It has been a while since I work with Linux. I used Linux while in collage for C/C++ classes.

I am starting to get back into Linux since Microsoft release WSL and Docker. I am Exploring containers on Linux servers to host ASP.net web application.

Linux GUI apps coming to Windows 10 WSL along with GPU access
5 posted on 05/20/2020 5:38:45 AM PDT by DEPcom
[ Post Reply | Private Reply | To 1 | View Replies]

To: Pollard

“There are plenty of gui text editors for linux.”

I use Visual Code for editing on Linux, Mac, and Windows 10. Another good GUI editor is Notepad++. Both are free and support many languages.


6 posted on 05/20/2020 5:41:14 AM PDT by DEPcom
[ Post Reply | Private Reply | To 4 | View Replies]

To: glorgau
I like vi to.
I've used it for decades.
7 posted on 05/20/2020 5:42:19 AM PDT by StormEye
[ Post Reply | Private Reply | To 3 | View Replies]

To: ShadowAce
Never used 'nano' until using raspberry pi's. Now I use it all the time, even on this Fedora system [just updated to v 32], for editing small config files. When first starting Linux, RedHat 5.x, I was using emacs or vi, but that seemed overkill for editing small files.
Linux is all I use here at home (the wife still uses Windows 7). Fedora Mate Compiz spin on this big box, and raspbian - now Buster - with Compiz on the raspberry pi 4's.
Not being a gamer, no reason to use Windows. Have to use Windows 10 at work, but it's nowhere nearly as user friendly for me. For instance, using Caja file browser on Linux, I can have two folders open side-by-side in a single window, simplifying copying/moving files between machines. Similar for using Konsole for terminal/command line work, with multiple tabs available. Linux works the way I think.
That's my story. Have been using Linux as my primary home system for nearly twenty years.
8 posted on 05/20/2020 5:44:26 AM PDT by Montana_Sam (Truth lives.)
[ Post Reply | Private Reply | To 1 | View Replies]

To: DEPcom

I like sublime, also multi-platform, for code. In my case, code is html/css and very occasionally, php.


9 posted on 05/20/2020 5:49:06 AM PDT by Pollard (whatever)
[ Post Reply | Private Reply | To 6 | View Replies]

To: Pollard
Stuff like this scares people away from linux. There are plenty of gui text editors for linux.

Linux/unix editors predate modern GUI-based computer systems. They are there because at the time, it was all there was. Amazing, really, that all modern servers and even desktop machines, like Macs, are running Unix under the hood; an OS invented back in the 1960s. I guess it points to UNIX's rock solid simplicity that it is in use to this day.

10 posted on 05/20/2020 5:50:13 AM PDT by Flick Lives (The real virus is the MSM)
[ Post Reply | Private Reply | To 4 | View Replies]

To: ShadowAce
Time to start the religious war - when going minimal it's vi or nothing! ;-)

Then again don't listen to me, I'm continuing my journey into penguin inspired madness... Not satisfied with wandering around the Debian family tree for years and a half dozen or so distros I'm now experimenting with an Arch based distro...

I guess when I start building and packaging my own from source I'll know my descent into madness is complete.

11 posted on 05/20/2020 5:59:22 AM PDT by ThunderSleeps
[ Post Reply | Private Reply | To 1 | View Replies]

To: Montana_Sam
Similar for using Konsole for terminal/command line work, with multiple tabs available.

Try Terminator for your terminal. It's all I use now. Very powerful, and scriptable. For instance, at work, I do some admin on a couple of clusters. I have configs for terminator that will open multiple tabs, divide some tabs into multiple sections, and log into each node and put them all into a group just by clicking one icon on my desktop. That allows me to enter a command just once, and every node in the cluster will run it. I know--I could use pdsh, but this is more fun, and some nodes may fail. It allows me to watch the progress of my work easier. It's very cool.

Linux works the way I think.

Same here. I have a very difficult time working in Windows.

Have been using Linux as my primary home system for nearly twenty years.

Yup. I've run every version of Fedora except for 13. I was trying an experiment to just install every other version at that time, and decided that was not worth it. I started with Fedora Core 1, and haven't looked back.

12 posted on 05/20/2020 6:01:39 AM PDT by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 8 | View Replies]

To: ThunderSleeps
I guess when I start building and packaging my own from source I'll know my descent into madness is complete.

Here ya go

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

To: glorgau

It’s sad that in 2020 people are still using 45 year old command line editors, thinking they are necessary.


14 posted on 05/20/2020 6:22:07 AM PDT by CodeToad (Arm Up! They Have!)
[ Post Reply | Private Reply | To 3 | View Replies]

To: CodeToad

I work on embedded systems, and vi is the only option.


15 posted on 05/20/2020 6:43:39 AM PDT by dinodino
[ Post Reply | Private Reply | To 14 | View Replies]

To: CodeToad
It’s sad that in 2020 people are still using 45 year old command line editors, thinking they are necessary.

If all you have is a terminal connection to a system, a gui editor isn't going to do you much good. Personally, I prefer to use vi, but any console editor that you know works as far as I'm concerned. Also, if a 45 year old program works, why not use it? Newer /= better in all cases.

16 posted on 05/20/2020 6:55:02 AM PDT by zeugma (Stop deluding yourself that America is still a free country.)
[ Post Reply | Private Reply | To 14 | View Replies]

To: CodeToad
It’s sad that in 2020 people are still using 45 year old command line editors, thinking they are necessary.

What's sad is people who think that an editor good enough to last for 45 years isn't good enough to use.

17 posted on 05/20/2020 6:55:21 AM PDT by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 14 | View Replies]

To: ShadowAce

What’s sad are the people who choose to use it because they think it makes them a real programmer. VI and Emacs are crap and always were crap.


18 posted on 05/20/2020 6:56:27 AM PDT by CodeToad (Arm Up! They Have!)
[ Post Reply | Private Reply | To 17 | View Replies]

To: zeugma

“If all you have is a terminal connection to a system, a gui editor isn’t going to do you much good.”

Actually, it can. Think s l o w l y about this. If you are connected, your computer can do what it wants including presenting a file in a GUI.


19 posted on 05/20/2020 6:57:55 AM PDT by CodeToad (Arm Up! They Have!)
[ Post Reply | Private Reply | To 16 | View Replies]

To: dinodino

Even the PC BIOS has moved to GUI.


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


Navigation: use the links below to view more comments.
first 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