Introduction to vim editor

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.


Screenshot of neovim editor. Text Editing in Linux
Text Editing in Linux

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


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


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

Deleting Text

Undo and Redo


6. Saving and Quitting


7. Searching and Replacing

Searching

Replacing


8. Copying and Pasting


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:

Introduction to Helix:

Out-of-the-Box Features:

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

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.

Installation:

$ 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:

$ hx filename

Modes:

Normal Mode: Default mode for commands and navigation.

Select Mode: For selecting text.

Keybindings:

Navigation:

Editing:

Selection and Multiple Cursors:

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

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:

Additional Resources:

Summary:

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


Pages: 1 2 3