Does anyone know is it possible to set file associations in vscode?
I'm working with .pt files which contain html and was looking to set the editor, so that when it opens these files it automatically set the type to html.
Currently changing a mode is not persisted on a file. For the future we will look into an option to allow the user to associate file extensions to modes and persist this setting.
Update for our VS Code 1.0 release:
You can now persist a file to language association in settings. The newly introduced files.associations setting can be used like this:
files.associations: {
"*.php4": "php",
"*.php3": "php"
}
If switching language using the bottom-right menu is annoying you - I have an interim solution, it isn't automated but it allows keyboard-switching:
[
{
"key": "ctrl+l",
"command": "workbench.action.editor.changeLanguageMode",
"when": "editorTextFocus"
}
]
You can now type CTRL + L then HT then ENTER.
VSCode remembers what language you have selected and even remembers that language if you copy and paste files (either in VSCode or on the file system). I can't see where they have stored that information though.
So you would only need to do this for brand new files.
Related
I often find myself working on file test-1a.robot and I want to open test-1b.robot, which is the next alphabetical file in the editor list.
With the files are open and I have a "sort tabs" extension enabled I can use ctrl+pgup and ctrl+pgdn which is already something, but I'd like to have it even without the tab sorting or opening files in advance.
Maybe you need to train yourself in the following key combo:
Ctrl+Shift+E DownArrow Enter
I tried to use multi-command to create a combo of
show explorer tab
go to next file down
open this file
For (1.) I could find a command, it sets the focus on the current file in the Explorer.
Edit With the tip from mark for the commands to type the cursor down (2) and select command (3) in listboxes.
You can add this to your settings and create a key binding for this new command
"multiCommand.commands": [
{
"command": "multiCommand.openNextABCFile",
"sequence": [
"workbench.view.explorer",
"list.focusDown",
"list.select"
]
}
]
It opens the next alphabetical filename in preview mode.
I work with big flat database files which I like to inspect at line/column.
I have a tool which generates a log with hundreds of lines like:
code --goto path/to/my/file:line:column
Visiting one such a spot works ok if I throw one such a line into the command line window and click it from there, but not from the editor (at least I have not found the way)
I would like to be able to have this log file open in the VSCode editor and hover/click in every line which would take me to the spot I want to visit in the big db file. Is this possible at all?
Should I convert my log file to some script format which supports the hover and click functionality?
Is there otherwise any extension available which can do the trick here?
Thanks a lot.
Miguel
Edit
Using HTML Related Links v0.9.0
I guess we're almost there but it's still not working for me.
The concrete scenario I have is: I have a file
d:\Users\Abc.def\Git\lingware-data\test\spx\spm\spm_cfg3\spm_cfg3_tst_row_valid_lng_abbr.dbr
containing several lines of the type
code --goto d:\Users\Abc.def\Git\lingware-data\dct\spx\dct.u08:176:36
(probably code is not needed, I tried with and without it)
In my settings.json file:
"files.associations": { ".u08": "csv (pipe)", ".dbr": "html" },
"editor.largeFileOptimizations": false,
"html-related-links.include": {
"html": [
{ "find": "--goto ([a-z:]+[^:]+):(\\d+):(\\d+)",
"filePath": "$1",
"lineNr": "$2",
"charPos": "$3"
}
]
}
if I use "plaintext" as languageId I don't get the HTML related links section displayed in the Explorer area, so I am using "html" - I have updated to v0.9.0 of the extension, and what I continue seeing in the explorer view is:
"d:\Users\Abc.def\Git\lingware-data\dct\spx\dct.u08 d:\Users\Abc.def\Git\lingware-data\test\spx\spm\spm_cfg3\d:\Users\Abc.def\Git\lingware-data\dct\spx\dct.u08"
If I click on it nothing happens I hope this is more or less clear.
What am I doing wrong?
*** edit ***
Installed v0.9.1. Enabled Developer console to debug click behavior on a row. What get now is:
[Extension Host] Clicked on: d:\Users\Miguel.Duran\Git\lingware-data\dct\spx\dct.u08
console.ts:137 [Extension Host] goto: 175:28
You can use the extension HTML Related Links v0.9.0 (it not only applies to HTML)
Define this in settings.json
"html-related-links.include": {
"plaintext": [
{ "find": "--goto ([^:]+):(\\d+):(\\d+)",
"filePath": "$1",
"isAbsolutePath": true,
"lineNr": "$2",
"charPos": "$3"
}
]
},
"html-related-links.removePathFromLabel": true
After loading the log file you can lock the Explorer View HTML RELATED LINKS to this file. Use lock button in top right of the view. Now only changes to this log file will update the view.
In an absolute path the label will contain the path and the view adds the directory path at the end. You can remove the path from the label with the setting html-related-links.removePathFromLabel
Click on a row in the view and that file will be opened to the given line:char position.
If on Windows the path is absolute you have to specify the drive first because it contains a :
"find": "--goto ([a-z]:[^:]+):(\\d+):(\\d+)"
Edit
There was a small typo in the regular expression string (need escaping the escape char).
Add possibility for absolute paths.
I would like to be able to have this log file open in the VSCode editor and hover/click in every line which would take me to the spot I want to visit in the big db file.
Is this possible at all?
If you visualize your file in a VSCode terminal, VSCode 1.70 (July 2022) will offer a way.
See issue 140780 ("Jump to line+column in terminal links (format <path> l:c)"), PR 153957 ("Detect terminal links with space, then line:col") and PR 155904 ("Ensure folders with spaces don't impact link line/col")
To test, you can open a terminal and run echo ./myFile.txt 10:5 (replacing the filename with one that exists).
When you click to open the file, it should open to line 10, column 5.
This is available today (July 2022) in Code insiders.
Is there a keyboard shortcut How to make VS Code treat/reopen/reload files without extension as a certain language?
Just to be clear, here's my use case:
Sometimes I have a big JSON I need to read so I copy it in VsCode new file but since it's a new file I have to save it a s JSON extension to read it in the correct format. So my question is: Is there a way to specify the language for this new file(without extension) to open it as JSON for example?
Thank you
I found these two options:
// The following example associates all files in a folder `somefolder` to PHP:
"files.associations": {
"**/somefolder/*.*": "php"
}
https://code.visualstudio.com/docs/languages/overview#_can-i-map-additional-file-extensions-to-a-language
// The default language mode that is assigned to new files.
"files.defaultLanguage": "html"
https://code.visualstudio.com/docs/languages/overview#_how-do-i-set-the-default-language-for-new-files
Add the lines of your choice to settings.json.
Nevermind guys I found it:
The feature is called Change Language Mode.
The default shortcut is Ctrl+K M if you want to customize it here's the name of the command:
workbench.action.editor.changeLanguageMode
You can also click in the lower right of VSCODE window to change the language mode:
VSCode remembers the file tabs that are open in my Workspace from the last environment, and so if I close VSCode and re-open, I have the same files opened.
Over a day or two of work, I may switch between 2 or 3 different feature branches while my PR's are going through review.
Each feature branch is usually on a totally different area of the code base, which means, I will want to open different groups of files when working on each branch.
I wonder if there is a way to save snapshots of open tabs so that I can re-reopen in future.
A really nice flow would be to have those files open automatically whilst other files close when VSCode detects a branch change.
Get a "snapshot" of currently opened files.
Save this snapshot somewhere; make it easy to change.
Use the snapshot to open all of its files; close all other files first.
Be able to make multiple snapshots and call each one easily.
(1) is harder than you might think. There are a number of vscode issues about searching only within the currently opened files and the problem remains largely unsolved after a few years for this reason.
Demo: get the relative paths of all opened files (unfortunately the gif creation software did a poor job of capturing all the keystrokes used in all these demos) :
Holy crap, what just happened. One keybinding and they are collected and formatted in a specific way.
A number of things happened. The only way I know of to efficiently get a list of opened files (maybe true even in an extension) is through the "Open Editors" view. So we
(a) focus that Open Editors view,
(b) select the entire list, and fortunately there is a
(c) copyRelativeFilePath command (or copyPath for the full path) that will get them all in a list.
But the list initially looks like:
1.html
simple\\gulpfile.js
test1.txt
which isn't going to do us much good. But it is now on the ClipBoard and there is an extension, Replace Rules that is able to run the Clipboard content through a series of regex's (without modifying the Clipboard content either) and paste the result somewhere. So you will need that extension and a macro extension, here using multi-command to run all the steps. Here is the macro which goes into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.getOpenedEditorsForTaskOpenAll",
"interval": 50,
"sequence": [
"workbench.files.action.focusOpenEditorsView",
"list.selectAll",
"copyRelativeFilePath",
// "copyFilePath",
"workbench.action.focusActiveEditorGroup",
{
"command": "replacerules.pasteAndReplace",
"args": {
"ruleName": "Prepare file list for task open"
}
},
"editor.action.formatSelection",
"cancelSelection",
"deleteLeft"
]
}
]
Here is the replacerules rule that is used in the macro:
"replacerules.rules": {
"Prepare file list for task open": {
"find": ["(\\\\)", "^(.+)$"],
"replace": ["\\\\", "\"'$1'\","]
}
}
It just modifies that bare file list into something we can use in a task. Here is a keybinding to run that macro (keybindings.json):
{
"key": "ctrl+shift+i",
"command": "extension.multiCommand.execute",
"args": {
"command": "multiCommand.getOpenedEditorsForTaskOpenAll"
},
}
You should be able to test this already to see if it'll dump the formatted file list to wherever your cursor is in the current text editor.
One way to open up all these files at once is to put them into a task (in tasks.json):
{
"label": "Open snapshot 1",
"command": "code",
"args": [
],
"type": "shell",
"problemMatcher": [],
"dependsOrder": "sequence",
"dependsOn": [ "close all editors" ]
},
{
"label": "close all editors",
"command": "${command:workbench.action.closeAllEditors}",
"type": "shell",
"problemMatcher": []
},
You see the task Open snapshot 1 is dependent on the close all editors task so that happens first. In the Open snapshot 1 the command is code to open all the arg files. Put your cursor in the args array and that is where the properly formatted list of files to open will be written by the macro. Demo:
If you want to update that file list you can just select them and rerun the macro. And you can now set up as many Open snapshot <n> tasks as you want (with whatever labels you want to give them).
Now, to trigger the task we will use a keybinding as well:
{
"key": "alt+s 1",
"command": "workbench.action.tasks.runTask",
"args": "Open snapshot 1"
},
{
"key": "alt+s 2",
"command": "workbench.action.tasks.runTask",
"args": "Open snapshot 2"
},
etc. As noted earlier, running this task will first run the dependent task which closes all currently opened files. If you just wanted to open a batch of files you frequently use without closing the others, just remove the "dependsOn": [ "close all editors" ] option.
Here is a final demo of the task closing the open files and opening the snapshot files (I just changed the file list above a little to make it look different).
Two things to remember:
(1) the Editor > Open Editors: Visible setting must be enable with a number high enough to show all your opened files. The Open Editors can be hidden so you don't have to look at it all the time if you don't want, but it will be opened automatically by the macro - that can't be avoided. You can see it opening in the demos. It can be hidden by its context menu.
(2) The terminal is used, so you see it opening.
It seems like a lot of set-up but the operation is actually pretty simple - just a couple of keybindings to remember.
Try an extension called File Group.
My searching reveled that a lot of people are looking for this option but vsCode does not seem to have any good way to do it. This extension lets you list out the files with full path and associate them to a key shortcut.
Three files below will load by hitting ctrl-alt-1 if you add this to youProject.code-workspace:
"1": {
"files": [
"C:\\temp\\file1.txt",
"C:\\svn\\foo.txt",
"C:\inetpub\wwwroot\iisstart.htm"
]
}
(It is a pain to set up if you are new to vsCode, hopefully it gets developed further.)
This worked for me. But it may have side affects when git does refresh
https://marketplace.visualstudio.com/items?itemName=gkotas.restore-git-branch-tabs
It seems that there is an extension that does the job: Editor Group Minimizer
There's this extension, that seems to kind of work:
Save and Restore Tabs
It seems to get most of the files and maintains the file split, but if I have multiple tabs on multiple groups, sometimes the secondary ones aren't opened.
There's an extension that would save your open tabs and swap them based on your git branch. Seems very useful, but in practice it was a little too intrusive.
Looks like its been abandoned, but there's this fork: Restore Git Branch Tabs Improved Tried it but isn't worked like I remember but maybe I just missed something for setup.
I want to create .HTML file whenever I choose New File, is this possible?
In VS Code, my separate folders for different HTML projects are stored under an UNTITLED (WORKSPACE).
For a new HTML file, I simply right-click on any folder in (WORKSPACE), select New File and then name that file with an HTML extension. The file is completely empty with no HTML whatsoever. Then, because the Emmet plugin is built into VS Code, I simply type an ! (exclamation point) and press TAB or ENTER and the empty file is filed with a complete HTML5 boilerplate.
5 simple steps to create an HTML file in Visual Studio Code
right-click on folder in (WORKSPACE)
Select New File
Name the file with an HTML extension
Type ! (one exclamation point only)
Press TAB or ENTER
As far as I can see, there is no way to configure VS Code to do this out of the box so I wrote an extension to do it. You can download that extension from the marketplace.
It adds two types of commands:
editor.newFile.withCurrentLanguageMode will open a new file with the same language mode as the currently active file
editor.newFile.withLanguageMode.<languageId> will open a new file with specified id as the language mode. The language must be first added to the configuration and a keybinding set up
So to set up one command to open new html files and another to open markdown files, you can add settings like this:
settings.json:
"editor.newFile.languageModes": ["markdown", "html"]
keybindings.json:
{
"key": "ctrl+shift+t 0",
"command": "editor.newFile.withCurrentLanguageMode",
"when": ""
},
{
"key": "ctrl+shift+t 1",
"command": "editor.newFile.withLanguageMode.markdown",
"when": ""
},
{
"key": "ctrl+shift+t 2",
"command": "editor.newFile.withLanguageMode.html",
"when": ""
}
Using these settings you would then press "ctrl+shift+t" then "0" to open a new file with the same language as whatever file you have active, "ctrl+shift+t" then "1" to open a new file with "markdown" as the language mode, and "ctrl+shift+t" then "2" to open a new file with "html" as the language mode. Of course, you can customize the keybindings however you want and set the languages to whatever you want.