surrealdb
+
elementary
+
+
nomad
pinecone
json
graphql
+
raspbian
rubymine
wasm
+
emacs
rb
nvim
fastapi
bsd
couchdb
pinecone
+
yaml
=
emacs
pip
emacs
pnpm
+
perl
supabase
+
couchdb
+
travis
+
+
weaviate
*
fedora
+
0x
strapi
+
gradle
hugging
jenkins
ray
>=
+
+
+
+
+
lua
+
webpack
http
vscode
mysql
+
node
react
qwik
::
dart
[]
+
π
+
+
+
protobuf
elixir
torch
+
helm
+
+
android
sse
elementary
surrealdb
+
+
+
asm
+
+
Back to Blog
How to use Vi/Vim for efficient text editing
Linux

How to use Vi/Vim for efficient text editing

Published Nov 14, 2023

Vi/Vim, a long-standing Unix text editor, might seem tricky at first. Yet, with practice, it becomes a quick and efficient tool for text editing.

12 min read
0 views
Table of Contents

Introduction

When it comes to text editors, there are many options available. However, for many programmers, vi/vim remains a popular choice for editing text files. In this article, we’ll explore the basics of vi/vim, including how to navigate, edit, and save files efficiently.

What is vi/vim?

Vi is a text editor that originates from the early days of Unix, while vim (which stands for Vi Improved) is an enhanced version of vi with additional features. Both are command-line text editors that don’t have a graphical user interface, which can make them seem intimidating at first. However, once you get the hang of them, they can be incredibly powerful and efficient.

Reasons for using vi/vim

There are several reasons why many programmers and system administrators prefer vi/vim:

  • Efficiency: Once you learn the commands, you can navigate and edit files incredibly quickly.
  • Portability: Vi/vim is available on virtually all Unix-based systems, making it a reliable choice.
  • Customizability: Vi/vim is highly configurable, with many options and plugins available.

Basic Terminology

Before we dive into using vi/vim, let’s cover some basic terminology:

  • Mode: Vi/vim operates in different modes, primarily Normal mode, Insert mode, and Visual mode.
  • Cursor: The position where text will be inserted or deleted.
  • Buffer: A temporary memory space for storing text while editing.
  • Command: A keystroke or series of keystrokes that performs an action.
  • Escape key: Used to exit Insert mode and return to Normal mode.

Getting Started with vi/vim

Installing vi/vim

Unix-based systems

Most Unix-based systems come with vi preinstalled. However, if you want to install vim, you can do so using your system’s package manager.

For Ubuntu/Debian:

sudo apt-get install vim

For macOS with Homebrew:

brew install vim

Windows

For Windows, you can download vim from the official website. There are two main options:

  1. Self-installing executable: This is the easiest option. Download and run the installer.
  2. Zip archive: Download the zip file and extract it to a folder of your choice.

Understanding Vi/Vim Modes

One of the most important concepts in vi/vim is the idea of modes. There are three primary modes:

  1. Normal Mode: This is the default mode when you open vi/vim. In this mode, you can navigate the file and issue commands using keystrokes. For example, typing dd will delete the current line, and p will paste the contents of the clipboard.

  2. Insert Mode: In this mode, you can type text as you would in a regular text editor. To enter Insert mode from Normal mode, press i. To exit Insert mode and return to Normal mode, press the Escape key.

  3. Visual Mode: This mode allows you to select text visually. To enter Visual mode from Normal mode, press v. You can then use the arrow keys to select text. Once text is selected, you can perform operations like deletion or copying.

Moving Around in Vi/Vim

Efficient navigation is key to using vi/vim effectively. Here are some basic movement commands:

  • Arrow keys: You can use the arrow keys to move the cursor up, down, left, and right.
  • h, j, k, l: These keys serve the same purpose as the arrow keys. h moves left, j moves down, k moves up, and l moves right.
  • w: Move forward one word.
  • b: Move back one word.
  • 0: Move to the beginning of the line.
  • ^: Move to the first non-blank character of the line.
  • $: Move to the end of the line.
  • G: Move to the end of the file.
  • gg: Move to the beginning of the file.

Editing Text in Vi/Vim

To edit text, you’ll typically switch to Insert mode. Here are some common ways to enter Insert mode:

  • i: Insert before the cursor.
  • a: Insert after the cursor.
  • I: Insert at the beginning of the line.
  • A: Insert at the end of the line.
  • o: Open a new line below the current line and insert.
  • O: Open a new line above the current line and insert.

Once in Insert mode, you can type normally. To save your changes and exit Insert mode, press Escape.

Saving and Exiting Vi/Vim

Here are the basic commands for saving and exiting:

  • :w: Save the file.
  • :q: Quit vi/vim (this will fail if there are unsaved changes).
  • :wq or :x: Save and quit.
  • :q!: Quit without saving changes.

Basic Vi/Vim Commands

File Management

  • :e filename: Open a file for editing.
  • :w filename: Save the file with a specific name.
  • :r filename: Read a file and insert its contents after the cursor.

Editing Commands

  • x: Delete the character under the cursor.
  • dw: Delete from the cursor to the end of the word.
  • dd: Delete the entire line.
  • yy: Copy (yank) the entire line.
  • p: Paste after the cursor.
  • P: Paste before the cursor.
  • u: Undo the last action.
  • Ctrl+r: Redo.

Search and Replace

  • /pattern: Search forward for a pattern.
  • ?pattern: Search backward for a pattern.
  • n: Go to the next search result.
  • N: Go to the previous search result.
  • :%s/old/new/g: Replace all occurrences of “old” with “new” in the file.
  • :s/old/new/g: Replace all occurrences of “old” with “new” in the current line.

Advanced Vi/Vim Features

Using Multiple Files

Vi/vim allows you to work with multiple files simultaneously:

  • :e filename: Open another file.
  • :bn: Go to the next buffer (file).
  • :bp: Go to the previous buffer.
  • :bd: Delete the current buffer.

Split Windows

You can split the vi/vim window to view multiple files or different parts of the same file:

  • :sp filename: Split horizontally and open a file.
  • :vsp filename: Split vertically and open a file.
  • Ctrl+w w: Switch between windows.
  • Ctrl+w q: Close the current window.

Macros

Macros allow you to record and replay a series of commands:

  • qa: Start recording a macro in register ‘a’.
  • q: Stop recording.
  • @a: Play the macro stored in register ‘a’.
  • @@: Replay the last played macro.

Customizing Vi/Vim

Vi/vim is highly customizable. You can create a configuration file (.vimrc in your home directory) to set preferences:

" Enable line numbers
set number

" Enable syntax highlighting
syntax on

" Set tab width
set tabstop=4
set shiftwidth=4

" Enable auto-indentation
set autoindent

Tips for Becoming Proficient

  1. Practice regularly: The more you use vi/vim, the more natural it becomes.
  2. Learn incrementally: Don’t try to learn all commands at once. Start with basic navigation and editing.
  3. Use vimtutor: Run vimtutor in your terminal for an interactive tutorial.
  4. Customize your setup: Create a .vimrc file to make vi/vim work the way you want.
  5. Use plugins: Consider using plugin managers like Vundle or Pathogen to extend functionality.

Conclusion

Vi/vim may have a steep learning curve, but the investment in learning it pays off in increased productivity and efficiency. With its powerful command system, extensive customization options, and availability on virtually every Unix-based system, vi/vim remains a valuable tool for programmers and system administrators. Start with the basics, practice regularly, and gradually incorporate more advanced features into your workflow. Before you know it, you’ll be editing text at lightning speed!