How to search in currently opened folder using selected line - visual-studio-code

I am using vscode. How can I search (workbench.view.search) in currently opened folder using selected line (expandLineSelection) via a keyboard shortcut.

Using an extension I wrote, Find and Transform, and this keybinding:
{
"key": "alt+z", // whatever keybinding you want
"command": "runInSearchPanel",
"args": {
"isRegex": false,
"triggerSearch": true,
"filesToInclude": "${relativeFileDirname}", // many other path variables as well
// all the other search options are available, like 'matchCase',
// 'find', 'replace, etc.
}
}
any selection will be searched for in the current file's parent directory. If you don't specify a find query, the selection will be used as the query.
You could put this together with a macro extension, here using multi-command, like this:
{
"key": "alt+z",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"expandLineSelection",
"cursorLeftSelect", // to get rid of the trailing newline
{
"command": "runInSearchPanel",
"args": {
"isRegex": false,
"triggerSearch": true,
"filesToInclude": "${relativeFileDirname}"
}
}
]
},
"when": "editorTextFocus"
}
[I see that expandLineSelection includes the trailing newline. But it doesn't seem to affect the resuts. In any case, I added "cursorLeftSelect" to the above macro to get rid of that trailing newline.]

Related

VS Code creating custom shortcuts for common methods with line break (e.g. dd($var) or console.log($var))

First, I have a shortcut on VSCode to Wrap a text with the thing I'm typing.
<div>
Hello World
</div>
If I select "World" and use the Emmet: Wrap with Abbreviation shortcut and type span I can make this:
<div>
Hello <span>World</span>
</div>
But here's the thing :
I know we can create custom wrapping that are not the same on each side of the word we selected (source: VS Code : create custom snippet/shortcut)
{
"key": "ctrl+i",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "{something}$TM_SELECTED_TEXT{/some other thing}"
},
"when": "editorTextFocus && editorHasSelection"
}
What I would like is to select my variable, then use the shortcut, and it will print what I need UNDER the selected line, and whitout breaking the current line where I come from.
For this example, I'm selecting the var $user_id, press shortcut, and then boom it will add the second line.
$user_id = User::where('user_name', $user_name)->get()->first()->id;
dd($user_id);
Here's a start:
{
"key": "ctrl+alt+c",
"command": "editor.action.insertSnippet",
"args": {
"snippet": " ==> here we need to find how to line break without spliting the code and then: <==
{console.log(}$TM_SELECTED_TEXT{)}"
},
"when": "editorTextFocus && editorHasSelection"
}
Do you think it is possible ? Maybe the solution is to use Keyboard Macro separately from VSCode ?
I came up with this solution, thanks to #Mark in comments, related to this thread : How can I insert a snippet on a new line with vscode?
Install Multi-command VSCode extension
Open the settings file of the extension (settings.json)
Implement your code (here's mine with console.log() and dd() )
"multiCommand.commands": [
{
"command": "multiCommand.console.log",
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "console.log(\"$CLIPBOARD: \", $$CLIPBOARD)\n$0"
}
},
]
},
{
"command": "multiCommand.dd",
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "dd($$CLIPBOARD);"
}
},
]
}
Implement the shortcut in your VSCode settings (keybindings.json)
{
"key": "ctrl+1",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.console.log" }
},
{
"key": "ctrl+2",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.dd" }
}
If I'm understanding your question correctly, you can probably just use $0 to show where your cursor will end and use \n to insert a line break.
However, I'm not entirely sure if this works when creating a snippet from the Keyboard Shortcut file, but it works from the snippet file so I'm assuming it'll work here.

VS Code insert predefined characters at the end of current line

I would like to assign a keyboard shortcut which-
puts the cursor at the end of current line
and then, insert ->
I googled a lot. But not found any satisfactory solution.
Any idea ?
If your cursor doesn't start at the end of the line you will need to use a macro extension to run two commands. Using multi-command below, make this keybinding (in your keybindings.json):
{
"key": "alt+w", // whatever keybinding you like
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"cursorEnd",
{
"command": "type",
"args": { "text": "->" }
}
]
},
// "when": "editorLangId == php" // for example
}

VSCode: Toggle between defined texts

I have to toggle between two texts example ABC and MNO by any keyboard shortcut or mouse click.
Like if I double click and selected word is ABC, It should be changed to MNO or vice versa.
Or after selecting the word I can run some keyboard shortcut to toggle.
Or if current line has ABC it gets changed to MNO with a double click or keyboard shortcut.
I know it's a very weird request but I have a use case. Any sort of work around will work even if I have to write an extension.
I have written an extension Replace On that does this "immediate" replace on double click.
You can add the following to your settings.json
"replace-on.selection-changed": {
"all": {
"ABC": {
"replace": "MNO",
"literal": true,
"immediate": true
},
"MNO": {
"replace": "ABC",
"literal": true,
"immediate": true
}
},
"javascript": {
"foo": {
"replace": "Bar",
"flags": "i"
}
}
}
I have added, for the example, that you could limit the rules for a particular languageId.
In the next version I will add the possibility to change the current "word" with the non-immediate rules by invoking a command. From command palette or key binding.
I forgot that I made an extension that does this pretty easily, see Find and Transform.
This keybinding:
{
"key": "alt+y", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "(Cookie)|(SkipCookie)",
"replace": "${1:+SkipCookie}${2:+Cookie}",
"isRegex": true,
"restrictFind": "line"
// "matchCase": true
}
}
That will perform a conditional replacement of Cookie with SkipCookie and vice versa. Replacing all occurrences of each on the line.
"Or after selecting the word I can run some keyboard shortcut to toggle."
You can make a snippet do this with a conditional replacement. In your keybindings.json:
{
"key": "alt+t", // whatever keybinding you want
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/(ABC)|(MNO)/${1:+MNO}${2:+ABC}/}"
},
"when": "editorTextFocus && editorHasSelection"
}
If the selected text is ABC it will be replaced with MNO and vice versa. It will work with multiple cursors too.
${1:+MNO} means if there is a capture group 1, i.e., ABC replace it with MNO.
This form works to toggle all occurrences in a line if you select the line first and then trigger the keybinding:
{
"key": "alt+t",
"command": "editor.action.insertSnippet",
"args": {
// "snippet": "${TM_SELECTED_TEXT/(ABC)|(MNO)/${1:+MNO}${2:+ABC}/}"
"snippet": "${TM_CURRENT_LINE/(.*?)((ABC)|(MNO))/$1${3:+MNO}${4:+ABC}/g}"
},
// "when": "editorTextFocus && editorHasSelection"
"when": "editorTextFocus"
}

How do I disable inserting a new empty line when I press Enter when the cursor is in brackets?

Steps to reproduce:
simple code:
if () {}
cursor is between {}
i press Enter
result:
if () {
}
expected result:
if () {
}
I want a empty line not to be inserted.
It may be that it works by default(adds a empty line), and when Alt+Enter it doesn't add a empty line.
I did not find settings in vscode. I didn't find anything on google.
I tried this:
{
"key": "alt+enter",
"command": "type",
"args": {
"text": "\n"
},
"when": "editorTextFocus"
}
Because Alt+Enter does nothing by default.
However, the onEnterRules function used with the editor.autoIndent option detects the addition of the \n character and adds an extra empty line anyway. :(
I want to use editor.autoIndent. But I want to turn off (do not turn on) using the shortcut Alt+Enter.
Worst option: look for an extension that does exactly the same as editor.autoIndent, but has the ability to create a shortcut Alt+Enter to work the way I want.
You can use the extension multi-command and construct a command that does what you want.
Add this to your settings.json (global or workspace)
"multiCommand.commands": {
"multiCommand.lineBreakNoEmptyline": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
}
}
Add this to your keybindings.json:
{
"key": "alt+enter",
"command": "multiCommand.lineBreakNoEmptyline",
"when": "editorTextFocus"
}
Or using the keybinding only method
{
"key": "alt+enter",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"lineBreakInsert",
"deleteWordRight",
"cursorRight",
"cursorHome"
]
},
"when": "editorTextFocus"
}
I'll show the info from my comment here so that it is clearer. This keybinding:
{
"key": "alt+enter", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
// "lineBreakInsert",
{
"command": "type",
"args": {
"text": "\n"
}
},
"editor.action.clipboardCutAction"
]
},
}
The type command acts differently than a lineBreakInsert command so it is a little easier to then delete that extra line as the cursor is already there. It is just a small improvement, 2 less commands.
Demo:

Visual Studio Code keybindings - Running two or more commands with one shortcut

I have the following keybinding in VS Code which toggles the position of the cursor between the active document and built-in terminal:
// Toggle between terminal and editor focus
{
"key": "oem_8",
"command": "workbench.action.terminal.focus"
},
{
"key": "oem_8",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
}
Before i click the shortcut key to move the cursor to the terminal, i first have to save the active file.
I would therefore like to run the file saving command, which after searching on google i believe is workbench.action.files.save
How would i do this? I have tried adding the above code snippet at the end of the "command" line but it has not worked.
You would need a macro extension to run multiple commands from one keybinding.
I now use multi-command and there are other macro extensions now.
You can use this keybinding (in your keybindings.json) with the multi-command extension - no need for anything in settings.json:
{
"key": "oem_8", // or whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
]
},
"when": "editorTextFocus" // if you want this, you probably do
}
If you have more complicated macros you can still build them in your settings.json if you wish.
There is a way to run a sequence of commands without any extensions. It is by using Tasks. I like this method, because it allows to define a command, that is in workspace scope, not in global scope. The idea was taken from this blog post.
Example of tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmd-1",
"command": "${command:workbench.action.files.save}"
},
{
"label": "cmd-2",
"command": "${command:workbench.action.terminal.focus}"
},
{
"label": "cmd-All",
"dependsOrder": "sequence",
"dependsOn": [
"cmd-1",
"cmd-2"
],
}
]
}
Then in keybindings.json you just bind your hotkey to the task:
{
"key": "oem_8",
"command": "workbench.action.tasks.runTask",
"args": "cmd-All"
}
Note, that when defining a task, you can pass arguments to the command with the help of input variables.
Another extension to run multiple commands: Commands
{
"key": "oem_8",
"command": "commands.run",
"args": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
],
"when": "editorTextFocus"
}
I made this extension. It's great.