Fred
Fred

Reputation: 159

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?

Upvotes: 5

Views: 3173

Answers (3)

dirkk0
dirkk0

Reputation: 2570

Another solution would be to change the file type at the bottom right; for a txt file I changed this to CMake and then the commenting out works. The only disadvantage is that you have to do this again when you reopen the file. Still the easiest workaround for me.

Upvotes: 1

Fred
Fred

Reputation: 159

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!!

Upvotes: 2

Mark
Mark

Reputation: 182641

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:

demo of commenting a file with no extension.


There are some limitations to this approach rather than a full-blown extension.

  1. The last cursor position will not be remembered.

  2. Empty lines will be commented - as you can see in the demo.

  3. When block-commenting lines of different indentation, the comment #'s won't line up.

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

Upvotes: 0

Related Questions