HowtoForge

Linux Basics: How to Edit Files on The Shell with Nano

Linux Basics: How to Edit Files on The Shell with Nano

Contents

1.1 Nano-An Overview

Nano is a fairly versatile and user-friendly text editor that is capable of being used in a fuss-free manner by the somewhat inexperienced users too, as against the more sophisticated text editors such like Emacs and Vim. While these are excellent programs, they do have a bit of a learning curve. It works seamlessly in combination with Sudo, much to the glee of end-users.

Simplicity of use remains the core forte of Nano . Its shortcuts function in a similar manner as GUI-based word processors (for instance Open Office and Word), hence the text editor requires a much shorter learning curve to get the hang of basic functions before you move on to plain text-editing.

The following reasons make Nano a highly sought-after command-line text editor:

1.2 Navigating Nano Text Editor

For the benefit of learners, given below are tips to get acquainted with the editor interface.

1.2.1 Opening, Saving and Inserting Files

1. When you enter Nano on its own at the command line, it gets brought up in its default state. Thanks to the excellent on-screen feedback which remains amongst the main strengths of the Nano text editor, users enjoy the privilege of entering text right away by simply making use of the the cursor keys to navigate.

2. On top, you will find a bar that would have the Nano version number, in addition to the name of the file that is being edited. Below, since you would not have opened the file as yet, you will find the term New Buffer. To open nano using an empty buffer, all you need to do is type its name (as given below):

nano /path/to/filename

Here, hence pressing Ctrl+O will lead you to the following prompt:

File Name to Write:

Nano shall follow the path to open that file if it does exists. If it does not exist, a new buffer would be automatically started with that filename in that directory.To edit the file, just start typing your changes.

3. If you have already opened an existing text file, the filename would be automatically filled, and all you need to do is press Enter. Else, you may type in a new filename as per your preference. Here, you will get to witness the wonders of Nano! If you press Ctrl+T at this point, the screen would transform into a helpful two-column file browser. You may now effortlessly use the cursor keys for navigating through directories to hunt for the filename you wish to to save to! The File Browser is pretty versatile, and remains available for other tasks too. You need to hit Ctrl+X for exiting the same, followed by Ctrl+C for cancelling the Save operation. Please note that if you have edited text without saving it, quite predictably, you shall be prompted as to whether you actually want to exit.

4. Subsequently, you need to hit Ctrl+R to read a file and insert the same into the current editing session. When the filename prompt is exhibited, you need to press Ctrl+T and search for the file you wish to include.

1.2.2 Cut and Paste Tasks

Though the Nano mechanism works a tad differently vis-a-vis other popular editors, it turns out to be quite efficient once users get a hang of it. Explained below is the process of cut and paste:

1. To understand this, you need to begin by typing a few lines at random, then moving the cursor to the second line. Here, if you press Ctrl+K, the line would vanish irrespective of the cursor position on the line. You have just kut away the whole thing! (that is the K in the step for you!).Now that you have stored the contents onto the clipboard, you may move elsewhere in that section and hit press Ctrl+U to uncut it (which means paste it into the document).

2. The above may also be done for multiple lines. You need to position the cursor on the topmost line of the chunk you wish to cut, and subsequently press Ctrl+K on all the lines you wish to cut. This will enable you to overwrite the previous contents of the clipboard with the chunk you just cut out. Once done, you need to move the cursor exactly to the place where you wish to paste the text, and press Ctrl+U .Consequently, all the lines that you removed in the cutting session will be put back into the document. You have the option to hit Ctrl+U numerous times to execute multiple pastes.

1.2.3 Navigation Around the File

Finding your way around the file is an absolute cinch! You may simply use the arrow keys on the keyboard for the same. Also, if you find the file contents to be too long to accommodate on the screen, you need to simply hit Ctrl+Y to move back a page and Ctrl+V to move forward a page.

Lastly, you may access fantastic online help during any stage of the process by simply hitting Ctrl+G.

1.3: Helpful Shortcuts

Here is a fairly helpful quick reference to the most common keybindings (shortcuts) when working on the Nano text editor to expedite your work:

Linux Basics: How to Edit Files on The Shell with Nano