Vim in Vs code | Must Know Vim Commands

Murali mahadeva B S
7 min readMar 9, 2021

If you are wondering something like this, then… 😅

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” with most UNIX systems and with Apple OS X. Vim’s interface is not based on menus or icons but on commands given in a text user interface.

If no… You must have heard about or started using it but couldn’t continue.

Here is a simple guide for you to get started with Vim easily.

Why use Vim?

When you have all the cool graphical editors out there with customizable themes and icons why would anyone want to use, boring text interface 🤨.

That’s a reasonable point, but Vim makes your editing more easier and accurate without moving your hands off the keyboard.

Its hard getting into the world of Vim, but once you are here you wont ever leave again 😎.

Vim is rock stable and is continuously being developed to become even better. Among its features are:

  • persistent, multi-level undo tree
  • extensive plugin system
  • support for hundreds of programming languages and file formats
  • powerful search and replace
  • integrates with many tools

Use case of Vim

Vim is just an editor. You can use it to do just any text editing or for software development. Vim is not a magic tool, you use it and your productivity will skyrocket 🚀. Its not like that, it takes quite a lot of time to get used to Vim. And you wont be able to use mouse for anything as in a regular text editor. Its just all keyboard. All keyboard… ⌨️.

Don’t get discouraged, I have an amazing option to make use of both amazing features of modern editors and Vim.

Its an investment, You wont regret once you get used to Vim. And above all its cool.

Vim in Visual Studio Code

If you are a developer, most likely you will be using Visual Studio Code. It offers all the features of a modern editor. Install vim plugin for vs code from extensions manager(Ctrl+Shift+x). Lets get started with… with the commands.

Vim extension for vs code

Many struggle exiting Vim. In vs code you wont need this but if you are working in the actual vim, then, click escape to enter command mode and type

  • “:q” to exit the editor without saving the changes.
  • “:wq” to exit the editor saving the changes.
Vim plus Vs code

1. Modes

You need to understand the different modes in Vim first. There are 3 modes. Visual mode, Inset mode and Normal mode.

  • In Visual mode you can do select operation, as in with mouse selecting a whole block of code.
  • In Insert mode, you can add and edit the content.
  • In Normal mode, you can navigate and and edit the content(like copy, delete ).

You can see the mode you are in at the bottom of the editor.

Normal mode depiction in vs code
  1. Click escape to get into normal mode.
  2. Click “i” to get into insert mode.
  3. Click “v” to get into visual mode.

Its easy to remember, all the commands in Vim are like talking to the editor.

2. Navigation:

We wont be using any mouse here. You can use it but lets explore how to do it Vim way.

  • “h” to move left by one character.
  • “j” to move down by one line.
  • “k” to move up by one line.
  • “l” to move right one character.
  • “w” to move forward by a word.
  • “b” to move backward by a word.
  • “gg” to go the top of the file.
  • G” to go to the bottom of the file.
  • “Ctrl + d” to move down by 16 lines.
  • “Ctrl + u” to move up by 16 lines.
  • “{“ to move backwards by a block .
  • “}” to move forward by a block.
  • “:26” to move to the 26th line of the file.

Mostly I use:

  1. “j”, “k” to move up and down.
  2. “gg”, “G” to move to the top and bottom of the file.
  3. “w”, “b” to move by a word.
  4. “:26” to move to a particular line number.

3. Insert mode

Insert mode is where you play a lot.

  • “a” to insert the cursor after the current character.
  • “i” to insert the cursor before the current character.
  • “A” inset at the end of the line.
  • “I” insert at the beginning of the line.

Mostly I use:

  1. “a” and “i” to insert before and after the character.

You can use backspace to clear out the characters in insert mode.

4. Visual mode:

This is where you select and then perform copy or delete operations.

  • “v” to getting into visual mode.
  • “V” to select the entire line.

Combining keys:

  • “V+j” to select the entire line and move down selecting.
  • “V+k” to select the entire line and move up selecting.
  • “viw” to select the word under the cursor.
  • “vi(“ to select the contents within a parenthesis. You can use any bracket or quotes in place of the parenthesis. Example vi{, vi”, vi[, etc.

Mostly I use:

  • “V+j” and “V+k” to select entire line and up or down.
  • “vi(“ to select within a bracket or quotes.

5. Copy and paste:

You can do copy paste in normal mode or in visual mode.

  • “yy” to copy the whole line.
  • “Y” to copy the whole line.
  • “p” to paste after the cursor.(small p)
  • “P” to paste before the cursor.(capital p)

You can paste as many times as you want by hitting p. Once copied will be stored in clipboard. “y” means yank. Easy to remember.

Things you copied using “yy” cannot be pasted into another editor. It is restricted only to that editor. If you want to copy something to paste elsewhere use “Ctrl + C” or “Cmd + C”. And use “Ctrl + Shift + v” or “Cmd + Shist + v” to paste.

6. Delete, Undo and Rename:

  • “dd” to delete the whole line.
  • “D” to delete everything after the cursor.
  • “x” to delete a single character under the cursor.
  • “u” to undo.
  • “Ctrl + r” to redo.
  • “r” to rename character under the cursor.

Tip: Once deleted it’ll be stored in clipboard. You can paste it if you want to using “p”.

Mostly I use:

  • “dd” to delete the whole line.
  • “x” to delete a single character.
  • “u” to undo.
  • “r” to rename a single character under the cursor.

7. Search and Find

I use search very often. Search is very importing in locating a variables existence. Type “/” to enter into search mode from normal mode. You cannot do this in any other mode.

  • “/searchWord” replace searchWord with whatever you want to search and then hit enter.
  • “n” to go to next instance of the search result.
  • “N” to go to the next instance in the backward direction.

You can find and move to that character within a line using “f” and “F”.

  • “f” followed by a character to move the cursor to the first instance of that character in forward direction. Example, “fw” will move to the first instance of w in that line in forward direction.

Tip: “#” to move to the next instance of the word under cursor without search.

8. Combining keys

By combining keys we can harvest the true power of Vim 💪.

Mostly I use:

  • “ciw” to delete the word under the cursor and getting into insert mode (Change In Word).
  • “cw” to delete the word after the cursor and getting into insert mode (Change word).
  • “dw” to delete the word under the cursor (Delete Word).
  • “yiw” to copy the word under the cursor (Yank In Word).
  • “viw” to select the word under the cursor (View In Word).
  • “vi(“ to select the contents within the parenthesis. You can use any bracket or quote in place of the parenthesis.
  • “o” to getting into insert mode in the next line.
  • “O” to getting into insert mode in the previous line.
  • “>” and “<” to indent forward and backward. You can indent a selection using “V” to select multiple.
  • “$” to get to the ending of the line.
  • “0” to get to the beginning of the line (zero).
  • “A” to get into inset mode at the end of line.
  • “I” to get into insert mode at the beginning of the line.

Mostly you will go wrong in picking up with the different modes. Don’t forget to check the bottom of the editor to know your mode. Or just hit escape multiple times to get back to Normal mode and start again.

⚠️ Don’t touch your mouse. It’ll be very frustrating in the beginning but stay there it’ll reward you later.

Conclusion

Vim key bindings are amazing. You can find your own way of doing things. There are tons of ways to do the same thing. Start exploring now. For you to get started and working with Vim these key bindings are more than enough.

Watch Vim coding sessions to get new ways of doing things. Hope I gave a magic stone to your development career. If this helped you in some way consider leaving a clap. Till then 👋 🚶

Be Curious… Be Creative…

--

--