Learning the Command Line – Editing files

In this session we are going to learn one way of editing text files in Terminal. Back in the day there was pico. This was the editor part of the Pine mail program used in *nix. It was a very simple plain text editor used to create mail and text files. There are other editors such as vi, vim and emacs but those can get complicated and very powerful; these aren’t really needed to create or edit a simple text file.

Fast forward to today, we now have nano, the successor to pico. Various new features have been added such as interactive search and replace, among others. Nano is an excellent editor for editing dot-files, like .bashrc or .bash_profile and indeed, any plain text. In it’s simplest form you just use the command like this:

nano filename

If the file exists it will open it for editing. If it doesn’t, it will create it and open it. For example lets make a file called test1. This doesn’t exist yet so nano will create it and open it.

nano test1

Terminal — nano — test1-1
This is the editor window in Terminal. It is currently an empty file with command hints down at the bottom. Lets add some text. (Note: your screen may look different from mine depending on what theme you use in Terminal.) That blinking green square is the cursor. Start adding text to your file. Be aware that the mouse will not place the cursor. Use the arrow keys or shortcuts to move the cursor. Shortcuts can be found by typing control-G for help.
Terminal — nano — test1-2
This text doesn’t mean anything, just an example. So now we want to save it (no auto-save…I did say it was simple). Look down at the bottom of the window. See the command hints? In this case the ^ stands for the Control key. So to save a file, press control-O. (The window says ‘WriteOut’, same as Save)
Terminal — nano — test1 - 3
See how the bottom bar has changed? It says ‘Filename to write: test1’. Here’s where you can edit the filename if desired. You wont see a cursor when changing the name. Just press delete to backspace and type in a new name. If not, just press enter to save. Finished with the file? Type control-X to quit. Typing control-X without saving will prompt ‘Save modified buffer?’. Press y to save. Press n to just quit.
When editing be aware that the character directly under the cursor will remain when deleting. In other words, if you want to delete the f in ‘binford’, place the cursor on top of the o and press delete.
These are just the basics to get you started. There are many switches which can alter the behavior of nano. Power users can also use a .nanorc file in your User folder to set favorite characteristics.

Sources:
man nano via Manopen app. http://www.clindberg.org/projects/ManOpen.html
The nano command manual: http://www.nano-editor.org/dist/v2.2/nano.html