Vim basics(part 1): Modes and Commands

mvoto

Mauricio Voto

Posted on June 21, 2018

Vim basics(part 1): Modes and Commands

Introduction

Developers like new things and Vim is definitely a totally new and different thing if you are used to editors like Sublime, Atom, Text Mate or VSCode. Seems difficult because is new. Everything that is new for us tends to be difficult. So, let's start with a plan: keep it short, keep it simple, keep it going. It means that the plan is to constantly improve vim knowledge by getting new tips, using it a lot and then master it. Will try to follow that with this Vim series.

Advices

Getting started with Vim involves some good advices:

  • Avoid frustration, by not using it when you need speed, keep in mind that you won't get same efficiency as your regular editor for a while. It's similar to learning a new language or framework and if you keep expectations high it will be frustrating.
  • No mouse allowed, neither arrow keys for navigation on Normal mode.
  • Remind yourself that you will benefit soon from the hard times you may find at the beginning.

Modes

Learning about vim modes:

Normal: is the "initial" mode, where you start(unless you changed configs), can navigate and use all edit commands, including the plugins ones.

Insert: is the mode that you use for editing the file text itself(there are a few keys to enter this mode, for this article let's assume the i, that takes you to the Insert mode exactly where your cursor is at that moment).

Visual: it is basically selecting text, so you can manipulate it(usually v enters this mode).

When you are not on Normal mode, you can press Esc to switch back to it. And if you want to exit vim, on Normal mode, you can use :q. Now that you know how to exit vim, this article is done, see you next time. Just kidding, lol.

Commands

When inside the Normal mode, you can also modify the content by using some commands, all you have to do is navigate into the content you want to change(we'll talk about navigation in another article from this series). A very basic but useful example is how to edit code inside quotes or parenthesis. Let's say we have something like this string:

'VisualCode is awesome !'

But we want to edit the text inside the quotes, so we perform a command: Ci'
That would result into: '' and then you can type whatever you want: 'Vim is awesome !'.
Explaining that command:

  • C: for Change
  • i: for inside
  • ': for whatever is enclosing the content you want to modify

You could use D instead of C, the difference is that D just deletes and keep you at the Normal mode when C does exactly the same thing, but gets you into the Insert mode, so you can write something right away.

Now let's say you want to duplicate a line:
'Vim is awesome !'
Then you just type(with cursor at any point of the line): YP
That would result into:


'Vim is awesome !'
'Vim is awesome !'

Explaining the command:

  • Y: Yank whole line(if we use it on downcase would perform for whatever you specify next in command, w for word as example)
  • p: paste after cursor(if we use it as capital case it would paste what was yanked before the cursor)

Conclusion

To stick with the plan, let's keep it short and simple.
You can now start with your vim by switching modes and trying these two basic commands. Practice a lot and stay tuned for a next posts, where we'll probably cover navigation and more cool commands. Until there, some references to look at:

💖 💪 🙅 🚩
mvoto
Mauricio Voto

Posted on June 21, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related