Helix Golf
For all examples, we’ll assume that your cursor is on the very first character
Object into Array
Before
const palette = { apricot: "#f47868", lightning: "#ffcd1c", delta: "6f44f0",};
After
const palette = [ ["apricot", "#f47868"], ["lightning", "#ffcd1c"], ["delta", "6f44f0"],];
Command
jmr{[mi[s:<enter>r,t,;vgsms[lems"
jmr{[mi[s:<enter>r,t,;vgsms[lems"
- Go to the line below with
j
, this is because we need to be inside of the object for the next step. mr{[
replaces the nearest pair of curly braces ”{” with square brackets ”[”mi[
selects inside the entire array- Use
s
to enter select mode, which searches inside our selection and creates sub-selections based on a pattern - Input
:
and then hitEnter
, which will place a cursor on every ”:” creating many single-width selections r,
replaces each selection with a ”,”. Essentially we’ve replaced each colon with a commat,
moves the cursor on each line to the ending comma;
collapses the selection around each cursor into a single selectionvgs
selects each line excluding the final commams[
surrounds each individual selection with ”[” to turn it into an array. We’re almost done here. We just need to transform the first item in each sub-array into a string.l
moves 1 character forward, replacing the selection with just a 1-width selectione
selects until the end of each word. Since we start at the first character and select until the end, this selects the entire word.ms"
surrounds each word with double quotes to make strings
CSV to SQL
Before
id 1,Item 1,cost 1,location 1id 2,Item 2,cost 2,location 2id 10,Item 10,cost 10,location 10
After
INSERT INTO `database`.`table` (`id` ,`item` ,`cost` ,`location`) VALUES ('id 1','Item 1','Cost 1','Location 1');INSERT INTO `database`.`table` (`id` ,`item` ,`cost` ,`location`) VALUES ('id 2','Item 2','Cost 2','Location 2');INSERT INTO `database`.`table` (`id` ,`item` ,`cost` ,`location`) VALUES ('id 10','Item 10','Cost 10','Location 10');
Command
%<alt-s>"yys\d<enter>dhhbms``x_ms(IINSERT INTO `database<esc>a.`table<esc>la <esc>AVALUES (<esc>"yPS,<enter>ms'A;<esc>Fl;~
%<alt-s>"yys\d<enter>dhhbms``x_ms(IINSERT INTO `database<esc>a.`table<esc>la <esc>AVALUES (<esc>"yPS,<enter>ms'A;<esc>Fl;~
-
%
selects full file -
Alt
+s
split selection into multiple selections on newlines -
"yy
yanks the selections into “y” register. We’ll need it for later -
s
and then input the pattern\d
thenEnter
which creates a selection on all digits -
d
deletes the selections. Essentially we’ve removed all the digits. -
hh
goes backwards 2 chars, important to make sure we are at the end of each word -
Use
b
to select till the beginning of every word, which also nicely selects all the words that there are -
ms`
surrounds each word with a backtick -
`
switches all characters to lowercase -
x
selects each line then use_
to trim the trailing whitespace -
ms(
surrounds each line with parentheses -
I
goes into insert mode at the beginning of each line -
Type the following:
INSERT INTO `database -
Escape
goes back to normal mode -
a
to go into insert mode after the backtick then type:.`table -
Escape
goes back into normal mode, thenla
to enter insert mode just before the opening parentheses -
Add a
Space
thenEscape
to go back into normal mode again -
A
goes into insert mode at the end of each line, now type:VALUES ( -
Hit
Escape
to leave insert mode. Your cursor will be at the closing parenthesis. -
"yP
pastes our previously yanked items from the “y” register -
S,<enter>
splits current selection into multiple selections on each comma -
ms'
surrounds each item with a single quote -
A;
adds a semicolon at the end of each line -
Escape
goes back to normal mode andFl
to place your cursor on the lowercase “l” of each “location” -
;
collapses each selection into a single-width selection -
~
toggles the case for each “l” into “L”
Text into Array
Before
HelloThisIsHelix
After
[ "Hello", "This", "Is", "Helix" ]
Command
%<A-s>ms"<A-J>i,<esc>xms ms[
%<A-s>ms"<A-J>i,<esc>xms ms[
%
selects full file<A-s>
split selection into multiple selections on newlinesms"
surrounds each word with"
’s<A-J>i,
join lines inside selection, select the inserted space, and insert,
’s<esc>xms
enter normal mode, select line and surround by spacesms[
surround by[]
Convert functions into a class
Before
def calculate_area(length, width): result = length * width return result
def calculate_perimiter(length, width): result = 2 * (length + width) return result
def calculate_volume(length, width, height): result = length * width * height return result
After
class Calculator @staticmethod def get_area(len, wid): return len * wid
@staticmethod def get_perimiter(len, wid): return 2 * (len + wid)
@staticmethod def get_volume(len, wid, hei): return len * wid * hei
Command
%scalculate<enter>cget<esc>O@staticmethod<esc>jxxs\w+<enter>slength|width|height<enter>bllled%sresult =<enter>C<alt-(>;ddss<enter>xd%>O<backspace>class Calculator:
%scalculate<enter>cget<esc>O@staticmethod<esc>jxxs\w+<enter>slength|width|height<enter>bllled%sresult =<enter>C<alt-(>;ddss<enter>xd%>O<backspace>class Calculator:
-
%
selects the entire file -
s
searches inside the current selection and creates sub-selections based on a pattern. Inputcalculate
then hitEnter
to make a selection on all instances of the word -
c
then typeget
to change each “calculate” word into a “get” -
Escape
to go back to normal mode -
Use
O
to create an empty line above each cursor, write:@staticmethod -
Hit
Esc
to go into normal mode. -
We need to select 2 lines below the current line, first go down with
j
and then pressxx
which will select the current line, and then select the next line In total we now have 3 cursors each with 2 lines selected, which includes the first line of the bodies of each function and the function definition themselves -
s
brings up a prompt to select sub-selections by a given regex. The\w+
regex selects each word, type it and thenEnter
-
s
again then typelength|width|height
followed byEnter
. This will look at the contents of the current selections, and create sub-selections where it finds the regex which means “length or width or height”. So we select each instance of those 3 words -
Our cursor is currently at the end of each word. Let’s go to the beginning with
b
-
We want to keep the first 3 characters and discard the rest from each of the parameters. To do this, move to the 4th character with
lll
-
Use
e
to select until the end of each word and thend
to delete it -
Select the entire file again with
%
followed bys
to bring up selection prompt again -
Write
result =
followed byEnter
to select all instances of that string -
C
creates a new selection on the line directly below, for each cursor -
Use
Alt
+(
to rotate the contents of the selection backward -
;
collapses each cursor into a single selection -
dd
deletes two characters on each of the 6 lines -
s
to bring up the prompt, then inputs
followed byEnter
to select all “s” characters -
Select each of the lines with
x
followed byd
to delete -
Select whole file with
%
and indent with>
-
O
creates a newline above and enters Insert mode, thenBackspace
to delete an extra tab -
Write this:
class Calculator:
Enumerate and align
Before
[ { word: "a", count: 2565 }, { word: "and", count: 1777 }, { word: "of", count: 1331 }, { word: "that", count: 1263 }, { word: "to", count: 1030 }, { word: "in", count: 1027 }, { word: "it", count: 754 }, { word: "as", count: 730 }, { word: "was", count: 687 }, { word: "you", count: 652 }, { word: "for", count: 630 },]
After
[ { rank: 1, word: "a", count: 2565 }, { rank: 2, word: "and", count: 1777 }, { rank: 3, word: "of", count: 1331 }, { rank: 4, word: "that", count: 1263 }, { rank: 5, word: "to", count: 1030 }, { rank: 6, word: "in", count: 1027 }, { rank: 7, word: "it", count: 754 }, { rank: 8, word: "as", count: 730 }, { rank: 9, word: "was", count: 687 }, { rank: 10, word: "you", count: 652 }, { rank: 11, word: "for", count: 630 },]
Command
%s\{<enter>a rank: <ctrl-r>#,<esc>%s |\d+<enter>&
%
selects full file- Use
s
to enter select mode, which searches inside our selection and creates sub-selections based on a pattern - Input
\{
and then hitEnter
, which will place a cursor on every ”{”, creating many single-width selections a
to go into insert mode after the ”{”- Input
rank:
Ctrl
+r
followed by#
inserts an increasing number for every selection starting with 1- Input
,
Escape
goes back to normal mode- Use
s
to enter select mode again - Input
Space
and|\d+
which is a regular expression selecting all spaces and numbers, then hitEnter
- & to align all selections in columns, note that the numbers get right-aligned
Convert snake_case to camelCase
Before
const user_profile = { first_name: "John", last_name: "Doe", birth_date: "1990-05-15", email_address: "john_doe@example.com", phone_number: "555-123-4567", mailing_address: { street_name: "Main Street", house_number: 123, apartment_unit: "4B", zip_code: "10001", city_name: "New York" }};
After
const userProfile = { firstName: "John", lastName: "Doe", birthDate: "1990-05-15", emailAddress: "john_doe@example.com", phoneNumber: "555-123-4567", mailingAddress: { streetName: "Main Street", houseNumber: 123, apartmentUnit: "4B", zipCode: "10001", cityName: "New York" }};
Command
%s\w+(:| =)<enter>s_\w<enter>~<alt-;>;d
%s\w+(:| =)<enter>s_\w<enter>~<alt-;>;d
%
selects the entire files\w+(:| =)<enter>
selects all words that are followed by either a colon or equals sign with space - capturing variable and property namess_\w<enter>
finds all underscores followed by a character within those selections~
toggles the case of the character after each underscore (making it uppercase)<alt-;>
flips the selection cursor and anchor - this adjusts our selections to ensure we can properly target the underscores;
collapses multiple selections to just the underscoresd
deletes all the underscores