103.8 Basic File Editing

LPIC-1 Exam Objective: 103.8 Basic File Editing #

Introduction #

Editing text files is a fundamental skill for any Linux system administrator. The LPIC-1 certification requires candidates to be proficient in using vi, a powerful and widely-used text editor. This tutorial will cover navigating documents using vi, understanding and using its modes, inserting, editing, deleting, copying, and finding text. We will also touch on other common editors like Emacs, nano, and vim, and how to set the default editor.

Key Knowledge Areas #

  1. Navigate a document using vi.
  2. Understand and use vi modes.
  3. Insert, edit, delete, copy and find text in vi.
  4. Awareness of Emacs, nano, and vim.
  5. Configure the standard editor.

vi Basics #

vi is a modal editor, meaning it has different modes for different operations. The two primary modes are:

  • Normal Mode: Used for navigation and manipulation of text.
  • Insert Mode: Used for inserting text.

Starting vi #

To start editing a file with vi, open a terminal and type:

vi filename

If the file does not exist, vi will create it.

In Normal Mode, you can navigate using the following keys:

  • h: Move the cursor left.
  • j: Move the cursor down.
  • k: Move the cursor up.
  • l: Move the cursor right.

You can also use arrow keys for navigation.

Understanding vi Modes #

  • Normal Mode: Press Esc to enter Normal Mode.
  • Insert Mode: Press i to enter Insert Mode at the cursor, o to open a new line below, or a to append after the cursor.

Basic Editing Commands #

Inserting Text #

  • i: Insert text at the cursor position.
  • o: Open a new line below the cursor and enter Insert Mode.
  • a: Append text after the cursor position.

Deleting Text #

  • x: Delete the character under the cursor.
  • dd: Delete the entire line.

Copying and Pasting Text #

  • yy: Yank (copy) the entire line.
  • p: Paste the yanked text after the cursor.

Finding Text #

  • /pattern: Search forward for pattern.
  • ?pattern: Search backward for pattern.

Saving and Quitting #

  • :w: Save the file.
  • :q: Quit vi.
  • :wq or ZZ: Save and quit.
  • :q!: Quit without saving.

Awareness of Other Editors #

  • Emacs: A highly customizable text editor with a large ecosystem of plugins.
  • nano: A simple, easy-to-use text editor, ideal for beginners.
  • vim: An enhanced version of vi with additional features and improvements.

Configuring the Standard Editor #

You can set the default editor by exporting the EDITOR environment variable. For example, to set vim as the default editor:

export EDITOR=vim

Real-World Examples #

Example 1: Editing a Configuration File on Ubuntu/Debian #

vi /etc/apt/sources.list

Navigate to the desired line using j or k, press o to open a new line, and add a new repository:

deb http://example.com/ubuntu focal main

Press Esc to return to Normal Mode, then :wq to save and exit.

Example 2: Editing a Log File on Enterprise Linux (RHEL/CentOS) #

vi /var/log/messages

Search for a specific error message using:

/error

Navigate through the matches using n (next) and N (previous). To delete a line, move the cursor to the line and press dd. Save changes with :w and exit with :q.

Conclusion #

Mastering vi is essential for efficient text file editing in Linux. Understanding its modes and commands will significantly enhance your productivity. While vi is a powerful tool, being aware of other editors and knowing how to set the default editor ensures you are well-prepared for various scenarios in the Linux environment.