Skip to content

2. Tutorial: Multiple Cursors

Nik Revenco edited this page Mar 1, 2025 · 2 revisions

One of the most powerful features Helix has is multiple cursor.

Multiple cursors allow you to perform complex refactors which can be broken down to a series of steps, as well as search-and-replace.

The best way to learn is with examples, and we'll provide you with many examples on this page.

Introduction

Note: Start of a selection is denoted by [[, end of a selection is denoted by ]].

We have some HTML which has repeating classes:

<a class="font-bold px-3" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />

And we want to change each px-3 class to px-4, as well as each font-bold class to font-thin:

<a class="font-thin px-4" />
<a class="font-thin px-4" />
<a class="font-thin px-4" />
<a class="font-thin px-4" />

  1. First, begin by placing your cursor on the first 3:
<a class="font-bold px-[[3]]" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />
  1. Press C which creates a cursor below, until you have a cursor on each individual 3:
<a class="font-bold px-[[3]]" />
<a class="font-bold px-[[3]]" />
<a class="font-bold px-[[3]]" />
<a class="font-bold px-[[3]]" />
  1. Press r which will replace the character, and then 4 to replace each of the 3s with a 4.
<a class="font-bold px-[[4]]" />
<a class="font-bold px-[[4]]" />
<a class="font-bold px-[[4]]" />
<a class="font-bold px-[[4]]" />
  1. Our goal is to change the class font-bold to font-thin, which you can do by pressing bbb until each bold is highlighted:
<a class="font-[[bold ]]px-4" />
<a class="font-[[bold ]]px-4" />
<a class="font-[[bold ]]px-4" />
<a class="font-[[bold ]]px-4" />
  1. Press c to change, and then type thin and Esc to return back to normal mode.
<a class="font-thin [[p]]x-4" />
<a class="font-thin [[p]]x-4" />
<a class="font-thin [[p]]x-4" />
<a class="font-thin [[p]]x-4" />

Choosing a Strategy

There are often many ways to accomplish the same refactoring with different keystrokes in Helix, which provides nice flexibility.

Let's see the same example we have done previously, but in a slightly different manner.


  1. From the following state:
<a class="font-bold px-3" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />
  1. Press gw to show two letters at the start of each word. Navigate to the first instance of the bold word by pressing the 2-character key combination that you see:
<a class="font-[[bold]] px-3" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />
<a class="font-bold px-3" />
  1. The entire bold word is highlighted now.

    Press C again 3 times until you select each instance of bold:

<a class="font-[[bold]] px-3" />
<a class="font-[[bold]] px-3" />
<a class="font-[[bold]] px-3" />
<a class="font-[[bold]] px-3" />
  1. As you see, the visual selection gets transferred over!

    Change the contents of each selection by using c to delete and enter insert mode -- then type thin and Esc to return back to normal mode.

<a class="font-thin[[ ]]px-3" />
<a class="font-thin[[ ]]px-3" />
<a class="font-thin[[ ]]px-3" />
<a class="font-thin[[ ]]px-3" />
  1. Press f + 3 to extend the selection to the next 3 for each selection:
<a class="font-thin[[ px-3]]" />
<a class="font-thin[[ px-3]]" />
<a class="font-thin[[ px-3]]" />
<a class="font-thin[[ px-3]]" />
  1. We could delete the entire px-3 class if we wanted.

    Instead press a to append at the end of each selection, which puts us into insert mode.

    Delete the 3 by pressing backspace, then add a 4, exiting back to insert mode with Esc:

<a class="font-thin[[ px-4]]" />
<a class="font-thin[[ px-4]]" />
<a class="font-thin[[ px-4]]" />
<a class="font-thin[[ px-4]]" />

As you become more familiar, you'll learn to pick out more efficient ways of accomplishing the same task, and which method is better based upon circumstances.