8

Let's say I select some text in the editor. I would like to define my own Python functions or scripts that:

  1. Take as an input the selected text as a string.
  2. Transform it and return a new string that replaces the selected text back in the editor

Moreover, I would like to ideally register these functions or scripts as a VSCode commands (with a specific commandIDs) that I can define myself and to the command palette in VSCode. By doing so, I can bind them to a shortcut in keybindings.json with specific shortcuts and when clauses.


Is there any built-in functionality in VSCode to do this? If not, what set of steps can I follow to do it?

7
  • 1
    I think you need to write a TypeScript wrapper for your python script to get the text and run your python script, then replace the text with output from your script. Tasks in VS Code: code.visualstudio.com/docs/editor/tasks Commented Nov 26, 2019 at 23:40
  • Thanks @Nathan Let's say my question asks for the exact same thing, but using TypeSript functions / scripts, instead of Python ones. In that case, I suppose I wouldn't need to write a TypeScript wrapper (of my Python code). Would that change your recommended course of action? (e.g. using tasks?) Commented Nov 27, 2019 at 16:58
  • No problem. Yes, I think it makes more sense to just use TypeScript and create a task for vscode. Commented Nov 27, 2019 at 20:53
  • Snippets can generally do what you ask unless it is very complicated. Attach an example to your question of before and after. Commented Nov 29, 2019 at 20:42
  • 2
    Thanks @Mark - I actually want to run my own Python functions on the input string to generate a new one. I come from Emacs and have too many scenarios to list, from formatting numbers and text, to postprocessing text that I paste in the editor to "LaTeXify it", to expanding my own set of macros or keywords across the selected text. There is a large world of options that opens up if I can easily keyboard bind a Python function of my choice that receives selected text as an input and optionally writes back to the editor or to the clipboard (w/o modifying the selected text in this last case) Commented Nov 29, 2019 at 23:17

3 Answers 3

3

Disclaimer: We are the author of vscode-texty extension

Based on OP's suggestion, we have enhanced the vscode-texty to add this support.

Now it supports following capabilities.

  • Select some text and process selection using adhoc script
  • Select some text and process line within selection using adhoc script
  • Custom script can refer to lodash, numeral and moment libraries for easy manipulation of text
  • Save the script as custom command and invoke that command with quick pick options
  • Key bind that custom command for quick execution

One issue (due to limitation of vscode api) is that, custom commands does not appear in the standard command palette. So to invoke via UI, you first invoke command Texty: Process selections using custom command, which shows all available commands as quick pick options, executes the selection.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

It is technically possible, but I'm unaware of any extension that currently supports this idea. You will probably need to write your extension to do this.

Comments

0

scriptbox allow you to write a js function to modify your selected text, but could not registry as a command

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.