How to bind ["alt+f,n"] in sublime on mac? - sublimetext

I'm trying to bind Alt+FN key sequence to a specific command. It only inserts an ƒ character, however.
[
{ "keys": ["alt+f,n"], "command": "advanced_new_file_new"}
]
How do I bind it correctly so that when I click alt+f and then n the command is executed?
Btw, if I change the binding to just ["alt+f"] the binding works, it's not what I want though.
EDIT:
sublime console says:
Unable to parse binding {command: advanced_new_file_new, keys: [alt+f,n]}
investigating further to understand why this is invalid..

I figured it out.. problem was I was looking into old sublime text documentation, which uses xml for key bindings. Sublime 3 uses json.
All I had to do was change this:
[
{ "keys": ["alt+f,n"], "command": "advanced_new_file_new"}
]
to this:
[
{ "keys": ["alt+f","n"], "command": "advanced_new_file_new"}
]
docs: http://docs.sublimetext.info/en/latest/reference/key_bindings.html

Related

Visual Studio Code multi-line comment for files with no extension

I'm editing a file with no extension (for example... or a file with an unknown extension, etc), and when i try to multi-line comment using Cmd+/ it doesn't do anything. I can change the extension to .py or .yaml to get the # comment feature i'm looking for, but that's a total pain. Is there a way to tell code "act like a .py extension on the file" type of thingy?... or maybe replace CarriageReturn+LineFeed with CarriageReturn+LineFeed plus # plus space ... or FORCE the Cmd+/ to work?
Finally found a couple of solutions that works... NO extensions, etc...
Just hold (mac)Option+Cmd (while in column 1), then click down arrow to multi-cursor select all of the lines, then simply type # to put that in col.1, and remainder will move over 1 column to right... you can do this again, to add another #, for example.
Also, another option is to:
-Go to Selection> ColumnSelectionMode, (put into column mode) then drag down, does "multi-select"!! ..then type # to comment lines, for example!!
I am a little surprised but this seems to work - test it out. You will need a macro extension of some sort. Here I am using the multi-command extension.
In your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.addCommentNoExtension",
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHome",
{
"command": "type",
"args": {
"text": "# "
}
},
"removeSecondaryCursors"
]
},
{
"command": "multiCommand.removeCommentNoExtension",
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHome",
"deleteRight",
"deleteRight",
"removeSecondaryCursors"
]
}
]
In your keybindings.json put these keybindings:
{
"key": "cmd+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.addCommentNoExtension" },
"when": "editorTextFocus && resourceFilename =~ /^(?!.*\\.)/"
},
{
"key": "shift+/",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.removeCommentNoExtension" },
"when": "editorTextFocus && resourceFilename =~ /^(?!.*\\.)/"
},
Here is a demo:
.
There are some limitations to this approach rather than a full-blown extension.
The last cursor position will not be remembered.
Empty lines will be commented - as you can see in the demo.
When block-commenting lines of different indentation, the comment #'s won't line up.
You have to use one keybinding Cmd+/ to add comments, and another binding Shift+/ to remove comments. I don't think there is a way to make it a one-command toggle.
The really interesting part of this answer is how files with no extension are targeted. In the keybindings there are when clauses that include resourceFilename =~ /^(?!.*\\.)/.
When clauses can take a regex to apply to certain keys like resourceFilename. See when clause operators. The regex /^(?!.*\\.)/ says to apply this keybinding when the filename does not include a literal .. It uses a negative lookahead asserting that there is no . following any characters.
I wasn't sure that was possible but it seems to work. The Cmd+/ command still works as it should with other file types.
You could use "when": "editorTextFocus && editorLangId == plaintext" and that would work as long as your files with no extension remained as plaintext langId files. It isn't quite as specific as the when clause I used above.

Automatically enclose a string within another string?

Let's say that I have some string, X, and that I want this to be enclosed within some other strings, for example like this: \emph{X}. Is there some tool that lets me do this quickly, for example through selecting the text and pressing a short-cut on my keyboard? I'm working in Sublime text in macOS Sierra.
This is something that is possible from directly within Sublime using a key binding that inserts a snippet, where the snippet body is told to include the text that is currently selected.
For your example above, the following key binding will wrap the selection in \emph{}. I used Super+W for my own testing, but you probably want to pick something better for your own purposes.
{
"keys": ["super+w"],
"command": "insert_snippet",
"args": {"contents": "\\emph{${0:$SELECTION}}"},
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
The inclusion of context here makes the key binding only active while all of the cursors have at least one character selected. If desired you can remove the context section entirely, in which case the key binding will either insert the snippet and leave the cursor between the braces, or wrap the selection, depending on the state of the selection.
If your snippet is more complex and involved than this (e.g. multiple lines), trying to insert the entire body of it into the key binding can be a little taxing. In that case, you probably want to use an external snippet instead.
To do that, you can select Tools > Developer > New Snippet... from the menu, and use a snippet such as the following, which you should save in the location that Sublime defaults to:
<snippet>
<content><![CDATA[
\emph{${0:$SELECTION}}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<!-- <tabTrigger>hello</tabTrigger> -->
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>
With this in place, the key binding from above needs to be modified to tell the insert_snippet command to insert the snippet with a particular file name instead of with raw content:
{
"keys": ["super+w"],
"command": "insert_snippet",
"args": {"name": "Packages/User/emph.sublime-snippet"},
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
Things to note here are that the key binding files are JSON, and so the contents of the snippet need to be adjusted slightly to make them valid JSON. In the example above that means using \\ instead of just \ to specify \emph.
Snippets in general also have their own special characters in them, so if you for example need to insert a $ you need to quote it as \$ so that Sublime knows what you mean; this is true regardless of whether the snippet is in a snippet file or inline.
More information on key bindings and snippets can be found in the Unofficial Documentation for a more complete picture of everything that's possible with them.

set auto complete on sublime text 3 for custom html elements

Good day,
how can I set auto complete on sublime text 3 for custom html elements, like for example if I type: dog then press tab it will become <dog></dog>.. thanks for your answer.
You can create a custom .sublime-completions file for this. Create a new file with JSON syntax in Sublime, using the following contents (of course customized to your needs):
{
"scope": "text.html - source, punctuation.definition.tag.begin",
"completions":
[
{ "trigger": "foo", "contents": "<foo>$0</foo>" },
{ "trigger": "bar", "contents": "<bar class=\"$1\">$0</bar>" },
{ "trigger": "baz", "contents": "<baz class=\"${1:myclass}\">$0</baz>" }
]
}
In the first example, typing foo and hitting Tab will insert <foo>|</foo> where | is the cursor position.
In the second example, typing bar and hitting Tab will insert <bar class="|"></bar>. The cursor will first be between the quotes following class= so you can enter your own class. Hitting Tab again will place the cursor just before </bar>.
The third example works just like the second, except that the class now has a default value. This value will be highlighted after typing baz and hitting Tab - you can either hit Tab again to keep the default value and move the cursor between the opening and closing tags, or you can enter your own value, hit Tab, and move the cursor to between the opening and closing tags.
Once you have everything set up how you like, save the file as Packages/User/HTML.sublime-completions, where Packages is the folder opened when you select Preferences -> Browse Packages....
For more information, follow the link above. To learn more about placeholders and variables, check out this documentation.
Good luck!

Can the way Sublime Text 2 auto encloses selections in pairs be changed?

In Sublime Text 2 you can type a string and then highlight some section of the string like the following:
Then you can enclose the selected text in " by just typing a single " which gives you this:
When you do that the cursor ends up between d and ", I'd really like it to end up outside of the " at the end of the selected string.
Is something like this possible?
This isn't a feature I use, but the following seems to work ok. Create a snippet like this:
<snippet>
<content><![CDATA["$SELECTION"$0]]></content>
</snippet>
The $0 specifies the cursor exit point. Keybind it in your /Packages/User/Default (Linux).sublime-keymap file:
{ "keys": ["\""], "command": "insert_snippet",
"args": {"name": "Packages/User/quotes.sublime-snippet"}}

Replace text in a Visual Studio Code Snippet Literal

Is it possible to replace text in a Visual Studio Snippet literal after the enter key is pressed and the snippet exits its edit mode?
For example, given a snippet like this:
public void $name$
{
$end$
}
If I were to enter $name$ as:
My function name
is it possible to have Visual Studio change it to:
My_function_name
or
MyFunctionName
After many years there is an answer, for anyone still coming across this question:
"Replace Whitespaces": {
"prefix": "wrap2",
"body": [
"${TM_SELECTED_TEXT/[' ']/_/gi}",
],
"description": "Replace all whitespaces of highlighted Text with underscores"
},
Add this to your user snippets. Or alternativly you can add a keyboard shortcut like this:
{
"key": "ctrl+shift+y",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "${TM_SELECTED_TEXT/[' ']/_/gi}"
}
},
Hope this helps anyone coming across this in the future
It's great. I used it to wrap things in a function with quotes. If a selection has quotes it can remove the quotes. In the snippet part, it seems to break down into:
TM_SELECTED_TEXT - this is the input
[ ' '] - regex find
_ - regex replace
gi - global flag for regex
So what I wanted is change: "User logged in" into: <%= gettext("User logged in") %>
For this in the snippet I used:
"body": ["<%= gettext(\"${TM_SELECTED_TEXT/['\"']//gi}\") %>"],
Note: you need to escape the quotein the regular expression, therefore: " becomes \".
In my case the goal was to replace a single instance of the word Authoring with Baker:
${TM_FILENAME_BASE/(Authoring)/Baker/}

Resources