vim
Shivan

Shivan

How I got started with Vim and become more productive while coding

Print Friendly, PDF & Email

Vim is a text editor that can greatly increase your productivity when you are coding. Typing speed is irrelevant up to a certain level. Your ability to navigate through code is much more important. This is wear Vim along with its keybindings, layout and set up can help you speed up the process.

In the beginning of using Vim you may be losing time. But don’t lose hope. This is the case of paying a small upfront cost, which will pay dividends later down the track.

Here is a post to help you get you started, or maybe to look into Vim as your chosen text editor.

What is Vim?

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient.

From the Vim webpage

Vim is a stable and open-source software and it supports many different programming languages including Python and Bash.

First opening Vim

When first opening Vim we are met with a pleasant welcome

When you first open Vim, you are in command mode. The first thing to learn is how to exit, which can be done by typing :q. Using :q! with the additional ! means you quit without saving.

When you open you are initially you are in command mode. To move the cursor:

  • h left
  • j down
  • k up
  • l right

You can put a number in front of commands and it will go the command that number of times. (e.g. 20h will take your cursor to the left 20 times).

One of the first things to learn it how to exit Vim. This can be done:

  • :q This is the most important being able to quit, following this with an ! such as :q! allows you to quit without saving.
  • :w will save your created file.
  • :wq will save and quit in one commands.
  • :x will save and quit as well.

To actually get started typing some words of writing some code. You use:

  • i puts you in insert mode.

More common keys for navigation

Back in command mode, here are some more keys to help with navigation:

  • We already know that h, j, k, and l allow us to move the cursor around.

Moving to start and end:

  • G (note that this shift+g) takes you to the start of the bottom line.
  • gg takes you to the top of the code.

Navigating on a line:

  • 0 will take you to the start of a line; using this in combination with w (so 0w) will take you to the first word of the line.
  • A (or shift+a) will take you to the end of the current line and into insert mod
  • b moves you back by one word, B ignores punctuation and we know w will take you to the next word, W will ignore punctuation.

More ways of gross navigation:

  • { and } navigates by blocks of code.
  • % will take you back and forth between parenthesis, curly braces or square brackets.
  • If we are at the top and we typing in a number then enter, this will take us to the that particular line of code (e.g. gg42 enter will take us to the 42nd line of code).
  • / then what ever you want to search and enter will take you whatever you want searched. Following this, n will take you to the next match; N will take you to the previous.
  • * locates all of the similar words that the cursor is at.

Simple keys for editing

Before we get into editing, a useful tool is to use visual mode:

  • V puts you into visual mode. This allows you to select code using your navigation keys. This comes in handy if you want to see what code you will be manipulating.
  • Also, . is particularly useful in redoing the previous command

Undo and redo:

  • u will undo (can repeat)
  • ctrl+r will redo (can repeat)

Deleting, copying and pasting:

  • dd deletes the line you are on and this also copies it to clipboard so its like using ‘cut’
  • D deletes from where the cursor is to the end.
  • ct and then whatever character will delete to the where you are to the the character you want to delete.
  • yy copies the selected line onto the clipboard
  • p will paste what is on the clipboard below

Inserting:

  • o creates a new line below and enters insert mode.
  • O (shift+o) creates a new line above and enters insert mode.

Indenting code:

  • < and > can be used to indent you code. You can use this in combination with visual mode to select and indent your code. Using . will allow you to repeat this action. If you use a number in front, then it will indent the number of lines below your selection.

Some other useful keys

Here are some other useful keys and settings that can help you:

  • ~ will swap the case of the character (i.e. make a lower case to upper and vice-versa).
  • :set number will provide lines numbers

Creating a macro

A macro a set of commands that you can map to any key. This is extremely powerful if you need to do a set of commands multiple times. You can set this to any key you want.

  1. q and then any key after (e.g. qw) is used to record the macro and set it to that particular key (e.g. w).
  2. We perform our commands, entering in and out of command, insert, visual mode etc.
  3. We can replay our macro using @ and then the key we used to save the macro.

We can repeat the macro multiple times by adding a number before calling the macro (i.e. number then @ then your assigned key).

What next

Now it’s your turn.

Get Vim on your system.

Make a simple program using Vim. Or play around with the commands with some code you have made already. It will feel awkward at first but it will get better as you progress.

So, I’m hoping this helps you out. How did you find it? What other keys did you discover useful that I haven’t listed here? What were useless? Please comment below and share this with others who might find this useful.

Related posts

python

The Zero to Hundred with Python

Reading Time: 6 minutes I’ve been embarking on the journey of learning Python. Python is a general-purpose programming language, excellent if you want to begin learning how to program

Read More »

Share this post

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email
Print Friendly, PDF & Email

Leave a Comment

Your email address will not be published. Required fields are marked *