Tuesday, January 12, 2021

Vim - Normal Mode

When learning Vim, it's important to understand its modes. Normal mode is the most common and the one you're expected to spend most of your time
Photo by Alex Knight on Unsplash

Once you installed Vim and understood the basics of Vim Modes, you probably understand that Vim has various modes. What's called the Normal Mode is Vim's default mode. On this tutorial we will understand more about it and how to use it effectively.

Normal Mode 101

Would you be able to precise how long you read a file and how long you modify it? I hope you agree that at this point in your Vim journey, unless you're not creating brand new content, you spend way more time reading and moving through the file than modifying it.

And that's exactly why the Normal mode exists and why it's Vim's default mode. Because 99% of the time you spend more time reading (and moving) your cursor through a file, it exists for you to interact with your files and with Vim and is where you should operate most of the time.

Getting to Normal Mode

So how to get to Normal mode? If you're in one of Vim's other modes, to get back to normal mode, press <Esc> or <Ctrl-C> or <Ctrl-[>.

Common Commands

Below are some of the most common commands you'll use in Normal mode.

Moving the cursor

  • h - move the cursor one character left
  • j - move the cursor one line down
  • k - move the cursor one line up
  • l - move the cursor one character right
  • 0 - move to the beginning of the line
  • $ - move to the end of the line 
  • w - move the cursor to beginning of next word
  • b - move the cursor to previous beginning of word
  • gg - move to the beginning of the document
  • G - move to the end of the document
  • Ctrl-D - move half a page down
  • Ctrl-U - move half a page up
  • :<num> - move the cursor to line <num>. Ex, :133 moves the cursor to line 133 in the current file

Undoing

  • u - undo previous change
  • Ctrl-r - redo previous change

Running Commands

  • : - opens the command mode (where you can type commands, for example to quit Vim, save or open a file)

Modifying text

  • x- deletes the next character
  • X - deletes the previous character
  • D - delete from the cursor to the end of the line
  • S - delete the whole line and set in insert mode

Searching

By typing / you open the search mode. We will study it better in a future tutorial

Marks

  • ma - creates a mark a
  • `a - moves the cursor to mark a

If all of the above sounds a lot, fear not! Keep following this blog as we will gradually guide you to understand, organically memorize (and love!) how Vim works. 

Running a single Normal Mode Command

There's one more interesting thing to mention about the Normal mode. It may happen that you just want to run one command in Normal mode and getting back to insert mode. Vim also gets you covered with Ctrl-O. For example, to delete from the cursor to the rest of the line, do:

Ctrl-O D

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 continued in our Vim journey by learning a bit more about Vim's Normal mode. 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