Bash - introducing the nano text editor

As you worked through the previous article, you may have realised that the techniques shown have limitations. Surely we need something with a bit more power and flexibility?

You'd be right: enter the command line text editor.

It's important you like the editor you use in your server administration and it probably won't surprise you to learn there are quite a few different editors available.

There isn't the space to introduce you to all of them so I'm going to concentrate on the editor I use: nano.

If you want to see if another editor is more to your taste, search the wide wild web for 'vim' or 'emacs'. These are two (of many) other popular editors. Warning: such a sublime topic can produce heated debate!

I use nano because I like it. I find it quick and easy and fits my purpose exactly.

Let's move into our sandbox directory and start nano:

cd sandbox
nano

A fairly plain screen presents itself with the version in the top-left corner. In this case it's version 1.3.10.

The strip at the bottom shows shortcuts to common tasks such as 'exit', 'where is' (i.e. find) and so on. The upward arrow (^), known as a caret, is a symbol for the Control Key, so to exit you would press Control and 'x'.

Quite handily, the first shortcut says 'Get Help'. Let's do that by pressing Control and 'g':

First thing to notice is the line:

^G      (F1)            Invoke the help menu

This gives another option of getting to this screen by pressing F1. I'll leave you to read through the rest but as you progress through the dialogue I think you'll be pleased with the breadth of options available to you.

Once ready, press Ctrl and 'x' to exit the help file.

Now open nano and paste the content of this nano.txt file into it:

It seems like a bit of nonsense now, but we'll use this text to practice saving the file, finding words, replacing them and deleting lines:

There are a couple of ways to save the file.

The first saves it 'in place' and does not close nano. This is accomplished with Ctrl and 'o' (the letter 'o', not the number 0). Nano asks what to name the file - let's call it 'test.txt'.

At the bottom of the screen, you'll be informed that nano 'Wrote 4 lines'.

The second way is to save the file and to exit. This is accomplished with Ctrl and 'x'. Try this now.

No warning was given as there was nothing new to save. Had you made adjustments it would ask if you wanted to save the changes (we'll see this in action later).

Now open the text.txt file with nano:

nano test.txt

Perhaps obviously, but nano opened the named file.

I know we can see all of the text, but lets use nano to find the word 'third'. Do this by pressing Ctrl and 'w' and enter 'third' at the Search prompt:

Press enter and the cursor has moved to the first instance of 'third'. Well, we want to replace that with 'second'. We could delete it by hand and type in 'second' but that can get messy.

Handily, there is a shortcut to replace words: Ctrl and '\' (the backslash is normally located on the bottom left of the keyboard).

Nano gives us the option:

Search (to replace) [third]:

the word 'third' is there as it saves the last search term. We can just press enter (if another word or phrase was wanted, simply type that in and press enter).

Next to 'Replace with', type 'second' and press enter. You will notice that the second instance of 'third', on the last line, is highlighted, we don't want to replace that one, so press 'n', it then moves through the file to find any other instances of 'third'. It has highlighted the one we want to replace so press 'y':

Done. I think you will agree that was pretty painless. A large file containing many instances of 'third' that need replacing with 'second' is easily navigated. The advantage over using regular expressions and 'sed' to accomplish this is that you have a choice - we didn't want every instance replaced!

Last item in our whirlwind tour of nano is to delete an entire line. I reckon the line containing 'I've no idea what I'm doing here!' should be deleted.

Using the arrow keys, scroll down one line and delete the line with Ctrl and 'k'. In fact that is so simple, you can make a mistake. If you do delete the wrong line, you can 'undo' the delete with Ctrl and 'u'.

Let's save the file and exit nano using Ctrl and 'x'. A warning is given - do we want to save or destroy our changes? Press 'y' to save the file. Again, a nice choice here, we could save it as test.txt (the original file name) or, if I'm not entirely sure this is the final change, I could save it as test_trial.txt. As it is, test.txt will do, so I press enter.

You saw from the help menu that there is a lot more you can do with nano, but this quick starter should get you going.

For me, nano is ideal. I'll repeat the opening paragraphs: there are many text editors, if you want to try another one, go ahead!

PickledOnion.

Digg it | del.icio.us | reddit | StumbleUpon

Article Comments:

Ajay 17 May, 2007

An article on permission system of unix might help lot of people like me coming from windows background.

PickledOnion 17 May, 2007

Ajay, you are absolutely right and there should be a permissions and users article (or one for each subject) within the next week or so. Thanks for your interest.

BobbyL 05 Jul, 2007

These tutorials are great. Keep it up. Thanks.

Comments are closed for this article.