Skip to main content

LPI E - vi or nano

LPI E - Scripting

3.3 Turning Commands into a Script 

Review of Topics- Part 2 of 2
Part One: Scripting

Editors to use with Linux Command Line Interface (CLI)

Vi Editor 

The Vi editor is a powerful and versatile text editor that is included with most Linux distributions. It can be accessed from the terminal by typing vi followed by the name of the file you want to edit. For example, to edit a file called "test.txt", you would enter the command

$ vi test.txt

Once you have opened a file in vi, you can begin editing it by pressing the "i" key to enter insert mode. In insert mode, you can type and edit text as you normally would in any other text editor.

When you are finished editing, press the "Esc" key to return to command mode. From here, you can save your changes and exit the editor by typing :wq and pressing Enter. This command will save your changes and exit the editor.

If you want to exit the editor without saving your changes, type :q! and press Enter. This command will discard your changes and exit the editor.

One useful feature of vi for working with scripts is the ability to search and replace text. To search for a specific string of text, type / followed by the text you want to search for and press Enter. vi will then search for the text and highlight it on the screen. To replace the text, type :s/old/new/g and press Enter, where "old" is the text you want to replace and "new" is the text you want to replace it with.

Vi Editor Navigation

Navigation in Vi can take some getting used to, but it offers a lot of power and flexibility once you get the hang of it. In command mode, you can use the following key combinations to navigate around the document:

  • press h:
    • move the cursor one character to the left 
  • press j:
    • move the cursor down one line 
  • press k:
    • move the cursor up one line 
  • press l:
    • move the cursor one character to the right 
  • press w:
    • move the cursor to the beginning of the next word 
  • press b:
    • move the cursor to the beginning of the previous word 
  • press 0 (zero):
    • move the cursor to the beginning of the current line 
  • press $:
    • move the cursor to the end of the current line 
  • press G:
    • move the cursor to the end of the document 
  • press gg:
    • move the cursor to the beginning of the document
You can also use marks to navigate to specific parts of the document. To set a mark, press the m key followed by a letter (e.g. m a). To return to the mark, press ' followed by the same letter (e.g. 'a).

When working with programming languages in vi, it can be helpful to enable syntax highlighting. This will color-code different elements of your code to make it easier to read and understand. 

To enable syntax highlighting, add the following lines to your .vimrc file:

syntax on
set hlsearch

Nano Editor 

Nano is a simple and user-friendly text editor that is often recommended for beginners. It can be accessed from the terminal by typing nano followed by the name of the file you want to edit. For example, to edit a file called "test.txt", you would enter the command  

$ nano test.txt

Once you have opened a file in Nano, you can begin editing it immediately. Unlike vi, there is no need to enter a specific mode to begin editing. Simply type and edit text as you normally would in any other text editor.

When you are finished editing, press "Ctrl+X" to exit Nano. If you have made changes to the file, Nano will prompt you to save your changes before exiting. To save your changes, press "Y" and then Enter. To discard your changes and exit without saving, press "N" and then Enter.

Like vi, Nano also has the ability to search and replace text. To search for a specific string of text, press "Ctrl+W" and then type the text you want to search for. Nano will then search for the text and highlight it on the screen. To replace the text, press "Ctrl+\" and then type old followed by new and then "Enter" to replace all instances of "old" with "new" in the file.

Nano Editor Navigation

Navigation in Nano is generally more intuitive than in Vi. The arrow keys can be used to move the cursor around the document. 

You can also use the following key combinations:

  • Ctrl+A:
    • move the cursor to the beginning of the current line 
  • Ctrl+E:
    • move the cursor to the end of the current line 
  • Ctrl+Y:
    • move the cursor up one page 
  • Ctrl+V:
    • move the cursor down one page

When working with programming languages in Nano, you can enable syntax highlighting by adding the following line to your .nanorc file:

include /usr/share/nano/\<language\>.nanorc

Replace <language> with the appropriate language (e.g. c for C, python for Python).

Conclusion

In this answer, we have provided further details about navigation in the Vi and Nano text editors. We have also provided some specific instructions for working with programming languages in these editors, such as enabling syntax highlighting. With these additional details, you should be able to navigate and edit text more efficiently in the Linux Terminal. 

 

 

Disclaimer:

The samples provided here are intended to serve as a general guide and reference for individuals preparing for the LPI Linux certifications. These samples are not meant to represent the exact questions that may appear on the actual exam. The LPI certification exams are constantly updated and revised, and the questions on each exam are carefully crafted to assess a candidate's knowledge and skills.

Therefore, while we have made every effort to ensure the accuracy and relevance of the samples provided, we cannot guarantee that they will reflect the content or difficulty level of the actual exam. Additionally, we do not endorse or have any affiliation with the Linux Professional Institute (LPI).

We strongly recommend that candidates use these samples as an additional resource for their exam preparation, in combination with other study materials and practice tests. Ultimately, success on the LPI Linux certification exams will depend on an individual's knowledge, experience, and understanding of the exam objectives.

By using these samples, you agree that neither the provider of these sample questions nor any of its affiliates or employees shall be liable for any damages arising from your use or reliance on these sample questions or any information provided herein.