1

I'm looking for a simple method to determine the column position of a VSCode editor's text insertion cursor/caret, either prior to selecting an area of text to copy or immediately the mouse is used to start the selection. The column number would then be stored in the clipboard before performing further clipboard manipulation.

I have tried searching for AutoHotkey methods to achieve this, but the only solution I'm able to find involves using ImageSearch, which is not suitable for my purpose.

Edit: I found this API reference, could I possibly use this to determine the cursor position preferably using windows cmd/powershell?

Eric
  • 95,302
  • 53
  • 242
  • 374
fade2gray
  • 21
  • 1
  • 3

1 Answers1

3

You can make a vscode extension and bind the command to a keyboard shortcut.

How to get line and column(character) of cursor:

const activeEditor = vscode.window.activeTextEditor
    if (activeEditor) {
        console.log(activeEditor.selection.active.line)
        console.log(activeEditor.selection.active.character) //column
    }

api: https://code.visualstudio.com/api/references/vscode-api#details-159

activeEditor.selection gives you an object with 4 objects

start:Object
  line:4
  character:8
end:Object
  line:6
  character:8
active:Object
  line:4
  character:8
anchor:Object
  line:6
  character:8

activeEditor.selection.active is your cursor.

activeEditor.selection.anchor is

The position at which the selection starts. This position might be before or after active.

anchor-active may be reversed, but start-end will always be up-down.

note: I found the api AFTER I found out how to get current line from here: VScode API why can't I get the current line?

EDIT: for columnNum (see Eric's comment):

const activeEditor = vscode.window.activeTextEditor
if (activeEditor) {
    const lineOffset = activeEditor.selection.active.line
    const charOffset = activeEditor.selection.active.character

    console.log(`line: ${lineOffset + 1}`)
    console.log(`character: ${charOffset + 1}`)
    console.log(`column: ${getColumn(activeEditor.document.lineAt(lineOffset).text,charOffset) + 1}`) //column

    function getColumn(str, sumCharacter) {
        const arr = [...str]
        let whichCharacter = 0
        for (let whichColumn = 0; whichColumn < arr.length; whichColumn++) {
            if (whichCharacter===sumCharacter) {
                return whichColumn
            }
            whichCharacter+=arr[whichColumn].length
        }
        return arr.length
    }
}
Mr. Doge
  • 796
  • 5
  • 11
  • 1
    Note that `.character` is _not_ the column number; it counts utf-16-encoded code units, whereas the column number in the status bar counts unicode codepoints; `` is counted as two characters by `Position.character`. – Eric May 19 '22 at 11:26
  • wow, after some... I found that column isn't by unicode segmentation (because `️‍` is counted as 4 columns), so it's most likely `[...str]` – Mr. Doge May 19 '22 at 13:36
  • `️‍` is counted as 4 columns by the VSCode status bar, but as 6 "characters" by [`Position.character`](https://code.visualstudio.com/api/references/vscode-api#Position.character) (which just measures `"️‍".length`). – Eric May 19 '22 at 15:39
  • I should add link: @Eric, can you vote this question as not-duplicate ? also: , oh, it's Unicode Text Segmentation, not unicode segmentation.. but I should always add link. ok, they are actually called Grapheme clusters/graphemes, this is the most useful/complete one (it's always either 1 of these 3) – Mr. Doge May 19 '22 at 17:32