DEV Community

loading...

Quickly Move Code in VS Code w/ Mac Shortcuts

Brad Beggs
Curious and Persistent.
・2 min read

CMD X - place the full line into memory & clear it simultaneously

What It Does: This is similar to the classic CMD X cut of text but without the need to select the text. This shortcut automatically moves text into memory and deletes the line. Use CMD V to paste the same line (if needed)

Best For: removing 1 line and moving it to a new position. At the new position, CMD V and the text will bump down the current line text and insert at the location.


CMD Enter - insert a new line from the middle of the line above

What It Does: On your cursor position, CMD Enter inserts a new line break, shifting the current line 1 lower and moving your cursor to the start of the empty line.

Best For: Spacing out code blocks or need a new line for new text


CMD D - select identical items and replace

What It Does: Finds identical text and selects it for simultaneous delete, or re-write

Best For: Renaming a variable/text which is used in multiple locations in one file.

Note: Move your cursor to the word/phrase and CMD D to select all adjoining characters. No need to highlight the whole word or number. CMD D multiple times to select each instance below the cursor. ESC to remove focus.


Option Up or Down Arrows - move line(s)

What It Does: Easily move a full line of code (or lines of code) up or down as many lines as needed. Similar to cut and paste or CMD X but visually see code move

Best For: Refactoring code. Moving one function above or below another.

Note: From anywhere on a single line, Option up or down to move the line. To move multiple lines, highlight at least 1 part of each line to move them as a block.


OPTION CMD [ or OPTION CMD ] - collapse/uncollapse code blocks

What It Does Quickly collapse or uncollapse blocks of code according to scope

Best For: condensing code that you don’t need to see or deal with at the moment.

Note: This command works per scope. So you can collapse while loops, if statements, etc, and not the complete function. To collapse the function, invoke the shortcut in the functional scope.

Discussion (0)