📢 Limited-time offer: Get lifetime access to premium Linux courses, eBooks, and certification resources for only $99 👉 Get Lifetime Access Now

Skip to main content

RHCSA Certification Course

Chapter #4: Editing Files in Linux with Nano, Vim, and Grep

A complete guide for RHEL users on editing files with Nano and Vim, plus tips for text analysis with grep and regex.

Every system administrator has to deal with text files as part of their daily responsibilities. That includes editing existing files (most likely configuration files), or creating new ones.

It has been said that if you want to start a holy war in the Linux world, you can ask sysadmins what their favorite text editor is and why.

We are not going to do that in this chapter, but will present a few tips that will be helpful to use two of the most widely used text editors in RHEL: nano (due to its simplicity and easiness of use, specially to new users), and vi/m (due to its several features that convert it into more than a simple editor).

I am sure that you can find many more reasons to use one or the other, or perhaps some other editor such as Emacs or Pico. It’s entirely up to you.

Editing Files with Nano Editor

To launch nano, you can either just type nano at the command prompt, optionally followed by a filename (in this case, if the file exists, it will be opened in edit mode).

If the file does not exist, or if we omit the filename, nano will also be opened in edit mode but will present a blank screen for us to start typing (see Fig. 1):

nano test
Figure 1: Nano initial screen

As you can see in the previous image, nano displays at the bottom of the screen several functions that are available via the indicated shortcuts (^, aka caret, indicates the Ctrl key).

To name a few of them:

Ctrl + G: brings up the help menu with a complete list of functions and descriptions (see Fig. 2):

Figure 2: Nano help

Ctrl + O: saves changes made to a file. It will let you save the file with the same name or a different one. Then press Enter to confirm (see Fig. 3).

Figure 3: Saving changes to a file
  • Ctrl + X: exits the current file. If changes have not been saved, they are discarded.
  • Ctrl + R: lets you choose a file to insert its contents into the present file by specifying a full path (see Fig. 4):
Figure 4: Inserting a file into the current window

will insert the contents of /etc/passwd into the current file.

  • Ctrl + K: cuts the current line.
  • Ctrl + U: paste.
  • Ctrl + C: cancels the current operation and places you at the previous screen.

To easily navigate the opened file, nano provides the following features:

  • Ctrl + F and Ctrl + B move the cursor forward or backward, whereas Ctrl + P and Ctrl + N move it up or down one line at a time, respectively, just like the arrow keys.
  • Ctrl + space and Alt + space move the cursor forward and backward one word at a time.

Finally,

  • Ctrl + shift + _ (underscore) and then entering X, Y will take you precisely to Line X, column Y, if you want to place the cursor at a specific place in the document (see Fig. 5):
Figure 5: Go to a specific line and column

The example above will take you to line 15, column 14 in the current document.

If you can recall your early Linux days, especially if you came from Windows, you will probably agree that starting with nano is the best way to go for a new user.

Membership