Level Up Your Vim Game: Daily Practices for Developers

8 min read
Posted on
Level Up Your Vim Game: Daily Practices for Developers

TL;DR

This article dives deeper into mastering Vim by focusing on advanced motions, search techniques, and powerful editing commands. Learn to move quickly using character motions like f{char}, t{char}, and their backward equivalents F{char} and T{char}. Enhance your text manipulation skills with search (/, ?) and edit (c, d) combos. Explore practical commands to join lines (J), change case (g~, gU, gu), wrap text neatly (gq, gw), and repeat edits efficiently using the . command. Perfect for developers looking to turn everyday Vim use into a powerful editing workflow.

Most developers know Vim but few master it in depth. What if that daily practice could turn your muscle memory into superpowers?

As I have covered in this article about mastering Vim and boosting your productivity, we will go deeper into how to perform searches in Vim, using other editing commands, master the visual mode, which helps with selecting multiple chunks of text, and much more.

Let’s get started!

Character Motions in Vim

To move between specific characters, using f{char} will move forward onto the next occurrence of {char}. When paired with c or d allows for the changing or deletion of text. A typical example is using cfNn<ESC> to change a variable, “favouriteColor” to “color”.

To repeat something similar backwards, use F{char}. Position the cursor after the word “Color”, then use dFN to delete the word.

Another important character motion to consider, is the use of t{char} which moves the cursor until the next occurrence of {char}. Let’s try it out with ct”.

Also similarly to F, T{char} will move backwards with the cursor positioned rightly.

Search Smarter in Vim: Find What You Need Fast

In Vim, searching is one of the most important features which enables you to quickly jump to the specified text without interrupting your work. Searching uses the / key followed by the text you want to find.

If your search term has more than a single match, you can use n to go to the next search result. Likewise the N key helps you find the previous match.

P.S.: Hit the enter key after using the search query, /<word><ENTER>.

Another way of searching is using ? followed by some text to search backwards from your cursor position. From the previous exercise, try searching to find a word with the search query ?<word><ENTER>.

To delete an entire chunk of text, searching in Vim is not limited to finding things, you can use c or d to change or delete text.

From the video, the word “Brandenburg” will be removed by typing /brand<ENTER> and afterwards use d/gate<ENTER> to delete everything from the cursor but not including the next search result.

P.S.: Vim is smart to identify words even if it is not typed in full.

Finding Occurrences

To find occurences of a word under the cursor, use * and to search backwards, use #.

Miscellaneous Vim Edit Commands

There are other editing commands available to use in Vim. Like the use of J to join lines together.

Changing the Case

With Vim, it is possible to change the case of letters and words. A good use case study will be with naming variable and function names. Here are some of the commands you need to know:

  • ~: Change an entire sentence by holding ~

  • g~{motion}: To change a case based on motion, means to effect a change to a particular word.

  • g~$: It is kind of similar with function of ~

  • g~~: This command is responsible for changing the case of an entire line

  • gU{motion}: Useful to convert all the letters to uppercase.

    # gUw
    const favorite_snack = "Chin chin" # const FAVORITE_SNACK = "Chin chin"
    

    Similarly if you want to revert to lowercase, use guiw

Wrappig Text

It is well known that editors come with the feature of wrapping text once it reaches a certain length. For unusual length, Vim can automatically wrap lines using using gw{motion} or gq{motion} based on your configuration.

When working with plaintext documentation or commit messages, it's helpful to reformat lines so they wrap neatly at a certain width. Vim makes this simple with the gq command, which can be applied to a motion, a visual selection, or even an entire paragraph. This is particularly useful for keeping files tidy and readable, especially when dealing with tools or environments that don't support dynamic word wrapping.

To format this sentence, use gw2j (Neovim) or gq2j (VSCode)

Repeating

Let’s try something unique. To change a word in multiple instances it appears, use . to repeat the last edit in this order.

  • Search the word. For instance /book
  • Use n to the first occurrence of the word “book”
  • change the first occurrence to “magazine” using ciw
  • Repeatedly press n. (n dot) to change the remaining occurrence of the word “book”

Vim Visual Mode and Substitute Explained

Want to select multiple chunks of text visually at once? The visual mode is the designated feature for this function in Vim. Vim has two types of visual modes:

  • Line: It selects entire lines
  • Block: It allows you draw a rectangle around characters and selects everything within

Now, to enter the visual mode, use v{motion} like v8j (block) or vw (line).

For replacing text, we use Substitute. Substitute is a command that must be entered on the Vim command line. To substitute, position the cursor on the beginning of the word, and then open the Vim command line (positioned at the bottom of the screen) with :. Once it is opened, type s/OLD/NEW.

P.S.: The OLD is the original text while the NEW is the text you want to see exchanged.

Note: If you want to replace all the occurrences of repeated words, append /g (global) flag to the end like this s/OLD/NEW/g.

Another way of replacing text in a whole file is using :%s/OLD/NEW/g.

Let’s substitute text within a specified area by using the visual mode. Use :s/\%Vbook/novel (:s/\%VOLD/NEW)

The %V constrains the substitution to the visual selection.