Book Image

Mastering Vim

By : Ruslan Osipov
Book Image

Mastering Vim

By: Ruslan Osipov

Overview of this book

Vim is a ubiquitous text editor that can be used for all programming languages. It has an extensive plugin system and integrates with many tools. Vim offers an extensible and customizable development environment for programmers, making it one of the most popular text editors in the world. Mastering Vim begins with explaining how the Vim editor will help you build applications efficiently. With the fundamentals of Vim, you will be taken through the Vim philosophy. As you make your way through the chapters, you will learn about advanced movement, text operations, and how Vim can be used as a Python (or any other language for that matter) IDE. The book will then cover essential tasks, such as refactoring, debugging, building, testing, and working with a version control system, as well as plugin configuration and management. In the concluding chapters, you will be introduced to additional mindset guidelines, learn to personalize your Vim experience, and go above and beyond with Vimscript. By the end of this book, you will be sufficiently confident to make Vim (or its fork, Neovim) your first choice when writing applications in Python and other programming languages.
Table of Contents (12 chapters)

Making simple edits in insert mode

When working with Vim, you usually want to spend as little time as possible in the insert mode (unless you're writing and not editing). Since most text operations involve editing, we'll focus on that.

You've already learned to enter the insert mode by pressing i. There are more ways to get to the insert mode. Often times you will want to change some piece of text for another one, and there's a command just for that c. The change command allows you to remove a portion of text and immediately enter an insert mode. Change is a compound command, meaning that it needs to be followed by a command which tells Vim what needs to be changed. You can combine it with any of the movement commands you've learned before. Here are some examples:

Command Before After
cw
c3e (comma counts as a word)
cb
c4l
cW
As an odd exception, cw behaves like ce. This is a leftover from Vi, Vim's predecessor.

As you learn more complex movements commands, you can combine these with a change for quick and seamless editing. We'll also be covering a few plugins which will supercharge a change command to allow for even more powerful editing, like changing text within braces, or replacing the type of quotes on the go.

All of these examples follow the <command> <number> <movement or a text object> structure. You can put a number before or after the <command>.

For example, if you wish to change farm = add_animal(farm, animal) to farm = add_animal(farm, creature), you can execute the following set of commands:

Contents of the line Action
Start with a cursor in the beginning of the line
Hit 3W to move the cursor three WORDs forward to the beginning of animal
Press cw to delete the word animal and enter the insert mode
Type creature

Hit the Esc key to return to NORMAL mode

Sometimes we just want to cut things, without putting anything instead, and d does just that. It stands for delete. It behaves similarly to c, except that the behavior of w and e is more standard, as can be seen in the following example:

Command Before After
dw
d3e (comma counts as a word)
db
d4l
dW

There are also two more nifty shortcuts which allow you to change or delete a whole line:

Command What it does
cc Clears the whole line and enters insert mode. Preserves current indentation level, which is useful when coding.
dd Deletes an entire line.

For example, look at the following piece:

By hitting dd you will completely remove a line, as demonstrated in the following example:

Hitting cc will clear the line and enter insert mode with the proper indent, as shown in the following example:

If you run into difficulties picking the right movement commands, you can also use the visual mode to select text you want to modify. Hit v to enter the visual mode and use the usual movement commands to adjust the selection. Run the desired command (like c to change or d to delete) once you're satisfied with the selection.