How to use Vi/Vim for efficient text editing

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.

How to use Vi/Vim for efficient text editing


When it comes to text editors, there are a plethora of options available, from simple and easy-to-use editors like Notepad to feature-rich editors like Sublime Text and Atom. However, for many programmers, vi/vim remains a popular choice for editing text files. In this article, we'll explore what vi/vim is, why you might want to use it, and some of the basic terminology you'll need to know to get started.

  1. Explanation of what vi/vim is

Vi is a text editor that has been around since the early days of Unix, while vim (Vi Improved) is an enhanced version of vi. Both are command-line editors, meaning that they don't have a graphical user interface (GUI) and are instead run from the command line. This may seem daunting at first, but once you become comfortable with vi/vim, you may find that it offers a more efficient and streamlined way of editing text files.

  1. Reasons for using vi/vim

There are several reasons why programmers and other tech professionals choose to use vi/vim as their primary text editor. Here are just a few:

  • Efficiency: Because vi/vim is a command-line editor, it can be very fast to use once you learn the commands. You don't have to move your hands away from the keyboard to use a mouse or touchpad, which can save time and increase productivity.
  • Portability: Vi/vim is available on virtually all Unix-based systems, as well as on Windows and Mac OS X, so you can use it regardless of the platform you're working on.
  • Customizability: Vi/vim is highly customizable, with a wide range of options and plugins available to suit your preferences and needs.

  1. Basic terminology used in vi/vim

Before we dive into the details of using vi/vim, it's important to understand some of the basic terminology used in the editor. Here are a few terms you'll need to know:

  • Mode: Vi/vim has several modes, including Normal mode, Insert mode, and Visual mode. Each mode has different commands and behaviors.
  • Cursor: The cursor is the blinking line or block that indicates where text will be inserted or deleted.
  • Buffer: A buffer is the memory space used to store the text being edited.
  • Command: In vi/vim, a command is a combination of keystrokes that perform an action. Commands are entered in Normal mode.
  • Escape key: The Escape key is used to exit Insert mode and return to Normal mode.

With these basic terms in mind, you're ready to dive into using vi/vim.

Getting Started with vi/vim

If you're new to vi/vim, getting started can seem daunting. But with a little practice, you'll soon find that vi/vim can be a powerful and efficient tool for editing text files. In this section, we'll cover the basics of getting started with vi/vim, including how to install it, open it, and understand the different modes available.

  1. Installing vi/vim

Installing vi/vim can vary depending on your operating system. Here are the steps to install it on a few common platforms:

  • Unix-based systems

Most Unix-based systems, such as Linux and macOS, come with vi preinstalled. If you prefer to use vim, you can install it using your system's package manager. For example, on Ubuntu or Debian-based systems, you can use the following command:

sudo apt-get install vim

On macOS, you can use the Homebrew package manager to install vim:

brew install vim

  • Windows

On Windows, you can download vim from the official website ( There are two versions available: the "self-installing executable" and the "zip archive". The self-installing executable is recommended for most users, as it includes a graphical installer that will guide you through the installation process.

To install vim using the self-installing executable, follow these steps:

  1. Download the executable from the vim website.
  2. Double-click the downloaded file to start the installation process.
  3. Follow the prompts in the installer to complete the installation.

If you prefer to use the zip archive, you can extract the contents of the archive to a folder on your computer. You can then run vim from the command prompt in that folder.

  • Other platforms

Vim is available on a wide range of platforms, including BSD, Solaris, and AIX. The installation process will vary depending on your platform, but you can usually find instructions on the vim website or from your system's documentation.

Once you have installed vim, you can open it from the command line by typing vim. This will open vim in Normal mode, ready for you to start editing text files. In the next section, we'll cover how to navigate around in vi/vim.

  1. Understanding the different modes in vi/vim

One of the unique features of vi/vim is its different modes, each with its own set of commands and behaviors. Here are the three main modes you'll need to know:

  • Normal mode

When you first open vim, you'll be in Normal mode. In this mode, you can navigate around the text file and issue commands using keystrokes. For example, typing dd will delete the current line, and typing p will paste the contents of the clipboard after the cursor.

  • Insert mode

To start inserting text, you need to switch to Insert mode. You can do this by pressing the i key. In Insert mode, you can type as you would in any other text editor. To return to Normal mode, press the Escape key.

  • Visual mode

Visual mode allows you to select text in a variety of ways, such as by character, line, or block. To enter Visual mode, press the v key. You can then use the arrow keys to select the text you want. Once you have selected the text, you can perform operations on it, such as deleting it or copying it to the clipboard.

  1. Moving around in vi/vim

Moving around in vi/vim can take some getting used to, as it relies on a combination of keystrokes and commands. Here are the three main ways to move around in vi/vim:

  • Using the arrow keys

The simplest way to move around in vi/vim is to use the arrow keys on your keyboard. This will move the cursor one character or one line at a time, depending on the direction of the arrow key.

  • Using the hjk, and l keys

In Normal mode, you can use the hjk, and l keys to move left, down, up, and right, respectively. These keys correspond to the arrow keys on the keyboard, but using them can be faster and more efficient, as your hands can stay on the home row.

  • Using other movement commands

In addition to the arrow keys and the hjk, and l keys, vi/vim has a variety of other movement commands that can help you navigate around a text file. Here are a few examples:

  • 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

These commands can be combined with a number to move a specific number of lines or words. For example, typing 3w will move the cursor forward three words.

With these movement commands in mind, you can start navigating around a text file in vi/vim.

Editing Text in vi/vim

Editing text in vi/vim can be more complex than in other text editors, but it also gives you more control over your text. Here are the main commands for editing text in vi/vim:

  1. Inserting text in vi/vim

In vi/vim, you can enter Insert mode to add text to a file. To enter Insert mode, press the i key in Normal mode. You can then start typing your text. When you're finished, press the Esc key to return to Normal mode.

There are several other ways to enter Insert mode, depending on your preference. For example, you can use the a key to enter Insert mode after the current cursor position, or the o key to start a new line and enter Insert mode.

  1. Deleting text in vi/vim

To delete text in vi/vim, you first need to move the cursor to the location of the text you want to delete. You can then use one of several commands to delete the text:

  • x - delete the character under the cursor
  • dd - delete the current line
  • d$ - delete from the cursor position to the end of the line
  • dw - delete the current word
  • d3w - delete the next three words

  1. Replacing text in vi/vim

To replace text in vi/vim, you can use the r command. Move the cursor to the character you want to replace, then type r followed by the new character. The old character will be replaced with the new one.

You can also use the s command to delete the character under the cursor and enter Insert mode. This is a quick way to replace a single character.

  1. Copying and pasting text in vi/vim

In vi/vim, copying and pasting text involves two steps: yanking and putting.

To yank text, move the cursor to the beginning of the text you want to copy, and type y followed by a movement command. For example, to yank the current line, type yy. To yank the next three words, type y3w.

To put the yanked text somewhere else in the file, move the cursor to the location where you want to insert the text, and type p. This will paste the yanked text after the current cursor position.

You can also use the P command to paste the yanked text before the current cursor position.

With these editing commands in mind, you can start making changes to a text file in vi/vim.

Working with Multiple Files in vi/vim

When working on a large project, it's common to have multiple files that you need to edit at the same time. In vi/vim, there are several ways to work with multiple files, including:

  1. Opening multiple files in vi/vim

To open multiple files in vi/vim, simply list them as arguments when you start vi/vim. For example, to open two files named file1.txt and file2.txt, you can use the command:

vi file1.txt file2.txt

This will open both files in separate buffers in vi/vim.

  1. Moving between files in vi/vim

Once you have multiple files open in vi/vim, you can use the :next and :prev commands to move between them. For example, to move to the next file, type :next and press Enter. To move to the previous file, type :prev and press Enter.

You can also use the :e command to open a new file in the current buffer. For example, if you're currently editing file1.txt and you want to switch to file2.txt, you can type :e file2.txt and press Enter.

  1. Splitting the screen in vi/vim

Another way to work with multiple files in vi/vim is to split the screen into multiple windows. To split the screen horizontally, use the command :split. To split the screen vertically, use the command :vsplit.

For example, if you're currently editing file1.txt and you want to open file2.txt in a new horizontal window, you can type :split file2.txt and press Enter.

  1. Navigating between split screens in vi/vim

Once you've split the screen in vi/vim, you can use the Ctrl-w key combination followed by a movement command to navigate between the split screens.

For example, to move to the window below the current window, you can type Ctrl-w j. To move to the window on the left, you can type Ctrl-w h.

With these commands, you can work efficiently with multiple files in vi/vim.

Searching and Replacing in vi/vim

Searching for text and replacing it is a common task when editing text files, and vi/vim offers powerful features to make it easy. In this section, we'll cover the basics of searching and replacing text in vi/vim, as well as using regular expressions.

  1. Searching for text in vi/vim

To search for text in vi/vim, enter the command mode by pressing the Esc key, and then type a forward slash followed by the text you want to search for. For example, to search for the word hello, type /hello and press Enter.

Vi/vim will highlight the first occurrence of the text and place the cursor on it. To find the next occurrence of the text, type the letter n. To find the previous occurrence, type N.

You can also search for text in a specific direction by using the forward search and backward search commands. To search forward for text, type /text and press Enter. To search backward for text, type ?text and press Enter.

  1. Replacing text in vi/vim

To replace text in vi/vim, enter the command mode by pressing the Esc key, and then type the letter s followed by the text you want to replace and the text you want to replace it with. For example, to replace the word hello with goodbye, type s/hello/goodbye and press Enter.

This will replace the first occurrence of the text on the current line. To replace all occurrences of the text on the current line, add the g modifier to the end of the command: s/hello/goodbye/g.

To replace all occurrences of the text in the entire file, use the :%s command. For example, to replace all occurrences of the word hello with goodbye in the entire file, type :%s/hello/goodbye/g and press Enter.

  1. Using regular expressions in vi/vim

Regular expressions are a powerful way to search for and replace text based on patterns. In vi/vim, you can use regular expressions in search and replace commands by using special characters.

For example, to search for any string that starts with foo and ends with bar, you can use the regular expression / The . means "any character, zero or more times".

To replace text using regular expressions, use the s command as before, but include the regular expression in the search string. For example, to replace any occurrence of the word foo followed by one or more digits with the word bar, you can use the command :%s/foo[0-9]+/bar/g.

With these tools, you can quickly and easily search for and replace text in vi/vim, including using powerful regular expressions.

Advanced Features of vi/vim

Vi/vim offers a vast range of features beyond the basic text editing functions. In this section, we'll explore some of the more advanced features of vi/vim, including macros, creating and using custom commands, and using plugins.

  1. Macros in vi/vim

Macros are a powerful tool in vi/vim that allow you to record a sequence of commands and replay them as needed. To start recording a macro, enter the command mode by pressing Esc, then type q followed by a letter to name the macro (e.g., q a to name it a). Then enter the sequence of commands you want to record.

To stop recording the macro, enter the command mode again and type q. To replay the macro, enter the command mode and type @ followed by the name of the macro (e.g., @a to replay macro a). You can also replay a macro multiple times by specifying a number before the @ command (e.g., 3@a to replay macro a three times).

  1. Creating and using custom commands in vi/vim

Vi/vim allows you to create custom commands to automate repetitive tasks or simplify complex operations. To create a custom command, enter the command mode by pressing Esc, then type : followed by the name of the command and the sequence of commands to execute. For example, to create a command named mycmd that deletes all blank lines, you could type :command MyCmd g/^$/d.

Once you've created a custom command, you can use it like any other vi/vim command. For example, to run the mycmd command, enter the command mode and type :mycmd.

  1. Using plugins in vi/vim

Vi/vim also supports plugins, which are extensions that add new functionality to the editor. There are many plugins available for vi/vim, ranging from simple tools to improve text editing to more complex tools that integrate with external systems.

To use a plugin in vi/vim, you'll need to first install it. Many plugins can be installed using a package manager like Vundle or Pathogen. Once the plugin is installed, you can enable it by adding a configuration line to your .vimrc file. For example, to enable the nerdcommenter plugin, you would add the line Plugin scrooloose/nerdcommenter to your .vimrc file.

After the plugin is installed and enabled, you can use its functionality by entering the appropriate commands. For example, the nerdcommenter plugin allows you to easily comment and uncomment lines of code by pressing a key combination.

By mastering these advanced features of vi/vim, you can streamline your editing workflow and work more efficiently with text files.


In this article, we've covered the basics of using vi/vim, including installation, opening the editor, moving around, editing text, working with multiple files, searching and replacing, and some of the more advanced features such as macros, custom commands, and plugins.

Vi/vim can seem intimidating at first, but with practice, it can become a powerful tool for editing text files quickly and efficiently. By mastering the basic commands and becoming familiar with the editor's features, you can speed up your workflow and accomplish more in less time.

One of the benefits of using vi/vim is that it is available on almost every Unix-based system, making it a reliable tool for editing files across different platforms.

If you're looking to learn more about vi/vim, there are many resources available online. Some popular resources include the Vim documentation, which is available online or can be accessed within the editor by typing :help, and the Vim subreddit, which is a community of Vim users who share tips, tricks, and advice.

In conclusion, while there is a learning curve when it comes to vi/vim, it is a powerful and versatile tool that can be used for a wide variety of text editing tasks. With practice and patience, you can become proficient in using vi/vim and streamline your text editing workflow.

Enjoying our content? Your support keeps us going! 🚀

Consider buying us a coffee to help fuel our creativity.