Skip to content
Playing golf illustration

Helix Golf

Refactoring Examples for the Helix Editor

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", "#fccd1c"],
["delta", "6f44f0"],
];
Command

mr{[mi[s:<enter>r,t,;vgsms[lems"

  1. Go inside of the object with j
  2. mr{[ replaces the surrounding ”{” with ”[”
  3. mi[ selects inside the entire array
  4. Use s to enter select mode, which searches inside our selection and creates sub-selections based on a pattern
  5. Input : and then hit Enter, which will place a cursor on every ”:” creating many single-width selections
  6. r, replaces each selection with a ”,”. Essentially we’ve replaced each colon with a comma
  7. t, moves the cursor on each line to the ending comma
  8. ; collapses the selection around each cursor into a single selection
  9. vgs selects each line excluding the final comma
  10. ms[ 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.
  11. l moves 1 character forward, replacing the selection with just a 1-width selection
  12. e selects until the end of each word. Since we start at the first character and select until the end, this selects the entire word.
  13. ms" surrounds each word with double quotes to make strings

CSV to SQL

Before
id 1,Item 1,cost 1,location 1
id 2,Item 2,cost 2,location 2
id 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,ms'A;<esc>Fl;~

  1. % selects full file

  2. Alt + s split selection into multiple selections on newlines

  3. "yy yanks the selections into “y” register. We’ll need it for later

  4. s and then input the pattern \d then Enter which creates a selection on all digits

  5. d deletes the selections. Essentially we’ve removed all the digits.

  6. hh goes backwards 2 chars, important to make sure we are at the end of each word

  7. Use b to select till the beginning of every word, which also nicely selects all the words that there are

  8. ms` surrounds each word with a backtick

  9. ` switches all characters to lowercase

  10. x selects each line then use _ to trim the trailing whitespace

  11. ms( surrounds each line with parentheses

  12. I goes into insert mode at the beginning of each line

  13. Type the following:

    INSERT INTO `database
  14. Escape goes back to normal mode

  15. a to go into insert mode after the backtick then type:

    .`table
  16. Escape goes back into normal mode, then la to enter insert mode just before the opening parentheses

  17. Add a Space then Escape to go back into normal mode again

  18. A goes into insert mode at the end of each line, now type:

    VALUES (
  19. Hit Escape to leave insert mode. Your cursor will be at the closing parenthesis.

  20. "yp pastes our previously yanked items from the “y” register

  21. S, splits current selection into multiple selections on each comma

  22. ms' surrounds each item with a single quote

  23. A; adds a semicolon at the end of each line

  24. Escape goes back to normal mode and Fl to place your cursor on the lowercase “l” of each “location”

  25. ; collapses each selection into a single-width selection

  26. ~ toggles the case for each “l” into “L”

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:

  1. % selects the entire file

  2. s searches inside the current selection and creates sub-selections based on a pattern. Input calculate then hit Enter to make a selection on all instances of the word

  3. c then type get to change each “calculate” word into a “get”

  4. Escape to go back to normal mode

  5. Use O to create an empty line above each cursor, write:

    @staticmethod
  6. Hit Esc to go into normal mode.

  7. We need to select 2 lines below the current line, first go down with j and then press xx 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

  8. s brings up a prompt to select sub-selections by a given regex. The \w+ regex selects each word, type it and then Enter

  9. s again then type length|width|height followed by Enter. 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

  10. Our cursor is currently at the end of each word. Let’s go to the beginning with b

  11. 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

  12. Use e to select until the end of each word and then d to delete it

  13. Select the entire file again with % followed by s to bring up selection prompt again

  14. Write result = followed by Enter to select all instances of that string

  15. C creates a new selection on the line directly below, for each cursor

  16. Use Alt + ( to rotate the contents of the selection backward

  17. ; collapses each cursor into a single selection

  18. dd deletes two characters on each of the 6 lines

  19. s to bring up the prompt, then input s followed by Enter to select all “s” characters

  20. Select each of the lines with x followed by d to delete

  21. Select whole file with % and indent with >

  22. O creates a newline above and enters Insert mode, then Backspace to delete an extra tab

  23. Write this:

    class Calculator: