Chapter 13: Introduction to vi/ vim

In this chapter, we’ll introduce the vi text editor, a powerful and ubiquitous tool in the Linux world. You’ll learn the basics of vi, including how to open, edit, save, and exit files. By the end of this chapter, you’ll be able to use vi for basic text editing tasks. 
Note: I will use vim in this chapter, and use vi/vim inter-changeably. Some Purists might frown, apologies in advance. I prefer simplicity over pedantics.


1. What is vi?

vi (pronounced “vee-eye”) is a text editor that’s been around since the 1970s. It’s lightweight, fast, and available on almost every Unix-like system, including Linux. While it has a steep learning curve, mastering vican make you incredibly efficient at editing text files.


2. Opening and Closing Files

Opening a File

To open a file in vi, simply type:

$ vim filename.txt

If the file doesn’t exist, vi will create it when you save.

Exiting vi

  • Save and Exit: Press Esc to ensure you’re in command mode, then type :wq and press Enter.
  • Exit Without Saving: Press Esc, then type :q!and press Enter.

3. Modes in vi

vi has two primary modes: 
1. Command Mode: This is the default mode when you open vi. In this mode, you can navigate the file, delete text, and enter other modes. 
2. Insert Mode: In this mode, you can type and edit text.

Switching Between Modes

  • Enter Insert Mode: Press i (insert before the cursor) or a (append after the cursor).
  • Return to Command Mode: Press Esc.

4. Basic Navigation

In command mode, you can navigate the file using the following keys: 
- h: Move left. 
- j: Move down. 
- k: Move up. 
- l: Move right. 
- 0: Move to the beginning of the line. 
- $: Move to the end of the line. 
- gg: Move to the beginning of the file. 
- G: Move to the end of the file.


5. Editing Text

Inserting Text

  • i: Insert before the cursor.
  • a: Append after the cursor.
  • o: Open a new line below the current line.
  • O: Open a new line above the current line.

Deleting Text

  • x: Delete the character under the cursor.
  • dd: Delete the current line.
  • dw: Delete the current word.

Undo and Redo

  • u: Undo the last change.
  • Ctrl + r: Redo the last undone change.

6. Saving and Quitting

  • Save Changes: Press Esc, then type :w and press Enter.
  • Save and Quit: Press Esc, then type :wq and press Enter.
  • Quit Without Saving: Press Esc, then type :q!and press Enter.

7. Searching and Replacing

Searching

  • Search Forward: Press /, type your search term, and press Enter.
  • Search Backward: Press ?, type your search term, and press Enter.
  • Navigate Matches: Press n to go to the next match and N to go to the previous match.

Replacing

  • Replace in the Entire File: Press Esc, then type :%s/old/new/g and press Enter.
  • old: The text to replace.
  • new: The replacement text.
  • g: Replace all occurrences in the line.

8. Copying and Pasting

  • Copy (Yank): In command mode, move the cursor to the start of the text, type v to enter visual mode, move the cursor to the end of the text, and press y.
  • Paste: Move the cursor to the desired location and press p.

Extra: Modern vi Alternatives

While traditional vi or vim remains a staple for text editing, neovim introduces modern enhancements like asynchronous plugin loading and Lua scripting.

The core navigation and editing commands remain the same, but Neovim’s plugin ecosystem allows for extensive customization. Tools like vim-plug or packer.nvim enable easy installation of plugins for features such as fuzzy finding, git integration, and syntax checking. 

By tailoring your vi-like editor with relevant plugins, you can significantly improve your editing efficiency and transform the terminal into a powerful development environment.

Use neovim for a modern vi experience:

$ sudo apt install neovim

Customize vim with plugins:

$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
  https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Practice Time!

Let’s put your new skills to the test: 
1. Open a new file in vi: $ vim practice.txt 
2. Enter insert mode and type a few lines of text. 
3. Save the file and exit vim
4. Reopen the file, practice navigating and editing the text, and then exit without saving.


Alternative Text Editors: Helix Editor

Overview:

Helix is a modern, open-source text editor written in Rust. It offers a fresh alternative to traditional editors like vi or vim, with a focus on performance, user experience, and modern features. 

Objectives:

  • Introduce Helix as an alternative to vim or vi.
  • Highlight key features of Helix.
  • Provide guidance on using Helix for text editing.

Introduction to Helix:

  • Inspired by Kakoune and Neovim: Helix takes inspiration from modal editors but introduces its own innovations. [[4]]
  • Written in Rust: Offers speed and safety benefits.

Out-of-the-Box Features:

Tree-sitter Integration: Provides accurate and fast syntax highlighting for multiple languages.

  • Language Server Protocol (LSP) Support: Offers IDE-like features such as code completion and go-to-definition without extra configuration. 

Key Features:

Selection-Based Editing:

Rather than issuing commands to affect text objects, you select text and then perform actions, aligning with how Kakoune operates.

Modern User Interface:

Syntax highlighting with Tree-sitter.

Built-in support for multiple programming languages.

No Configuration Needed:

Works well out of the box with sane defaults.

  • Minimal configuration to get started, though customization is possible.

Installation:

  • From Package Managers:

$ sudo apt install helix

Note: Availability may vary; check official repositories or download from Helix GitHub repository.

Building from Source:

$ git clone https://github.com/helix-editor/helix cd helix cargo install --path helix-term

Basic Usage:

  • Launching Helix:

$ hx filename

Modes:

Normal Mode: Default mode for commands and navigation.

  • Insert Mode: For inserting text (enter with i).

Select Mode: For selecting text.

Keybindings:

Navigation:

  • h, j, k, l: Move cursor left, down, up, right.
  • w, b: Move by words.
  • g, gg: Go to the start of the file.
  • G: Go to the end of the file.

Editing:

  • i: Enter insert mode.
  • x: Delete character under cursor.
  • dd: Delete the current line.
  • yy: Yank (copy) the current line.
  • p: Paste after the cursor.
  • u: Undo last change.
  • Ctrl+r: Redo.

Selection and Multiple Cursors:

Select Word: Place cursor over a word and press v.

  • Extend Selection: Use movement keys to expand the selection.
  • Add Cursors: Press Alt+Up/Down to add cursors on multiple lines.

Advantages over vim/neovim:

Modern Defaults:

Comes with built-in support for advanced features without needing plugins or configurations. 

Performance:

Efficient handling of large files.

User Experience:

Aims to reduce the learning curve with more intuitive keybindings. 

Considerations:

Different Keybindings:

Users accustomed to vim may need to adjust to Helix’s keybindings.

Community and Ecosystem:

Being a newer project, the community is smaller, and third-party support is growing. 

Hands-On Exercises:

  • Exercise 1: Open a file in Helix and practice basic navigation.
  • Exercise 2: Use multiple cursors to edit several lines simultaneously.
  • Exercise 3: Explore syntax highlighting and LSP features by opening a source code file.

Additional Resources:

  • Run hx --health to check tool installation and configuration status. 
  • Official documentation and tutorials are available on the Helix GitHub page.

Summary:

Helix offers a compelling alternative to traditional text editors by combining modern features, performance, and ease of use. It can be an excellent choice for users seeking a fresh editing experience.


That’s it for Chapter 13! You’ve now learned the basics of vi, one of the most powerful text editors in the Linux world. In the next chapter, we’ll dive into customizing the shell further, including advanced scripting and configuration. Until then, practice using vi to become more comfortable with its unique workflow.


Prev: Chapter 12 | Next: Chapter 14