Linux Basics: How to Edit Files on The Shell with Nano
Contents- 1.1: Nano- An Overview
- 1.2: Navigating Nano Text Editor
- 1.2.1 Opening, Saving and Inserting Files
- 1.2.2 Cut and Paste Tasks
- 1.2.3 Navigation Around the File
- 1.3: Helpful Shortcuts
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:
- It brags of numerous power-user features that render it comparable with Vim and Emacs
- It is quite lightweight and finds inclusion in a majority of distributions
- It is idiot-proof and simple-to-use, and offers adequate-screen feedback
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
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:- Ctrl+X: This is used to exit the editor. If you have edited the text without saving the same, you will be asked by thee system if you really want to quit, thus alerting you to save your work if you wish to.
- Ctrl+O: This lets you write (O stands for Output here) the current text buffer contents to a file. You will come across a filename prompt upon hitting this, and subsequently you need to press Ctrl+T to open the file navigator.
- Ctrl+R: It lets you read a text file well into the current editing session. At the filename prompt, you need to hit Ctrl+T to reach the file navigator.
- Ctrl+K: This is used to cut a line into the clipboard, and users may repeatedly press this in order to copy multiple lines, which may then be conveniently stored as one chunk.
- Ctrl+J: It is used to justify/fill out a text paragraph. By default, it reflows text to match the editing window width and make your life easier.
- Ctrl+U: It lets you uncut text, which means pasting it from the clipboard.
- Ctrl+T: This is used to do a spell check.
- Ctrl+W: It lets you find a particular word/ phrase. At the prompt, you may use the cursor keys to navigate through previous search terms, or alternatively hit Ctrl+R to get into replace mode. Else, you also have the option of pressing Ctrl+T to go to a particular line.
- Ctrl+C: It shows the current line number, along with the file information.
- Ctrl+G: This leads you to the Nano help manual, and gives you access to all the information you need on navigating through files and helps you learn about common keyboard commands.