Search code examples
visual-studio-codehexvscode-extensions

How can I make the line number field display numbers in hex?


I am doing a side project where I need to type in the value to line numbers. It would be fine to just type it into a calculator, but that would be slow for many times. I tried digging around settings and google but found nothing so far. How can I make the line numbers be in hex, instead? Like line 1 be displayed as 01 and line 42 be displayed as 2a? If there is no extension already, how may I go about coding this?


Solution

  • This may be able to be done with a text decorator - for an example.

    Also it is very easy to set up an extension that gets the hex version of a line number by right-clicking the line number (and choosing a command). Let me know if this works for you.

    In your package.json you would have these contributes:

    "contributes": {
        "commands": [
            {
                "command": "folder-operations.getLineNumberHex",
                "title": "Copy the hex for this line number"
            }
           ],
    
        "menus": {
            "editor/lineNumber/context": [
                {
                        "command": "folder-operations.getLineNumberHex"
                }
            ]
        }
    }
    

    using your own publisherID for the command. And in your extension.js activate function you would have this command registration:

    let disposable = vscode.commands.registerCommand('folder-operations.getLineNumberHex', async (args) => {
    
        // args.lineNumber is 1-based
        const hexLineNumber = Number(args.lineNumber).toString(16);
        await vscode.env.clipboard.writeText(hexLineNumber);
    });
    

    Here it is working:

    get the line number as a hex from line number context menu