Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VS Code extensions API support changing cursor position?

I would like to know if the extension API support changing cursor position. I tried to find in the API reference but I couldn't find related function.

The reason behind such function is that

  • I normally spend much time to move the cursor while writing source code. (by pressing arrow key, home key, pgUp, etc)
  • So, if I can write an extension that could change cursor location effectively, that could save time a lot. :-)

Thank you in advance!

like image 612
eric Avatar asked Oct 30 '25 04:10

eric


1 Answers

there is a command named cursorMove which can be run from an extension. In this example the extension uses showInputBox to prompt for the number of lines to move. Then vscode.commands.executeCommand is run to execute the cursorMove command to move down by the entered number of lines:

import * as vscode from 'vscode' ;

let lastNumLines = '1';

// -------------------------- registerCommand_moveCursor --------------------------
export function registerCommand_moveCursor(context: vscode.ExtensionContext)
{
    const fullExtensionName = `sample.moveCursorDown`;
    let disposable = vscode.commands.registerCommand(fullExtensionName, async ( ) =>
    {
        const val = await vscode.window.showInputBox(
                    { prompt: 'number lines to move', value: lastNumLines });

        if (val)
        {
            lastNumLines = val ;
            const numLines = Number(val) ;

            await vscode.commands.executeCommand("cursorMove",
                {
                    to: "down", by:'wrappedLine', value:numLines
                });
        }
    });
    context.subscriptions.push(disposable);
}
export function activate(context: ExtensionContext)
{
    registerCommand_moveCursor(context) ;
}

package.json

    "contributes": {

            "commands": [
                {
                    "command": "sample.moveCursorDown",
                    "title": "move cursor down",
                    "category": "sample"   
                }
            ],
}
like image 93
RockBoro Avatar answered Nov 01 '25 14:11

RockBoro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!