Tyler Lum
Tyler Lum
Sep 17 · 4 min read

Introduction

Vim is just a text editor. But if you have ever seen an expert use vim, you know that it is shockingly powerful, efficient, and fast. At a high level, vim is a tool that allows you to more clearly better describe what you are trying to do when editing text. Once you become fluent in vim’s language, you will be able to communicate with your computer at a much greater speed.

Prerequisites

For this article, I expect that you have at least a beginner’s fluency with vim, which means that simple commands such as basic movement, inserting, and searching are already understood. If you aren’t at this level yet, you can open the terminal and run:

sudo apt-get install vim

Then, you can go through the vim tutorials by running:

vimtutor

1. Macros

It isn’t nice when you have to do repetitive text editing tasks, but it is the worst when you have to do that task dozens of times. Macros allow you to essentially hit a record button, make your changes, and then stop the recording when you’re finished. Then you can replay the actions in that recording at the press of a button.

  • Press ‘q’ to tell vim you want to record, then press almost any key to tell vim you want to save your recording to that key. In this example, the key will be ‘a’. So this command would be ‘qa’.
  • Perform your command.
  • Press ‘q’ to end the recording.
  • When you want to replay that series of actions, press ‘@a’

2. Relative Line Number

No one likes mental math. Even if you are fast with mental math, you can always calculate 23 = 23 faster than 141–118 = 23. Relative line number displays line numbers not as absolute line numbers of a text file, but as line numbers relative to your cursor position. This allows you to easily delete lines, jump to exactly where you want to go with ease. You can get this by adding the following to you .vimrc:

set relativenumber

For example, you can delete the next 25 lines by using the following vim command:

25dd

3. Registers

Registers are used to store text when you copy text for pasting. Your computer has multiple registers. By default, vim uses a register for its stored text that is different from the system’s copy stored text. Add the following to your .vimrc:

set clipboard=unnamed
set clipboard=unnamedplus

With this addition, copied text from vim can be pasted by CTRL+V and copied text from CTRL+C can be pasted into vim with ‘p’.

If you want to delete text without overwriting your register, you can run “_ before your command, such as:

“_dd

to delete a line.

4. Remapping Escape

The escape key is very far from the home row. Given how often it is pressed, this is not the best way to exit a command. You can add the following to your .vimrc:

cnoremap kj <C-C>
cnoremap jk <C-C>

This allows you to escape with a ‘jk’ or ‘kj’ press, which leaves your hands right on the home row.

5. Verb Modifier Object

Vim has a special type of grammar that typically follows the pattern ‘verb modifier object’.

Example verbs:

  • ‘d’ delete
  • ‘c’ change

Example modifiers:

  • ‘t’ till
  • ‘f’ find
  • ‘i’ inside

Example objects:

  • ‘w’ word
  • ‘(‘ parenthesis
  • ‘t’ tag
  • ‘s’ sentence
  • ‘“‘ quotes

For example, ‘diw’ deletes a word that your cursor is on, regardless of whether it is at the beginning or end of the word. ‘dt)` deletes all text from the current cursor position up until and excluding ). ‘cfp’ deletes all text from the current cursor position up to and including p, then automatically puts you into insert mode. Combined with ‘.’, which repeats the previous command, this is very powerful.

6. White Space Fixing

An pet peeve in software engineering is when there are tabs instead of spaces and white space at the end of a line. You can add the following to your .vimrc:

set list

This shows a $ sign at the end of each line and shows ^I instead of tabs. This allows you to easily identify tabs and $ signs. As well, the example .vimrc below replaces all tabs with spaces, making white space handling much easier: https://vim.fandom.com/wiki/Example_vimrc.

7. Navigating Faster

The first thing you learn inv vim is how to use the hjkl keys to navigate. However, this is one of the slowest ways to navigate. Here are a list of important commands to help you navigate faster:

  • Click ‘gg’ to move to the top of the file and ‘G’ to move to the bottom
  • Click CTRL + F to move forward quickly and CTRL + B to move backward quickly
  • Click ‘zz’ to recenter the page so that your cursor is at the center
  • When on a bracket such as ‘{‘ or ‘(‘, you can click ‘%’ to move to the other matching bracket
  • Use ‘/’ to start a search to find the word you want

8. Date

In many scenarios, you will need to add the date into a text file. This can be slow and tedious. By adding the following to your .vimrc:

nmap <F3> i<C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR><Esc>
imap <F3> <C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR>

you can add the date and time at the press of F3.

Closing Thoughts

Although there is a steep learning curve to vim, the powerful tools listed above will not only save you time, but give you a more enjoyable text editing experience; rather than wasting your energy typing away furiously to solve a text editing problem, you can instead focus on finding intelligent ways to communicate with your computer in vim’s elegant language.

Check out my vim configuration file at https://github.com/tylerlum/vim_configuration.

The Startup

Medium's largest active publication, followed by +516K people. Follow to join our community.

Tyler Lum

Written by

Tyler Lum

Robotics | AI | UBC Engineering Physics

The Startup

Medium's largest active publication, followed by +516K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade