DEV Community

Gohomewho
Gohomewho

Posted on

Learn shortcuts to edit faster with VSCode (lots of images)

Editing texts becomes so much easier when we use shortcuts. I think the most important part is that it makes me feel less tedious and more productive because it allows me to focus more on coding rather than typing.

In this article, We'll learn a few VSCode shortcuts and combine them with some basic shortcuts I shared last week. We'll go through a few examples where shortcuts can make our lives so much easier.

Let's open VSCode and do this together. On the top left toolbar section, there's a selection tab and it contains some features of selection. We will go through most of them. Note that the keybinding you see in the image might be different than yours because I configured some of them. But don't worry! I'll show you how to configure.

VSCode selection tab

We can configure keybinding by clicking the gear icon at the bottom left corner and click Keyboard Shortcuts.
menu by clicking gear icon

We can search features and click the pencil icon to configure keybinding.
Search keyboard shortcuts

Press the keys we want then press Enter. Notice if the keybinding already exists, VSCode will notice us. We can click the link to see where the keybinding is used.
configure keybinding

Dummy HTML for practice

<button class="BTN button-primary" data-call-to-action-btn>
  Yes
</button>

<form class="form">
  <p class="heading">form</p>
  <button class="BTN b-success rounded btnnnn" data-submit-btn>
    Submit
  </button>
  <p>some text contains primary</p>
</form>

<button class="BTN b-success rounded" data-cancel-btn>
  No
</button>
Enter fullscreen mode Exit fullscreen mode

Search

👇Ctrl + F to open search bar, type the text you want to search, turn on Match Case and Match Whole Word if needed. We can press enter to find next matched instance.
Demonstrate the workflow

We can press Alt + Enter to select all matched instances.
Demonstrate the workflow

Press / to bring back your find history
Demonstrate the workflow

Press(maybe twice) Esc to close the search bar. The last matched instance you find will be selected.
Demonstrate the workflow


Multi-Select and Actions

Sometimes we want to rename a variable or a class. Wouldn't it be nice if we can select them all and edit at once. Luckily, it's easy to do in VSCode.

Add Selection To Next Find Match

keybinding of Add Selection To Next Find Match is configured to  raw `Ctrl` endraw  +  raw `D` endraw

If we currently don't have a selection, pressing Ctrl + D will behaves like double click, which select the word our cursor is on. After we have a selection, press Ctrl + D will find the next match instance and select it.

Notice there is cursor added to each selection which means we can edit them at once.
Demonstrate the workflow

Add Selection To Previous Find Match

Same usage as Add Selection To Next Find Match, but it select in a reverse order.
keybinding of Add Selection To Previous Find Match is configured to  raw `Alt` endraw  +  raw `D` endraw

Notice when I select primary there is one match below which is not I want. So I use Alt + D to select previous find match.
Demonstrate the workflow

Use Search with Add Selection together

One thing to note that these two Add Selection features by default is Match Case and Match Whole Word. But when we have search bar on, the configuration from the search bar is used.

Notice when I press Ctrl + D to select "BTN", matched instance are exactly "BTN", but after I press Ctrl + F to bring up the search bar, everything that includes "btn" are also matched because Match Case and Match Whole Word are turned off on the search bar.
Demonstrate the workflow

Add Multi-Cursor with mouse

Sometimes we want to select different things but edit them to the same or similar. We can do that by explicitly adding cursor and select multiple things. The shortcuts for Multi-Cursor can be toggled between Ctrl + Click and Alt + Click in the selection tab.

Toggle Multi-Cursor shortcuts in selection tab

Holding Alt/Ctrl + Click to add cursor then press Ctrl + D to select the words. Combined with Ctrl + /, we have the flexibility to edit more at once.
Demonstrate the workflow

Expand Selection and Shrink Selection

Expand more
editor.action.smartSelect.expand
keybinding of Expand Selection is configured to  raw `Alt` endraw  +  raw `A` endraw

Shrink some when we expand too much 🤣
editor.action.smartSelect.shrink
keybinding of Shrink Selection is configured to  raw `Shift` endraw  +  raw `Alt` endraw  +  raw `A` endraw

Notice how easily we can remove the classes from a element. Expand selection is usually used to select something within something else. It's very very powerful.
Demonstrate the workflow

Cut

In VSCode, if you don't have any selection, Ctrl + X can directly cut a line. I used it to quickly delete lines until I found a better option. The difference between Cut and Delete Line is cut will also copy. If we use Cut to delete empty lines, we will lose what we actually want to Cut.

editor.action.deleteLines
keybinding of Delete Line is configured to  raw `Alt` endraw  +  raw `X` endraw

Copy Paste

In VSCode, if you don't have any selection, Ctrl + C and Ctrl + V can directly copy and paste a line down.
Demonstrate the workflow

When copy multiple lines we have many options, We can either select everything we want and use Ctrl + C and Ctrl + V or use the copy line shortcuts.

keybinding of Copy Line Down is  raw `Shift` endraw  +  raw `Alt` endraw  +  raw `ArrowDown` endraw  and Copy Line Up is  raw `Shift` endraw  +  raw `Alt` endraw  +  raw `ArrowUp` endraw

The benefit of using copy line shortcuts is that you don't have to perfectly select everything in the lines.
Demonstrate the workflow

Move Line

keybinding of Move Line Down is  raw `Alt` endraw  +  raw `ArrowDown` endraw  and Move Line Up is  raw `Alt` endraw  +  raw `ArrowUp` endraw

Similar to copy line, we don't have to select everything in the lines.
Demonstrate the workflow

Be careful when you want to move through the code that is commented and folded, it will unfold them which is unlikely what you expect. This unintentional move could be frustrated because we use shortcuts to work faster and this result cannot be undo by Ctrl + Z. This is bad especially when our commented code is long.
Demonstrate the workflow

Toggle Fold

Toggle fold or unfold of the tree object.
keybinding of Toggle Fold is  raw `Ctrl` endraw  +  raw `Shift` endraw  +  raw `A` endraw

We can use this with expand selection Alt + A to see the area we want to fold.
Demonstrate the workflow

We can also use this shortcut to quickly fix the mistake I just made above. As long as our cursor is in the commented code, toggle fold can directly toggle the whole commented area.
Demonstrate the workflow

Parallel Create

This is useful when we have multiple things that need to be created to something similar.

keybinding of Add Cursor Above is  raw `Ctrl` endraw  +  raw `Shift` endraw  +  raw `ArrowUp` endraw  and Add Cursor Below is  raw `Ctrl` endraw  +  raw `Shift` endraw  +  raw `ArrowDown` endraw

When we switch file, we lose the previous multi-cursor, thus we need to use Add Cursor Above / Below in order to do parallel move. We can use Ctrl + P and search file name to switch to the file which is also a very useful shortcut.

Demonstrate the workflow

Add Wrapper to elements

Sometimes we want to wrap some elements within a new parent element, there is a feature for it.

Ctrl + Shift + P bring up the toolbar then type wrap. The option Emmet: Wrap With Abbreviation is what we want. This feature doesn't have a default keybinding, but you can configure it by clicking the gear icon on the right side.
 raw `Ctrl` endraw  +  raw `Shift` endraw  +  raw `P` endraw  bring up the toolbar then type wrap

we can use the shortcuts we've learned so far to select the range of elements, then use Emmet: Wrap With Abbreviation and type what you want to wrap elements with.
Demonstrate the workflow

Wrap Up

We cover so many shortcuts that can help us code faster in VSCode. In my opinion, if there is something we are going to use every day, it's definitely worth to spend some time to try and learn. These combinations will become intuitive as we practice more and use every day.

In case you are curious about the keystrokes, it is a VSCode built-in feature, you can toggle it by Ctrl + Shift + P bring up the VSCode features toolbar and type screencast then click it.
 raw `Ctrl` endraw  +  raw `Shift` endraw  +  raw `P` endraw  and type "screencast" in VSCode

By the way, if you forget a shortcut but you know the name of the feature. You can also search in that toolbar, e.g. fold.
 raw `Ctrl` endraw  +  raw `Shift` endraw  +  raw `P` endraw  and type "fold" in VSCode

Top comments (0)