-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
2. Tutorial: Multiple Cursors
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.
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" />
- 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" />
- 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]]" />
- Press
r
which will replace the character, and then4
to replace each of the3
s with a4
.<a class="font-bold px-[[4]]" /> <a class="font-bold px-[[4]]" /> <a class="font-bold px-[[4]]" /> <a class="font-bold px-[[4]]" />
- Our goal is to change the class
font-bold
tofont-thin
, which you can do by pressing bbb until eachbold
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" />
- 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" />
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.
- 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" />
- 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" />
- The entire
bold
word is highlighted now. Press C again 3 times until you select each instance ofbold
:<a class="font-[[bold]] px-3" /> <a class="font-[[bold]] px-3" /> <a class="font-[[bold]] px-3" /> <a class="font-[[bold]] px-3" />
- 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" />
- 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]]" />
- 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.