DEV Community

Mauricio Voto
Mauricio Voto

Posted on

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:

Top comments (3)

Collapse
 
stevensonmt profile image
stevensonmt

I switched to Vim a few months ago after some issues with Atom. Growing to love it. When getting started I installed Janus thinking it would be a shortcut to an efficient setup, but I have found this was probably a mistake. I would encourage people to simply use Vundle or Pathogen to install only the plugins they need. My recommended plugins are ALE, delimitMate, indentline, vim-autoformat, vim-closetag, vim-commentary, and vim-polyglot.

Collapse
 
mvoto profile image
Mauricio Voto

That's nice stevensonmt, thanks for the shout out on recommended plugins !
I am preparing another post to talk also about plugins and configuration, I am currently using Vundle and it's just perfect for my needs.
How do you feel about productivity after switching from Atom to Vim ?

Collapse
 
stevensonmt profile image
stevensonmt

Productivity at this point is at least on par and probably improved. I still forget to switch modes at times and just start typing, then have to undo whatever commands I accidentally sent. Other than that my ability to perform batch edits like finding regex patterns and substituting another pattern is much quicker.

On plugins I can't believe I forgot NERDTree. Essential, really. I almost think it should be included in the default configuration of vim.