How to hide options menu when cursor is inside single or double quotes in sublimetext - autocomplete

I am trying to use the autofilename plugin, but for some reason it replaces me or incorrectly corrects the file path, I think I know that it is because of the options that automatically appear as references when the cursor is inside single or double quotes by example:
As you can see, several options that do not exist such as directory or file names are shown, those names are mostly names that exist in class and id, among other things.
So how can it be avoided that only when the cursor is inside single or double quotes that these options do not appear.
As I mentioned I am trying to use the autofilename plugin but it automatically deletes or corrects me wrong data, I am using sublime text 4113

Probably the easiest way to do this is by assigning a custom key mapping to the filename autocomplete function. First, select Preferences → Key Bindings and add the following, ensuring that they are inside the outer [ ] characters (the file has to be valid JSON):
{
"keys": ["ctrl+super+alt+a"],
"command": "afn_show_filenames",
"context":
[
{
"key": "afn_use_keybinding",
"operator": "equal",
"operand": true
}
]
},
You can change the key combo to whatever you wish, just make sure it's not overwriting another Sublime or operating system key binding.
Next, open Preferences → Package Settings → AutoFileName → Settings—User and add the following line:
"afn_use_keybinding": true,
Now, to open the filename autocomplete, simply press CtrlWinAltA (on Windows and Linux) or Ctrl⌘AltA (on OSX/macOS).

Related

How can I change the key bindings in VS Code to change the tab key to four spaces?

I'm running the latest version of VS Code and Windows 10. I'm learning Python and would like all of my indentations to be spaces. However, I'd like to use the tab key to enter those spaces.
I've tried many different variations in the keybindings.json file with no luck.
[
{
"key": "tab",
"command": "-tab"
},
{
"key": "tab",
"command": "type",
"args": {"text": " "}
}
]
With the above code, I was just trying out many spaces to check the difference. However, when I save this as the json file, it doesn't give expected behavior. It will indent the full amount, but when I backspace I'd expect it to go back one space. Yet, it backs up to where a standard tab would be.. hitting it again returns to the beginning of the line.
Is it possible for the tab key to simply be used as a macro for four spaces?
You don't need to change it in json files. You can search Tab Size in VSCode Settings and change its value to 4. If this does not work, click the Editor: Detect Indentation link and disable it. This should solve the problem.

How to sync search term between tab groups?

It's common for me to have two tab groups open to compare things side-by-side but when I am searching for a term I find it a bit annoying that the search term isn't shared by the two editors. e.g. I ctrl-F3 on a word in the file to the left to start searching, then move over to the file on the right and I'd like to just hit F3 in order to continue searching for the same word, but it has it's own term saved to search for.
I assumed that this would be an option somewhere, but I don't see it in the Text Editor -> Find section of the pref's. Am I just blind and not seeing it or does this option not exist in VS Code?
Update: see https://stackoverflow.com/a/65851872/836330 "Searching only in open editors" might work for you.
I don't think there is an option to tie the find's together between groups like that. But with a macro you could do this fairly easily. Using a macro extension like multi-command put this into your settings:
"multiCommand.commands": [
{
"command": "multiCommand.findAcrossGroups",
"sequence": [
"editor.action.addSelectionToNextFindMatch",
"editor.action.clipboardCopyAction",
"workbench.action.focusNextGroup",
"actions.find",
"editor.action.clipboardPasteAction",
"editor.action.nextSelectionMatchFindAction",
"workbench.action.focusPreviousGroup" // to return to first group
]
}
]
and a keybinding to trigger it (in keybindings.json):
{
"key": "alt+s", // whatever binding you wish
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.findAcrossGroups" },
"when": "editorTextFocus"
}
How you would use this is when you trigger the keybinding, whatever word is under your cursor in group 1 will be transferred to the find widget in group 2 (and focus will return to group 1, don't use the last command "workbench.action.focusPreviousGroup" if you want to use this as you move to the next group. And then F3 works as you expect when you switch to group 2.
One downside to this approach is that it needs to use your clipboard to transfer the word under the cursor, that may not be an issue for you.
I made this to work with two groups primarily, although it really uses next/previous groups so it also works for any two adjoining group. It could be generalizeed to three groups pretty easily - beyond that it would probably be easier to make an extension to work with as many groups as you have open.

how to select real numbers by double clicking in VSCode?

because . is a word separator, a real number like 0.1 will be selected only 0 or 1.
but if I remove . from word separators, the whole method call such as a.b or a.b.c will be selected, rather than a, b or c selected.
furthermore, ' is also a possible separator between the digits, which can not be selected correctly as well.
so is there any extension that can solve this problem?
With the extension Select By v0.10.0 you can select the text surrounding the current selection described with a regular expression.
If you add the following to your settings
"selectby.regexes": {
"selectFloat": {
"surround": "[-+]?\\d+(\\.\\d+)?([eE][-+]?\\d+)?[fF]?"
}
}
Place the cursor somewhere inside the number and execute the command Select text range based on regex and select the option selectFloat from the QuickPick list.
You can add a keybinding if needed
{
"key": "ctrl+shift+f", // any key combo you like
"when": "editorTextFocus",
"command": "selectby.regex",
"args": ["selectFloat"]
}
This extension Quick and Simple Text Selection allows for selecting of everything within single quotes, double quotes, backticks, etc., although it requires keyboard shortcuts.

Is there a way to select code between certain characters

TL DR
Is there some extension, or other functionality, to select code above and below the cursor that is surrounded by some characters, e.g. %% or % ===== or anything else?
Why
I am coding Matlab in VSCode, since the builtin Matlab editor lacks a lot of useful functionalities. However, One thing that I miss is the ability to run 'sections':
%% section 1
x = 0:0.01:pi;
y = sin(y);
plot(x,y);
%% section 2
x = 0:0.01:pi;
y = cos(y);
plot(x,y);
%% section 3
x = 0:0.01:pi;
y = tan(y);
plot(x,y);
A section can be run using Ctrl+Enter. Since I use Matlab on Windows, there is no support for running the Matlab interpreter from the terminal/cmd.
So I created some AutoHotKey scripts that will copy selected code, open the Matlab window, paste it, and run. This works fine, however, when I want to run a very large section, this means that I have to select this manually, and press my defined shortcut to call the AutoHotkey script. Hence above question.
So say I have the cursor at some line insection 2, how can I select only the code in this section (including or excluding section 'headers', these are comments so does not matter).
I have made an extension Select By that allows the selection based on regular expressions relative to the cursor position. You can specify different expressions for forward and backward search and if you want to include the searched text in the selection.
You can set up to 5 regex searches.
The extension defines 5 commands that can be used in keyboard shortcuts
This can (of course) be done by writing a custom extension.
Since this seems like useful functionality in general and an interesting exercise, I've added the required capability to my personal vscode-smcpeak extension.
To use it:
Install vscode-smcpeak. This has to be installed manually for now. At time of writing, 0.0.7 is latest.
Install macros by ctf0. This one can be installed from within VSCode. Restart VSCode after installing it. (See below regarding the choice of "macros" extension.)
Add to your settings.json (File → Preferences → Settings, then click the "Open Settings (JSON)" icon in the upper right):
// Custom macros for ctf0.macros extension
"macros.list": {
// https://stackoverflow.com/questions/57749780/is-there-a-way-to-select-code-between-certain-characters
"selectPercentRegion": [
{
"command": "smcpeak.goToLineMatching",
"args": {
"regex": "^%%",
"moveUp": true,
"select": false,
"allowZeroMove": true
}
},
{
"command": "smcpeak.goToLineMatching",
"args": {
"regex": "^%%",
"moveUp": false,
"select": true,
"allowZeroMove": false
}
},
"smcpeak.revealCurrentSelection"
]
},
Add to keybindings.json (File → Preferences → Keyboard Shortcuts, then click the "Open Keyboard Shortcuts (JSON)" icon in the upper right):
{
"key": "ctrl+alt+m", // or whatever
"command": "macros.selectPercentRegion"
},
(Note: VSCode will always complain that macros.selectPercentRegion is not a valid command. That's a limitation of the "macros" extension.)
Then, with the cursor within a region delimited by the specified regexes, press the keybinding to activate the macro:
Why use two extensions?
The accepted answer, by rioV8 uses a single extension to answer the OP's question. This looks like a good solution to me, and is simpler to use than mine, so I approve of that being the accepted answer.
However, there is an advantage to my approach: it allows the behavior to be modified in settings.json. For example, the OP asked in a follow-up question about having the command additionally copy the text to the clipboard after selecting. Using the macro-based solution, this is an easy modification to settings.json; simply add:
"editor.action.clipboardCopyAction"
to the end of the selectPercentRegion macro. (That is the command normally bound to Ctrl+C.)
Thus, while my suggestion has more moving parts, I personally prefer this approach.
Which macros extension to use?
As Mark noted in a comment, the most popular macros extension, geddski.macros, is unmaintained. It furthermore has a rather serious bug in that the commands are not properly sequenced, leading to race conditions. (I actually saw this once during my testing.)
There are two forks of geddski.macros on the marketplace, l7ssha.macrosRe and ctf0.macros. l7ssha.macrosRe only adds the "delay" ability, which is an awkward way to resolve the race conditions. ctf0.macros, on the other hand, fully fixes the race condition (via a complete rewrite).
Therefore, I recommend ctf0.macros, and in fact have edited my answer to use it rather than the original geddski.macros extension.
However, be aware:
ctf0.macros uses macros.list rather than macros in settings.json
ctf0.macros requires a restart of VSCode after installation to activate
How it works
The additions are in vscode-smcpeak commit 6d5f5d4689.
My extension registers a new command, smcpeak.goToLineMatching, that searches up or down for a line containing a regex. The logic is very simple, just a loop and regex test.
It also registers smcpeak.revealCurrentSelection, needed because otherwise the selected text might be offscreen and nothing scrolls to it by default.
The macro selectPercentRegion uses goToLineMatching to look upwards for one regex, setting the anchor (selection start) there, then uses it again to look down for another regex (although they are the same here), extending the selection to that point. Then it scrolls to ensure the selection is visible.
It should be straightforward to transplant this code to your own extension if you don't want the rest of the stuff that mine provides. (But mine does not bind any keys, so there should be no downside to having the extra stuff.)
Here is another way to do this, with an extension I wrote Find and Transform.
Make this keybinding in your keybindings.json (or it could be a setting too):
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "^(%%[\\s\\S]+?)(?=^$)|(^%%[\\s\\S]+)",
"run": [
"$${",
"const sections = vscode.window.activeTextEditor.selections;",
"await vscode.commands.executeCommand('cursorUndo');",
"const mySection = sections.find(section => section.contains(vscode.window.activeTextEditor.selection));",
"if (mySection) {",
"vscode.window.activeTextEditor.selections = [mySection];"
"}",
"}$$",
],
"isRegex": true,
"postCommands": "editor.action.clipboardCopyAction"
}
}
The find regex finds and selects all %% .... sections. But since you only want the section where your cursor started, some code is run to restore that original cursor and then find that section that contains the one current cursor. Then that section is selected. And in the postCommand that selection is saved to the clipboard.
That is using javascript and the vscode extension api within a keybinding or setting.

How to prevent "."(dot) from cancelling autocomplete in Sublime Text 2?

I have defined some keywords for a proprietary language I use at work:
{ "match": "\\b(util.tickettimelimit|util.user_ip|util.server_name|util.today)\\b",
"name": "keyword.source.GTX2",
"comment": "Tags"
}
I also have a completion file:
{
"scope": "source.GTX2",
"completions":
[
"util.server_name",
"util.tickettimelimit",
"util.today",
"util.user_ip"
]
}
When I start typing "util" I see the correct autocomplete options:
But as soon as I enter the "."(dot) autocomplete options go away:
Is there a way to change this behavior? I just want the keywords to be trated as a whole thing and ignore the dots.
Thanks!
I've looked everywhere I can, and it seems the auto-complete code is embedded within the executable itself (at least on Windows, I haven't checked my Mac yet), and not in one of the numerous external .py files scattered around, so I can't even see the parameters for how auto-completion is performed. I looked through the default Packages/Default/Preferences.sublime-settings file and while there are several options relating to auto-complete, there are none relating to what we're looking for. While looking through the Default (Windows).sublime-keymap file in the same directory, I tried adding the following:
{ "keys": ["."], "command": "hide_auto_complete", "context":
[
{ "key": "auto_complete_visible", "operator": "equal", "operand": false }
]
},
but alas it didn't work. There are a number of auto_complete commands there, and while this looked the most promising I haven't tried the rest.
I haven't exhaustively looked through the source and config files for the nifty SublimeCodeIntel plugin (also available through Package Control), so it's possible you might be able to find an option there. You'd probably have to completely disable the built-in auto-complete functionality first, so it doesn't override SCI.
So, I guess for now there's not much you can do. You can always leave a feature request and see if it makes it into Sublime Text 3, or search/open a thread on the Sublime Text Forum and see if anyone else has any suggestions. Good luck!
What I think #Ashish is alluding to is the word_separators setting. You will want to create a syntax specific preference (Preferences -> Settings - More -> Syntax Specific - User). Create a word_separators entry with the dot removed (Copy from the default preferences as the base). This will give you the behavior you want but there are some things to note. The dot, obviously, will not be treated as a word separator, which will change some behavior.
I'll use java as an example. If I had a variable foo, with some method bar, I could enter foo.b and bar would be shown as a completion. Without the dot as a separator, you will not see this.
Another example, perhaps easier to understand is when selecting words. If you use ctrl/cmd + d to select the word, it selects words, bound by word separators. So if I had foo.ba|r, where the | represents the cursor position and used ctrl/cmd+d it would select bar. With the dot removed as a word separator, foo.bar would be selected.
Let me know if I can clarify anything.
It's a little late but I hope this can help, create a new plugin and add this code:
import re
myObjects = {"util": ["server", "tickettimelimit", "today", "user_ip"]}
class CustomAutocomplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(0, "source.GTX2"):
return
if prefix == "":
# get the last word in the current line
currentposition = view.sel()[0].begin()
currentline = view.full_line(currentposition)
text = view.substr(currentline)
words = re.findall("\w+", text)
lastword = words[-1]
if lastword in myObjects.keys():
# return a list of tuples, it represents snippets
# the first element is the trigger and the second one
# is the content
return [(x, x) for x in myObjects[lastword]]
return []
And add the next key in the user settings:
"auto_complete_triggers":
[
{
"characters": ".",
"selector": "source.GTX2"
}
]
Don't press . (dot) else you will need to type at least one character after dot so list can appear again. Using Brackets or Dot tells Sublime Text 2 that user has completed typing.
example: if I type for then sublime will show dropdown list but if I type for( list will disappear.
Click on Preferences > Settings - User, then copy and paste the following
// Characters that are considered to separate words – does not include periods.
// Place comma at the end of the line if there are multiple keybindings.
"word_separators": "/\\()\"‘-:,;~!##$%^&*|+=[]{}`~?"
From this webpage:
http://tomschenkjr.net/using-sublime-text-2-for-r/