> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Working With Text Files

> Guide teaching Vim basics and safe editing practices for managing Linux configuration files, with commands, tips, backups, and hands on exercises for RHEL system administrators.

In this lesson we focus on one of the most important skills for a Red Hat Certified System Administrator (RHCSA): editing text files.

Almost everything in Linux is managed through text configuration files — services, networking, users, security policies, and system behavior are all controlled by editable files. If you can't confidently edit text files from a terminal, you can't confidently administer a RHEL system.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9E2T0ocsNwD7Od8i/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Working-with-Text-Files/Working-With-Text-Files/tux-to-policy-document-list.jpg?fit=max&auto=format&n=9E2T0ocsNwD7Od8i&q=85&s=de200a85d7e5358735db1bdf1f0b0e5a" alt="A dark-themed diagram shows the Linux Tux penguin linked by an arrow to a document/edit icon. To the right is a vertical list of items: Services, Networking, Users, Security Policies, and System Behavior." width="1920" height="1080" data-path="images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Working-with-Text-Files/Working-With-Text-Files/tux-to-policy-document-list.jpg" />
</Frame>

This lesson starts with Vim basics: understanding Vim modes, entering insert mode, saving changes, and exiting properly. Next we cover safe editing practices for system configuration files. Finally, practice exercises help you build confidence — the only way to become proficient with Vim is to use it often.

By the end of this lesson you'll be able to open, edit, and safely save configuration files on a live RHEL system.

***

## Why Vim?

Vim is the editor you'll most frequently encounter on RHEL servers because it works reliably in any terminal session (no GUI required). Think of Vim like driving a manual car: the concept is different at first, but once you understand the modes (the “gears”), you gain precise, fast control.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9E2T0ocsNwD7Od8i/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Working-with-Text-Files/Working-With-Text-Files/vim-configure-red-hat-enterprise.jpg?fit=max&auto=format&n=9E2T0ocsNwD7Od8i&q=85&s=85d3ffdcc4f08569e627ecfbd306c22f" alt="The image shows the Vim logo on the left with a pencil and arrow pointing to the Red Hat Enterprise hat logo and text on the right. It suggests using Vim to edit or configure something on Red Hat Enterprise." width="1920" height="1080" data-path="images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Working-with-Text-Files/Working-With-Text-Files/vim-configure-red-hat-enterprise.jpg" />
</Frame>

***

## Vim Modes (the core concept)

Vim works by switching between modes. Knowing which mode you are in is essential before you press keys.

| Mode | Purpose | Common entry keys |
| - | - | - |
| Normal (command) mode | Navigate and issue commands (default) | `Esc` (ensure you are in normal mode) |
| Insert mode | Type and edit text | `i`, `a`, `o` |
| Visual mode | Select text (character-wise) | `v` |
| Visual block mode | Select rectangular/column blocks | `Ctrl+v` |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9E2T0ocsNwD7Od8i/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Working-with-Text-Files/Working-With-Text-Files/vim-modes-command-insert-visual-block.jpg?fit=max&auto=format&n=9E2T0ocsNwD7Od8i&q=85&s=ac8cd1126dea678c404425419f97486f" alt="A dark-themed infographic titled &#x22;Vim Modes&#x22; showing four labeled cards for different modes: Command Line (&#x22;:&#x22;), Insert (&#x22;i&#x22;), Visual (&#x22;v&#x22;), and Visual Block (&#x22;CTRL+V&#x22;)." width="1920" height="1080" data-path="images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Working-with-Text-Files/Working-With-Text-Files/vim-modes-command-insert-visual-block.jpg" />
</Frame>

At any time press `Esc` to return to normal mode. Once you understand these modes, Vim becomes fast and predictable for system administration tasks.

<Callout icon="lightbulb" color="#1CB2FE">
  Tip: Press `Esc` to make sure you're in normal mode before issuing commands like `:wq`, `dd`, or search commands.
</Callout>

***

## Essential Normal-Mode Commands

Normal mode is where you control and manipulate the file — navigation, search, delete, copy/paste, and file operations happen here.

| Keys | Effect |
| - | - |
| `u` | Undo last change |
| `<Ctrl-r>` | Redo last undone change |
| `yy` | Yank (copy) the current line |
| `p` | Paste after the cursor (or after the current line) |
| `dd` | Delete the current line |
| `dw` | Delete from cursor to the end of the current word |
| `i` / `a` / `o` | Enter insert mode (`i` before cursor, `a` after, `o` new line below) |
| `:w` | Save (write) changes to the file |
| `:q` | Quit (refuses if there are unsaved changes) |
| `:q!` | Quit and discard unsaved changes (force quit) |
| `:wq` or `ZZ` | Save and quit |
| `:e <file>` | Open/edit the specified file |
| `/pattern` | Search forward for `pattern` — press `n` for next, `N` for previous |
| `?pattern` | Search backward for `pattern` — `n` repeats in same direction, `N` reverses |

Notes:

* Use `ZZ` (capital Z twice) in normal mode as a shortcut for save-and-exit.
* To view line numbers while editing: enter `:set number` in normal mode.
* For syntax highlighting: enter `:syntax on` (if supported).

***

## Safe Editing of Configuration Files

Editing system configuration files requires care. Follow these recommended practices to avoid service disruption.

| Task | Recommended command / practice |
| - | - |
| Create a backup before editing | `sudo cp /etc/some.conf /etc/some.conf.bak` |
| Edit files as root safely | Use `sudoedit /etc/some.conf` or `sudo vim /etc/some.conf` (depending on policy) |
| Recover from mistakes | Restore from backup: `sudo cp /etc/some.conf.bak /etc/some.conf` |
| Test config changes | Validate or restart the affected service (e.g., `sudo systemctl restart sshd` after editing `sshd_config`) |

<Callout icon="warning" color="#FF6B6B">
  Warning: Never edit critical system files without a backup. A syntax error in a config file can prevent services from starting. Use `sudoedit` or make a copy before editing, and test changes in a staging environment when possible.
</Callout>

Practical tips:

* If you can't save a file because of permissions, either use `sudoedit` or save to a temporary path with `:w /tmp/yourfile` and then move it into place with `sudo mv`.
* Prefer `sudoedit` for safer privilege escalation: it opens the file in your editor but writes as root only after you complete the edit.
* When editing remote systems, ensure you have an active secondary connection or a recovery method in case SSH config is accidentally broken.

***

## Hands-on Practice Exercises

Try these short exercises on a non-production system to build muscle memory:

1. Open a file: `vim /etc/hosts`
2. Ensure you're in normal mode (`Esc`), navigate to a line, press `i` and add a comment.
3. Save: `:w` and then quit: `:q` (or `:wq`).
4. Practice copy/paste: `yy` on a line, move, then `p`.
5. Delete a line with `dd`, then undo with `u`.
6. Use search: `/localhost` then `n` to jump through matches.
7. Create and test a backup workflow: `sudo cp /etc/hosts /etc/hosts.bak`.

***

## Further Reading and References

* Vim documentation: [https://www.vim.org/docs.php](https://www.vim.org/docs.php)
* Red Hat Enterprise Linux documentation: [https://access.redhat.com/documentation/en-us/red\_hat\_enterprise\_linux/](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/)
* Linux text file and editor basics: [https://linux.die.net/man/1/vim](https://linux.die.net/man/1/vim)

Practice regularly. Knowing how to edit and safely manage configuration files is a foundational skill for RHCSA and daily system administration.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/d9be30b7-d3f5-480f-8843-5dc9c4026c76/lesson/eea2856d-265e-4c4c-bfa8-2c154b2c00a1" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.