VSCode: Select entire function definition?

2.1k Views Asked by At

Is there a way to select an entire function definition in VSCode? That's a recurring action in my dev workflow, and I'd like to automate it instead of using my mouse. Couldn't find a shortcut for it. Surely there ought to be a way?

I'm using python/javascript as my languages, with Python (ms-python.python) & JavaScript and TypeScript Nightly (ms-vscode.vscode-typescript-next) extensions installed.

Thank you!

2

There are 2 best solutions below

1
On BEST ANSWER

Use the folding arrows in the left-gutter.

When function collapsed select that line and Copy

It depends on the language what a function is and how to separate them, you need to build an AST from the text and the language server does this to determine the fold arrows.

0
On

The VS Code VIM extension has bindings vii and vai to select by indentation level (see github documentantion https://github.com/VSCodeVim/Vim?tab=readme-ov-file#vim-indent-object) .

This implies that you can then select the function as long as you're at the first indentation level of the function, but it would not select only the full function if the cursor is at the second indentation level or at the declaration of the function.

This could maybe be an alternative to the fold select method of the other answer when the cursor is not at the function declaration.