I am using VS Code with VI bindings and I am trying to do the following.
I have the following file contents:
abc="some value"
def="some other value"
ghi="some other other value"
jkl="some other other other value"
.
<etc. for many lines>
I want to make this file change to (Expected Output):
abc=${abc}
def=${def}
ghi=${ghi}
jkl=${jkl}
So far, I have first replaced =.+ with =${. I get the following:
abc=${
def=${
ghi=${
jkl=${
Then I am trying to use multiple cursors in VS Code by Cntrl + Alt + I and I am trying to copy each line from the beginning to paste it so that I get the expected output. For some reason, it isn't letting me select the whole line when I do a Cntrl + Home. Could someone please help me out with this?
For reference: The VI section of my settings.json file is as follows:
// my settings
"vim.easymotion": true,
"vim.sneak": true,
"vim.incsearch": true,
"vim.useSystemClipboard": true,
"vim.useCtrlKeys": true,
"vim.hlsearch": true,
"vim.handleKeys": {
"<C-a>": false,
"<C-f>": false,
"<C-w>" : false,
"<C-x>" : false,
"<C-c>" : false,
"<C-h>" : false,
"<C-b>" : false,
"<C-n>" : false
},
I don't use VI, but this is easy to do with a snippet (in your keybindings.json).
{
"key": "alt+b", // whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/(.*)=.*/$1=${$1}/g}"
},
"when": "textInputFocus && editorHasSelection"
},
Then select your code and alt+b (or whatever keybinding you use). You don't need multiple cursors or need to select each line separately.
Why does it not work with Multi Cursor?
Using the default key bindings.
position cursor before a
use Ctrl+Alt+DownArrow as often as you like
use Shift+Ctrl+RightArrow select all before the =
Ctrl+C
RightArrow 2 times
Shift+End select all after the =
${
Ctrl+V
Esc
Or start selecting =" followed by
Ctrl+Shift+L LeftArrow Shift+Home Ctrl+X RightArrow Shift+End ${ Ctrl+V Home Ctrl+V Esc
Related
Is it possible to customize the location of the comment symbol ('#' when using Python) in VSCode?
For example, if my code is:
def my_func():
value = 1
and I press CMD-/ on line 2, I get:
def my_func():
# value = 1
I would prefer to get:
def my_func():
# value = 1
Is there a way to modify the default behavior?
VSCode: 1.67.1
MacOS: 12.3.1
There is no built-in way to do that. You will need an extension. See https://stackoverflow.com/a/59448448/836330 for a previous answer using a different extension. Here is a better answer using an extension I made in the meantime, Find and Transform. But there are restrictions as noted below.
Make this keybinding (in your keybindings.json):
{
"key": "alt+/", // unfortunately, this cannot be ctrl+/
"command": "findInCurrentFile",
"args": {
{
"key": "alt+r",
"command": "findInCurrentFile",
"args": {
"preCommands": [
"cursorEnd",
"cursorHomeSelect",
"cursorHomeSelect"
],
"replace": "${LINE_COMMENT}${TM_CURRENT_LINE}",
"restrictFind": "line" // works on multiple lines, see the demo
},
"when": "editorLangId == python" // if you want to limit it to a language
},
}
You can use whatever keybinding you want, but not Ctrl+/ because then toggle off will not work.
Ctrl+/ will work to toggle off comments if you do not use it in the keybinding above to add comments.
Note: For this to work well you need to disable the following setting (which I have shown disabled for a particular language, python):
"[python]": {
"editor.comments.insertSpace": false
}
That goes into your settings.json.
I'm using the multi-command extension, and after pasting some text in a macro, I move the cursor around a few times, like the following:
{
"command": "multiCommand.test",
"interval": 0,
"sequence":[
{
"command": "editor.action.insertSnippet",
"args": {"snippet": "some text\n\n"}
},
{"command" : "cursorUp"},
{"command" : "cursorUp"},
{"command" : "cursorEnd"},
]
}
Everything ends up correct, but there's a noticeable delay in the cursor executing these commands that I'd like to be instantaneous (e.g. i'd like to not see it go through every step but just end up at the right spot.) Is there some setting I can change, or other command I can use to just instruct the cursor to move to a certain coordinate in the text?
Add the field $0 in your snippet
"snippet": "some text$0\n\n"
And remove the cursor commands
I want to set that if I write a colon, I am automatically added a space after it. I used to use IntelliJ IDEA, which did this automatically when working with JSON. Can this be set in VSC as well?
The extension HyperSnips is one way to do it.
In your json.hsnips file (so it is limited to json files):
snippet `:` "expand to : " A
``rv = ': '``
endsnippet
Now whenever you type a : it will be expanded to : in a json file.
See https://stackoverflow.com/a/62562886/836330 for more info.
Just using : as a key to a prefix in a keybinding won't work as you must have a modifier key - like alt, ctrl, etc. See https://code.visualstudio.com/docs/getstarted/keybindings#_accepted-keys.
You could do something like:
{
"key": "alt+;", // I chose ; because it is on the same key for me as :
"command": "type",
"args": { "text": ": " },
"when": "editorTextFocus && editorLangId == json"
},
and that would work.
The Problem
An operation I do a lot is to highlight/select code on the editors and then paste it on the integrated terminal to execute it. While I got pretty fast at using Ctrl + c, Ctrl + backtick, Ctrl + Shift + v and Enter, it's very annoying and repetitive. Is there a way to configure a macro or shortcut for this?
A Solution (failed) Attempt
This Github thread and this StackOverflow Question show how to create a shortcut to toggle between different Integrated Terminals. I would like for something similar to happen in my case (I used Ctrl + Shift + u in the example below), e.g.:
[
{
"key" : "ctrl+shift+k",
"command" : "workbench.action.terminal.focusNext"
},
{
"key" : "ctrl+shift+j",
"command" : "workbench.action.terminal.focusPrevious"
},
{
"key" : "ctrl+shift+u",
"command" : "ctrl+c+ctrl+`+ctrl+shift+v+enter"
},
]
Without VS Code Extensions
There is a command : workbench.action.terminal.runSelectedText which does what you want already and is not bound to any keychord by default.
[Just to see the sendSequence command]:
If you don't go another route, this keybinding will run the selected text in the terminal:
{
"key": "alt+t",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "${selectedText}\u000D"
}
},
The \u000D is a return.
** [Added by Phillippe] :::
With a VS Code Extension (Macro Sequence Configurations)
VS Code currently doesn't allow concatenating shortcuts, so, if you want to execute the code on the Integrated Terminal and focus on it, you will have to install a macro extension.
There are several options for this. However, the suggested one is to use the multi-command (don't go for first results like macros, some of them are very outdated).
To create the shortcut mentioned above, create a sequence of commands in your settings.json file:
"multiCommand.commands": [ // Copy Paste to the Integrated Terminal and also Focus on it
{
"command": "multiCommand.copyPasteTerminalAndFocus",
"sequence": [
{
"command" : "workbench.action.terminal.sendSequence",
"args" : {"text" : "${selectedText}\u000D"}
},
"workbench.action.terminal.focus"
]
},
]
And then create a shortcut for it in the keybindings.json file:
{
"key": "alt+y",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.copyPasteTerminalAndFocus" }
},
My current dilemma is a difficulty in calling some commands. Essentially, I want to create a macro that performs the following operations in sequence:
Ctrl + D
Ctrl + C
Ctrl + ;
Ctrl + V
For those of you who know your keyboard shortcuts, it's evident that this does the following:
Selects the current word
Copies it to the clipboard
Opens the "Goto Anything" search box, and prefixes your query with a # sign, denoting you wish to search for a symbol/function
Pastes the copied word in the search box
I've attempted to create a macro for this by recording each keystroke, but Sublime only seems to want to process the more basic commands (i.e. the copy and the paste). And when I manually attempt to edit the .sublime-macro file, it gives me an error on the other two commands, like so:
Unknown macro command find_under_expand // <-- Ctrl + D
Unknown macro command show_overlay // <-- Ctrl + ;
Is there a way to have Sublime recognize that I want to call these commands? Am I formatting them incorrectly?
Disclaimer: My goal is, effectively, to create a slightly modified version of goto_definition (initially bound to F12). goto_definition will match the exact symbol in question, but I need to have it perform a fuzzy search in order to find similar symbol definitions.
(I have mirrored this post here: http://www.reddit.com/r/SublimeText/comments/2a56zo/commands_and_macros/)
Edit: the current contents of my .sublime-macro file are as follows:
[
{
"args": null,
"command": "find_under_expand"
},
{
"args": null,
"command": "copy"
},
{
"args": {"overlay": "goto", "text": "#"},
"command": "show_overlay"
},
{
"args": null,
"command": "paste"
},
{
"args": null,
"command": "left_delete"
},
{
"args": null,
"command": "left_delete"
}
]