Get the most out of the Vi text editor | Opensource.com

Get the most out of the Vi text editor

Vi is the quintessential Unix text editor. Get to know it—or any of its incarnations, Vim, Neovim, gVim, nvi, or Elvis, for Linux, macOS, Windows, or BSD.

Business woman on laptop sitting in front of window
Image credits : 

Image by Mapbox Uncharted ERG, CC-BY 3.0 US

x

Subscribe now

Get the highlights in your inbox every week.

Whether you know it as Vim, Neovim, gVim, nvi, or even Elvis, the quintessential Unix editor is easily Vi. Included in probably every Linux and BSD distribution, Vi is a lightweight and minimalist text editor that many users love for its simple and succinct keyboard shortcuts and dual-mode design.

The original Vi editor was an application written by Bill Joy, creator of the C shell. Modern incarnations of Vi have added many features, including multiple levels of undo, better navigation while in insert mode, line folding, syntax highlighting, plugin support, and much more. Vim is regarded as the most popular modern implementation, and most people actually mean Vim when they refer to Vi.

All incarnations hearken back to the same goal, though, so this article looks at Vi in a generic sense. The implementation on your computer may differ slightly, but you can still benefit from editing text the Vi way.

Install Vi

If you're running Linux, macOS, or BSD, then you already have the vi command installed. If you're on Windows, you can download Vim and gVim.

gvim.jpg

gVim

(Seth Kenlon, CC BY-SA 4.0)

On NetBSD, nvi is a common alternative to Vi, while Slackware provides Elvis (and Vim), and the popular Neovim fork aims to help users extend Vim with Lua.

Launch Vi

Start Vi or Vim with the vi command in a terminal. If a .vimrc file is not found on your system, then Vim starts in Vi-compatibility mode (this can also be forced with the -C option). If you want to use gVim to have a graphical user interface (GUI), you can start it from your desktop's application menu.

If you're a new user just learning Vi, using a graphical user interface can be a nice way to provide yourself a buffer between how you might expect a text editor to behave and how Vi was designed to behave. The GUI version has a menu bar, some mouse integration, a toolbar, and other features to help you find the basic functions you probably take for granted in a typical text editor but don't know how to do in Vi yet.

How to use Vi

Probably the easiest way to learn Vi is with vimtutor, an interactive tutorial packaged with Vim. To start the tutorial, launch vimtutor and read through the instructions, trying each exercise. As the tutorial says, getting good with Vi is less about memorizing what key does what and more about establishing muscle memory to invoke common actions as you type.

Escape

One of the first things you learn about Vi is the importance of the Esc key. Esc is what activates command mode, and it doesn't take long to learn that whenever you're in doubt in Vi, just press Esc. Any key you press while in command mode is not entered into the text document you're working on; instead, it is interpreted by Vi as a command. For instance, to move your cursor left, you press the H key on your keyboard. If you're in insert mode, then pressing H types the letter H, just as you'd expect. But in command mode, pressing H moves left, L moves right, J moves down, and K moves up.

The separation between command mode and insert mode is a sharp contrast to the way any other text editor works, and for that reason, it's probably Vi's most significant differentiator. Interestingly, though, it's theoretically not so different from the way you probably already work. After all, when you take your hands off the keyboard to select text with a mouse, you're essentially placing yourself into a kind of command mode. With Vi, instead of moving your hands off the keyboard to move the mouse and press function keys or Ctrl, you put the editor into a special mode of operation, such that it reassigns your key presses to commands instead of text input.

Extend Vi

Before Vim version 8.0, Vi was very much "just" a text editor. There were plugins for it, but installing them was a manual process that many users never thought to do. Luckily, Vim version 8 and higher offer support for plugin management, making it trivial to install and load plugins.

Installing plugins for Vim can be done with the vim-plug function. For instance, to install the Vi file browser NERDTree:

:PlugInstall NERDTree

You can also update plugins:

:PlugUpdate NERDTree

For more information on installing plugins and themes, both with vim-plug and manually, read my article How to install Vim plugins.

Vi as default

Vi isn't just popular; it's a POSIX standard. It's an application every sysadmin should know how to use, even if they don't intend to use it on an everyday basis. It's also a fast and simple editor, so once you get good at it, it may be the editor you've long been searching for.

Computer keyboard typing

Kick your text editor up a notch with a few fun tricks.
Emoji keyboard

Emacs isn't a mere text editor; it places you in control and allows you to solve nearly any problem you encounter.

About the author

Seth Kenlon
Seth Kenlon - Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time. He is one of the maintainers of the Slackware-based multimedia production project Slackermedia.