How to disable/override the enter key for autocomplete? - autocomplete

In Sublime Text 3, I want to disable the enter key to select an item from the autocomplete drop down, and only allow the tab key to do so.
I found this section in the inbuilt Default (OSX).sublime-keymap file:
{ "keys": ["enter"], "command": "commit_completion", "context":
[
{ "key": "auto_complete_visible" },
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
]
},
It seems that if I remove this from the config that enter will not select an item in the drop down. Unfortunately it is not recommended to change this file, and only to override it in my User files. I don't think I actually can edit it without modifying the .app contents.
I tried to override it by removing different sections, and also remove everything except "keys": ["enter"], but nothing seems to work.
How would I go about achieving this without modifying the inbuilt Default (OSX).sublime-keymap and only the User/Default (OSX).sublime-keymap file?

I have never used Sublime Text 3, but I don't think the following has changed since Sublime Text 2.
What you want to achieve is actually a standard feature in Sublime Text. You just have to turn it on.
This line from your the code you quoted …
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
… means "only execute the command if the setting called 'auto_complete_commit_on_tab' is set to false". So simply turn on that setting.
In Default/Preferences.sublime-settings:
// By default, auto complete will commit the current completion on enter.
// This setting can be used to make it complete on tab instead.
// Completing on tab is generally a superior option, as it removes
// ambiguity between committing the completion and inserting a newline.
"auto_complete_commit_on_tab": false,
Put "auto_complete_commit_on_tab": true in User/Preferences.sublime-settings. Both mentioned files can be accessed via the Preferences menu.

You can assign it to a non existent command. Try adding the following to User/Default (OSX).sublime-keymap
{ "keys": ["enter"], "command": "noop", "context":
[
{ "key": "auto_complete_visible" },
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
]
}
Granted if you install/write a plugin that has a command noop you will need to change this command.
Edit
Lydell's solution is better :) Forgot about that setting (though it is in the context so I should have known...). Guess my answer is a more generic "how to disable a keybinding".

tried the solutions given above but they didn't work.
after some work this is what i came up with.
{ "keys": ["enter"], "command": "hide_auto_complete", "context":
[
{ "key": "auto_complete_visible" },
{ "key": "setting.auto_complete_commit_on_tab", "operand": false }
], "command": "insert", "args": {"characters": "\n"}
}

Related

Make jump to bracket and select to bracket in VSCode stay within brackets (like Sublime)

I am trying to switch from Sublime Text to VSCode and two commands I'm quite dependent on are jumping and selecting between brackets which seem to differ between in VSCode compared to Sublime.
I've found an option in VSCode to do this, namely editor.action.jumpToBracket and editor.action.selectToBracket. The problem is that these differ to the ones in Sublime.
Jumping between brackets in Sublime stays within brackets and selecting does not include brackets, while in VSCode jumping moves outside of the brackets and selecting also includes brackets.
I would like to preserve the Sublime functionality. Is there a simple way to do that in VSCode?
For the selection you can use the extensions
multi-command
Select By
Add the following key binding
{
"key": "ctrl+i ctrl+s", // or any other combo
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.selectToBracket",
{ "command": "selectby.moveLastSelectionActive", "args": {"offset": -2} },
{ "command": "selectby.moveLastSelection", "args": {"offset": 1} }
]
}
}
It only works for a single selection.
I will add commands selectby.moveSelectionActive and selectby.moveSelection that will act on all selections.
The brackets is a bit more difficult because jump-to-bracket places the cursor before the bracket, but you have no idea if you jump to the open-bracket. If you have multiple brackets it is even harder: ((()()(())))
Edit
With v1.17.0 of Select By it operates on all selections.
Modify to:
{
"key": "ctrl+i ctrl+s", // or any other combo
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.selectToBracket",
{ "command": "selectby.moveSelections", "args": {"start": 1, "end": -1} }
]
}
}

How to search in currently opened folder using selected line

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.]

Code scope depending "when" rules for keybindings?

i'm new in VSCode, but in Sublime Text it's possible to assign shortkeys with a code scope context, like:
{
"keys": ["tab"],
"command": "insert",
"args":
{
"characters": "\n"
},
"context": [
{
"key": "selector",
"operand": "source.css meta.rule.css meta.declaration-list.css",
"operator": "equal",
"match_all": true
}]
}
That means, when my cursor is at the scope source.css meta.rule.css meta.declaration-list.css and when I press tab key, I'll insert new line. Can I do like that in VSCode, because I try to search for the list of "when" rules, that's what I had found Visual Studio Code list of keyboard shortcuts options and there is nothing about code scope context, or comparing current line text, or something like that.

How to set hotkey to move cursor out of quotes/parenthesis?

In Sublime I could easily set a more complex hotkey that lets me exit quotes and parenthesis by pressing Enter. It is here below:
// Move out of single and double quotes with `Enter`
{
"keys": ["enter"],
"command": "move",
"args": {"by": "characters", "forward": true},
"context": [
{ "key": "following_text", "operator": "regex_match", "operand": "(?:\"|').*", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?:\"|')", "match_all": true }
]
},
In VS Code, is there any way to achieve this? This in keybindings.json moves the cursor, but it is active when I don't want too. Thanks.
{ "key": "enter", "command": "cursorRight",
"when": "editorTextFocus" }
Check out this extension which does what you want - https://marketplace.visualstudio.com/items?itemName=albert.TabOut
And you can find the implementation here - https://github.com/albertromkes/tabout
I found Ctrl+Shift+\ useful for moving out of quotes. Also, it could be remapped by searching jumpToBracket in Keyboard Shortcuts.
See full VSCode keyboard binding here.
In VS Code, you can type the closing quote (i.e. if using double quotes)
{shift+'}
while you are INSIDE the quotes to have it exit outside the closing quote. This also works for parenthesis and brackets, just type the closing one (i.e. ) or ]). You can also jump straight to a new line by pressing
{ctrl+enter}
which exits out of any brackets, parenthesis, quotes you are already in. These 2 methods here should be embedded in standard VS Code AFAIK.

How to use conditions/context in ST3 macros?

I am trying to modify the Ctrl+Enter-macro so that it appends a semicolon to the end of the line before writing a newline, similar to this question.
The basic solution is pretty simple
[
{"command": "move_to", "args": {"to": "hardeol"}},
{"command": "insert", "args": {"characters": ";\n"}}
]
, however, it has two problems:
1) If there is already a semicolon at eol, it will be duplicated. Is there a way to include a condition of ( preceding_text == ";" ) similar to keybindings'
{ "key": "preceding_text", "operator": "regex_match", "operand": ";$" }
and have the macro run one of two different insert commands depending on that?
2) As it is, the plugin runs independent of language, also inserting semicolons, for example, in html. Again, is there a way to make inserting the semicolon optional depending on the scope?
I found a pretty good workaround. I still don't know, if it's possible to add this kind of condition in the macro itself, but it can be substituted by adding the conditions in the key-bindings.
First, create a second macro "Packages/User/Add Line Semicolon.sublime-macro" with
[
{"command": "move_to", "args": {"to": "hardeol"}},
{"command": "insert", "args": {"characters": ";\n"}}
]
In the user-keybindings add
{ "keys": ["ctrl+enter"], "command": "run_macro_file", "args": {"file": "res://Packages/User/Add Line Semicolon.sublime-macro"}, "context":
[
{ "key": "following_text", "operator": "not_regex_contains", "operand": ";$", "match_all": false },
{ "key": "selector", "operator": "equal", "operand": "(source.css, source.scss) - comment", "match_all": false },
]
},
This listens to the same keys as the normal ctrl+enter, but calls the new macro and only triggers if certain conditions are met.
First, the text after the cursor may not end with a semicolon. If there's already a semicolon at the end of line, this binding will not trigger and the shortcut will be passed through to the default binding instead. Note that, since it only checks the text after the caret, this will not work if your cursor is already at the end of the line.
Secondly, the position of the caret must have the appropriate scope. For this example, I just included css and scss files, and it only matches if you're not currently in a comment. Again, if the condition fails, the shortcut will be passed through to the default macro.