Tuesday, December 15, 2020

Vim Modes - All you need to know to get started

When learning Vim, it's important to understand well its Modes, one of Vim's most fundamental concepts.
Photo by Alex Knight on Unsplash

On a previous post we learned about three of the most important foundational concepts of Vim: Commands, Buffers, Modes and Motions. We also recently reviewed how to install Vim on MacsWindows or Linux, learned how to get started with Vim and mastered your first Vim tutorial

Today we will continue our discussion on modes: what they are, what they serve and how to use them.

What are modes in Vim?

Modes are another feature that differentiates Vim from traditional text editors and something that's important to comprehend. Modes are essentially different ways (or modes) Vim can work on. To master (and learn well) Vim it's important to understand not only which they are but how they differ and how they can you help you get your work done.

The most common Modes

Vim has seven basic modes The most common modes are:

  • Normal: Vim's primary mode and the one we use to run commands:
  • Insert: the mode where you edit the contents of your files
  • Visual: visual selection of contents
  • Replace: allows you to type over the text, replacing it
  • Command-line: allows you to execute commands via its own command-line prompt
  • Terminal-Job: allows you to interact with a job in a terminal window
  • Ex: in this mode Vim emulates the Ex editor and is used mainly for batch processing
  • Select: like the visual mode but with more CUA like behavior.

Using the Modes

So let's see some examples in how can you use these modes.

Normal Mode

In normal mode you're not supposed to type text but to send commands to Vim. Some of them are:

  • h - move one character left
  • j - move one line down
  • k - move one line up
  • l - move one character right
  • 0 - move to the beginning of the line
  • $ - move to the end of the line 
  • w move to beginning of next word
  • b move to previous beginning of word
  • gg - move to the beginning of the document
  • G - move to the end of the document
  • u - undo previous change
  • Ctrl-r: redo previous change
  • : - go to ex mode (where you can type commands, for example to quit Vim, save or open a file)
  • ma - creates a mark a
  • `a - moves the cursor to mark a
  • and more, much more
Don't worry, we'll study normal modes in the future

Visual Mode

Press v in normal mode to enter visual mode, then move the arrow with h/j/k/l or with other commands (see above for examples). After you're comfortable with the selection, you can either type y to yank (copy) the text, x to delete it of any other command available in that mode.

We'll study more about the normal mode in the future.

Command Mode

It's also possible to run commands in Vim. That mode is called Command mode (or command-line mote) and to get to it type : while in normal mode. For example, the command below would run in command mode and would replace all occurrences of the word hello by world in the current buffer:

:%s/hello/world/g

Visual Mode

To use the visual mode (similar to the select feature in your text editor can be accomplished by pressing the following keys in normal mode:

  • v - start visual mode from the current character
  • V - start visual mode with line selection

Switching between modes

The last thing is how to switch between modes. The traditional workflow is to press <Esc> to go back to normal mode then press a keystroke to enter the other mode. Some of the keystrokes you can use to switch between modes are:

  • <Esc> - goes back to normal mode
  • i - enters insert mode
  • : - enters command mode
  • v - enters visual mode
  • R - enters command-line mode
  • V - enters visual mode (line selection)
  • <Ctrl-v>: enters visual block mode

In which mode am I?

And how do we know in which mode we are? Assuming you're not running any plugins, Vim should show you that in the bottom of the screen:

  • Normal Mode: blank (no info)
  • Insert Mode: -- INSERT --
  • Replace Mode: -- REPLACE --
  • Visual: -- VISUAL LINE --
  • and so on
For example, in insert mode you should see something like:

If that info is not available, try resetting the showmode config with:

:set showmode

Fore more info about that configuration, type:

:h showmode

Mode-specific help

Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:

What Prepend Example
Normal mode command :help x
Visual mode command v_ :help v_u
Insert mode command i_ :help i_<Esc>
Command-line command : :help :quit
Command-line editing c_ :help c_<Del>
Vim command argument - :help -r
Option ' :help 'textwidth'
Regular expression / :help /[

We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.

Conclusion

On this post we learned the most important modes, what they are, what they serve and how to use them.

If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim. Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

See Also

Any comment about this page? Please contact us on Twitter

Featured Article

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the  Ex Mode  where you can continually type your commands u...

Popular Posts