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

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.

Related

Extract the content of OUTLINE ( or AL OUTLINE ) from VS Code

Is there any bash command so that we can extract the content of OUTLINE or AL OUTLINE section of the VS Code and write the same into some text document ?
I made a VSCode extension to accomplish this.
Extension page
GitHub Repo
Install and run ctrl + shift + p -> List Symbols
If you don't get a better answer, you can try the Show Functions extension.
It can produce an output of a (clickable) list of functions and symbols into a separate editor which you can then Ctrl-A to copy and paste.
You don't say what languages you are using, I use the following for .js files:
"funcList": {
"doubleSpacing": true,
"filters": [
{
"extensions": [
".js"
],
"native": "/^[a-z]+\\s+\\w+\\s*\\(.*\\)/mgi",
"display": "/\\S* +(\\w+\\s*\\(.*\\))/1",
"sort": 0
}
]
}
which captures and displays the function name and args like:
loadCountryTaxonomy(country)
toggleSearchResultsPanel()
updatetaxArticleQueries(data)
but you can modify the regex to your requirements. I don't try to list symbols other than functions but apparently you can with this extension.

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.

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

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

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