VIM — Console Editor: An Introduction
Greetings!

This is the first post in a series about the console editor Vim. Sooner or later this had to happen) after all, Vim is an incredibly popular editor that often comes “out of the box” in most Linux distributions.

Preface

Vim (formerly Vi) is a powerful command-line text editor that provides extensive capabilities for editing text files. In this article we’ll get a bit familiar with the basics of using it.

Personally, I ignored this editor for a long time, because I thought it was made for reptilians/hardcore IT people, until I became a reptilian myself I got to know it better. In the end, getting acquainted turned into detailed study. Especially when I realized that Vim and its derivatives such as Neovim are a brilliantly thought-out tool for an IT specialist who often works with the Linux command line, and not only Linux.

Vi/Vim commands and features have existed for more than fifty years, starting with the QED editor created in 1966. Over this time, the editor has absorbed the best practices of working with text and acquired everything necessary for convenient keyboard-based work. If you’re interested, I recommend reading part of the article on Habr about the history of this editor: The History of Vim and a Guide to Using It Effectively.

Excerpt from William Shotts’s book ‘The Linux Command Line’ about the Vi editor

Why, in a modern world of editors with graphical interfaces and easy-to-use text-interface editors like nano, should one learn vi? There are three good reasons: vi is always at hand. It can come to the rescue on systems that lack a graphical interface, for example on a remote server or on a local system with a broken X configuration. The nano editor, although extremely popular, is still not versatile enough. POSIX, the Unix system compatibility standard, requires vi to be present on them. vi is lightweight and fast. For many tasks it’s much easier to launch vi than to find a graphical editor in a menu and wait for several megabytes to load into memory. Besides, vi was specifically designed for fast keyboard input. As will be shown below, an experienced vi user never takes their hands off the keyboard while editing. We don’t want other Linux and Unix users to think we’re softies. Fine, let it be two good reasons.

In the process of studying it, I “realized” how flexible this tool is. To understand the whole point: for configuring the Vim editor, the developers created their own scripting language — vimscript. And if you pay attention to more modern forks of Vim, for example Neovim, there the editor can be configured using the Lua programming language through an API mechanism. I think it goes without saying that with such an approach, the possibilities for extending the editor’s functionality go through the roof)

This editor can also be incredibly beautiful; for example, it can be configured like this:

But more on that in other articles) For now, let’s start getting acquainted.

The actions described below were carried out in the Linux Mint 21 distribution environment. Here are a few of my past materials on this distribution:

Installing Vim on Linux Mint

Out of the box, Linux Mint already has Vim installed, but only its lightweight version, vim.tiny.

vim.tiny is a stripped-down (or minimal) version of the Vim editor, intended for systems with limited resources, such as embedded systems or small Linux distributions. It provides the core functionality of Vim, but may not include some of the advanced features and plugins available in the full version of Vim.

Below you can see that vim.tiny on Linux Mint hides behind the vi symlink.

Let’s install the full version of Vim on our system:

BASH
sudo apt update && sudo apt install -y vim
Click to expand and view more

After installation, the Vim editor can be launched both from the console and from the Linux Mint main menu. During Vim’s installation, a .desktop file is created to launch the editor in a separate terminal window, as a standalone application.

To launch the editor, run the command of the same name in the terminal:

BASH
vim
Click to expand and view more

You’ll see a window like this:

Here the developers thoughtfully tell us how to exit Vim) — type (in an English layout) :q in sequence

Let’s check

VIM
:q
Click to expand and view more

The colon : here activates the editor’s command line, and q (quit) is the command that means exit the editor.

You can also exit Vim by pressing ZQ, i.e., while holding Shift, press the keys z and q in sequence — this exits the editor without saving the file. To exit with saving, press ZZ in the same way.

To open an existing file for editing, simply pass the vim command an argument with the name of the file and, if necessary, the path to it.

If you pass the name of a non-existent file, after saving it inside the editor, such a file will appear on disk.

For example, let’s open the file file.txt for editing in the Документы (Documents) directory of our home (~) folder:

BASH
vim ~/Документы/file.txt
Click to expand and view more

At the bottom of the window we’ll see the name of the document being edited:

The first thing beginners run into is the question of how to start editing the document. Of course, it’s not that simple) Here we need to talk about the modes of operation in Vim.

To call up help, press F1 or press the keys :help Enter in sequence

Vim modes of operation

When Vim is opened, the default mode is command mode.

Command mode (normal mode)

In this mode, pressing keys or their combinations is interpreted by the editor as commands.

For example, the : character is a command that calls up the editor’s console.

Below I’ll give the command-mode key commands I use most often.

An uppercase letter implies pressing it together with the Shift key, for example O = Shift + o.

The cursor is the blinking white block character that marks the current position in the file’s structure.

The clipboard in Vim is called a register. It’s a separate, flexible mechanism.

List of popular command mode keys:

Try practicing this.

I want to separately note the mechanism where you can specify how many times to repeat a command before the command itself. For example, if you type the number 15 and then press the V key — you’ll select 15 lines following the cursor:

Other commands work similarly, for example movement with the w, b keys, or deletion with d, x, etc.

Insert mode

The popular ways to switch to insert mode are listed above. Most often it’s the i key. Switching to insert mode is indicated by the message “ВСТАВКА” or “INSERT” at the bottom of the editor:

This is the classic text editing mode, where cursor movement with the arrow keys is supported, as well as some key combinations.

List of popular insert mode keys:

Selection mode (visual mode)

It’s precisely because of the appearance, long ago, of the visual mode (visual) in the editor that it got its name — vi/vim.

Visual mode is text selection mode. You switch to this mode with the v key. After pressing it, you can select text:

After selecting, the text can, for example, be copied by pressing y, deleted by pressing d, Del, x, etc. Then, for example, pasted with the p or P keys.

Pressing the v key again, or pressing Esc, exits this mode.

List of popular visual mode keys and combinations:

A rather comprehensive and useful image about navigation in Vim:

Other basic things

I’ll briefly talk about several frequently used procedures when working with the editor.

Open a new file in the current window

To open a new file in the current window, run the console command:

VIM
:open <имя_файла>
Click to expand and view more

Note that after running the :open/:edit commands, your previous file isn’t closed — it’s in the session buffer. To view the list of sessions in the buffer, run the console command:

VIM
:buffers
Click to expand and view more

To switch to the desired session, run:

VIM
:buffer <номер_сессии>
Click to expand and view more

or

VIM
:buffer <имя_файла>
Click to expand and view more

Don’t forget that the Vim console supports interactive completion, invoked with the Tab key:

Honestly, I only learned about the session buffer mechanism and switching between them not that long ago)) Before that, I would just close the editor and reopen the needed file again) well, as they say — live and learn Vim)

Open a file in a new tab

To open a file in a new tab, run:

VIM
:tabnew <имя_файла>
Click to expand and view more

To move between tabs, use movement commands such as gt (go tab). For example, to move to tab #1 in command mode, type: 1gt, where 1 is the tab number.

Split the window

Call up the editor’s command line : and enter split or split <имя_файла> for a horizontal window split, and vsplit or vsplit <имя_файла> for a vertical one. You can specify an existing file as the file name, otherwise Vim will open an empty window for a new file.

To move the cursor between windows, press Ctrl + w and then the desired direction with the arrow keys.

Searching content

To enter search mode, from command mode press the / key and then type the key you’re looking for. The found value will be highlighted. To move the cursor to the next occurrence, press the n key; to search backward, press N (Shift + n).

Note that by default, search is case-sensitive. To temporarily disable case sensitivity, type:

VIM
:set ignorecase
Click to expand and view more

Search and replace

This procedure in Vim is quite flexible. You can process specific occurrences in a line, a block, or across the whole file.

Let’s look only at the case of a global replace across the whole file for a given key. To do this you need to:

Where:

Enable line numbering

To enable the display of line numbers, type from the keyboard:

VIM
:set number
Click to expand and view more

Note that line numbering will be displayed until you exit Vim. For it to persist, you need to set this option in the configuration file. But config files are beyond the scope of this article. There will be separate instructions on this topic.

Insert content from another file

To insert into the current window the contents of another file, call up the editor’s console :, then type read <имя_файла>. If you specify a file name without a path, it implies using a file from the current directory. Also note that file names can be tab-completed for substitution. By the way, instead of read you can just use the character r.

Running external shell commands

You can run external commands of our shell from the editor’s console. To do this, in the editor’s console, specify the ! character as the first character and then type the command. For example, !ls -l:

The output of the command will be displayed in a separate window. To return to the editor, press Enter.

And so, the list of the editor’s features and tricks could go on forever) so there will be more than one post about configuring Vim/Neovim)

Configuration files

Vim, like a lot of software on Linux and similar OSes, has configuration files, which are a separate, extensive topic) As mentioned at the beginning, the developers created a separate scripting language for configuring the editor)

Standard config file paths:

About Neovim

Why do I mention Neovim alongside Vim? The thing is, Vim has been developed for quite a long time, and because of its large codebase and lots of legacy code, it’s becoming increasingly difficult to extend its functionality. As a result, the Neovim project emerged, aiming to:

That’s why my future materials will most often be about Neovim.

What Vim/Neovim can look like

Well, here are a few screenshots of my current Neovim config and a few taken from the internet.

Mine:

VSCode-style)

Reddit users:

Author: Oreki-H

Author: gquittet

Author: micro-aries

Author: unknown

Thanks for reading! Good luck conquering the Vim universe)

Useful sources

My other articles on these topics:

Copyright Notice

Author: Ivan Cherniy

Link: https://r4ven.me/en/software/vim-konsolnyj-redaktor-znakomstvo/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut