logo

How to Input Consecutive Numbers Conveniently in VSCode with Multi-Cursor 📂Programing

How to Input Consecutive Numbers Conveniently in VSCode with Multi-Cursor

Guide

Even when working with programming languages, using arrays might be cumbersome, or there might be scenarios when using markup languages where numbers need to be sequentially entered starting from 0 or 1. Here, I introduce a method to quickly and easily input multiple numbers using multi-cursor1.

Setting Up New Shortcuts

alt text

Open the Keyboard Shortcut settings in vscode through File/Preferences/Keyboard Shortcut or Ctrl + kCtrl + s, then click the page icon at the top right to open keybindings.json.

{
  "key": "alt+i",
  "command": "editor.action.insertSnippet",
  "args": {"snippet": "$CURSOR_INDEX"},
  "when": "editorTextFocus && editorHasMultipleSelections"
},
{
  "key": "alt+n",
  "command": "editor.action.insertSnippet",
  "args": {"snippet": "$CURSOR_NUMBER"},
  "when": "editorTextFocus && editorHasMultipleSelections"
},

Insert the following new shortcut settings.

alt text

The two shortcuts shown in the screenshot are bound to Alt + i or Alt + n, executing commands that input the value of $CURSOR_INDEX$CURSOR_NUMBER respectively.

Difference in $CURSOR_INDEX$CURSOR_NUMBER

CURSOR.webp

  • $CURSOR_INDEX: The number starts from 0.
  • $CURSOR_NUMBER: The number starts from 1.

Environment

  • OS: Windows 11
  • vscode: v1.97.2