I'm trying to create a custom package and one thing I would like to do is bring up the Build Systems(located in the Tools menu item) in the command palette(show_overlay). So I have tried to create a Default.sublime-commands file in my package and type in...
[
{ "caption": "Build System", "command": "build_system" }
]
...to enable that menu item for the command palette(I also tried set_build_system) and then I created a Default.sublime-keymap file in my package so that I can access the Build System list from via shortcut...
[
{
"keys": ["f9"], "command": "show_overlay",
"args": {"overlay": "command_palette", "text": "Build System"}
}
]
I am not having any luck exposing the Build System menu item to the command palette. Can I get some help on this? I also noticed the Tools menu item is not available in the command palette as well. What am I missing?
Ok I figured it out. The command palette can only be populated by existing commands that run in sublime. The way you can view which commands that are being run in sublime is to open up the console(CTR + ~) and type in sublime.log_command(True)
Now whenever you do anything that makes sublime trigger a command, it will log that action in the console. Armed with this knowledge, we we go to Tools > Build System and click on the build system type we want, say, C++, we get:
command: set_build_system {"file": "Packages/C++/C++.sublime-build"}
Sweet! Knowing this we can go to our .sublime-commands file(you can call it Default.sublime-commands) and type in the below code:
[
{
"caption": "Set Build System: C++", "command": "set_build_system",
"args": { "file":"Packages/C++/C++.sublime-build" }
}
]
Tip: pay close attention to the "caption" it's what we will use to tie our .sublime-command file with our .sublime-keymap file. Let's add another one build system:
[
{
"caption": "Set Build System: C++", "command": "set_build_system",
"args": { "file":"Packages/C++/C++.sublime-build" }
},
{
"caption": "Set Build System: Python", "command": "set_build_system",
"args": { "file":"Packages/Python/Python.sublime-build" }
}
]
Now that we have exposed these two commands in our .sublime-commands file. We can create a shortcut for it in our .sublime-keymap file. I called mine Default.sublime-keymap:
[
{
"keys": ["f8"], "command": "show_overlay",
"args": {"overlay": "command_palette", "text": "Set Build System:"}
}
]
Notice the "text" key. Look familiar? This is how you connect your key binding to your command. Save press F8 and boom! You have our own custom command palette menu. Enjoy!
PS: you can put your .sublime-commands/.sublime-keymap files in your User package or add to any existing ones if you have them there if you just want to customize your sublime text 2 without making a custom package.
Related
Is it possible to save Terminal settings to a workspace? For most of my projects I always have two terminal tabs open. One where I do all of my git work and one where I run gulp tasks. These are two different folders and neither are the project root. When I open a saved workspace it always just opens one tab to the project root.
Look at the Restore Terminals extension. For example, in your settings.json:
"restoreTerminals.runOnStartup": false, // true is the default
// set to false if using a keybinding or command palette
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "git",
"commands": [
"cd <your directory>",
"npm run test" // your git command(s)
]
}
]
},
{
"splitTerminals": [
{
"name": "gulp",
"commands": [
"cd zip",
"gulp sass"
]
}
]
}
]
will open two terminals, one for your git work and one for the gulp work. They can each take multiple commands.
Example keybinding:
{
"key": "shift+alt+t", // whatever keybinding if you wish
"command": "restore-terminals.restoreTerminals",
},
or you can it run at start-up.
The Tasks feature is the current recommended way to handle this. There is no need for an extension. See Automating launching of terminals in the VS Code documentation.
Then make sure that tasks are run automatically when the folder is opened by running command Tasks: Manage Automatic Tasks in Folder and choosing Allow Automatic Tasks in Folder (see Run behavior).
Please also see the note here that:
Task support is only available when working on a workspace folder. It is not available when editing single files."
For a Linux not-so-officially-recommended way that works. The xdotool key emulator for linux can be used. I'm sure any Windows key emulator could be used to accomplish the same thing.
First, set the Terminal: Change Color to a keyboard shortcut. Here I'm using Ctrl+Shift+C. This is done by bringing up the command pallet (Cntrl+Shift+P) and, typing Terminal: Change Color
Here is my Restore Terminals json from VS Code settings. The lengthy cli command to update the colors needs to be activated on the last tab, as it is the tab that is focused when Restore Tabs is finished restoring tabs.
We need a bit of a timeout between the setting of each tab color, as VS Code takes just about a 10th of a second to complete the command. The built in keyboard shortcut cntrl+Page_Up is used to bring the previous tabs into focus. This script is for a WorkSpace that includes a Docker-Compose enviroment that contains 3 repos.
Be sure to save this in your Workspace settings, not your user settings. For more info, visit: https://code.visualstudio.com/docs/getstarted/settings
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "docker-compose",
"commands": ["cd ..", "cd docker-compose", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "api",
"commands": ["cd ..", "cd api", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "ui",
"commands": [
"cd ..",
"cd ui",
"xdotool key ctrl+shift+c && xdotool type 'cyan' && xdotool key Return && sleep .2",
"xdotool key ctrl+Page_Up && xdotool key ctrl+shift+c && xdotool type 'green' && xdotool key Return && sleep .2",
"xdotool key ctrl+Page_Up && xdotool key ctrl+shift+c && xdotool type 'yellow' && xdotool key Return",
"clear"
]
},
]
}
],
And the end result... Is that all tab colors are updated after they are restored.
Of course, anyone who has ever used keyboard emulation for automating tasks should know you can not be entering text or clicking elsewhere until the task is complete.
If you're on windows you can use powershell to create a similar effect to #CodeBloodedChris's solution.
Create a powershell script in your workspace root directory (or where ever the last terminal's final path is) and name it something like 'restore-terminal-customization.ps1' or something like that. Then add the following code to it:
# Uses windows forms to send keystrokes to customize vscode Terminals
Add-Type -AssemblyName System.Windows.Forms
$tabDelay = .6
# Last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^+cRed~")
# Second to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cYellow~")
# Third to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cMagenta~")
In this script the '^+c' is ctrl+shift+c and '~' is the enter key.
Similarly I had also set up a keybinding for the icons which uses ctrl+shift+i '^+i'. Here's an example of setting both the color and the icon of a terminal:
# Fourth to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cGreen~^+iorganization~")
In your restoreTerminal's config call the script you created as a command in the last terminal on your list.
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "docker-compose",
"commands": ["cd ..", "cd docker-compose", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "api",
"commands": ["cd ..", "cd api", "clear"]
},
]
},
{
"splitTerminals": [
{
"name": "ui",
"commands": [
"cd ..",
"cd ui",
".\restore-terminal-customization.ps1",
"clear"
]
},
]
}
],
Don't forget to set the keybindings to the Terminal: Change Color and Terminal: Change Icon commands in VsCode.
How can I open a specific file in a specified split view with an built-in command in VS codium? Is that possible without writing an extension?
I have a project, and I periodically I want to open a pair of files in specific way in split view. Let's mention them as problem1.py and problem1.txt. I want to programmatically open problem1.py in the left side, and the problem1.txt in the right side.
I found an a documentation for the command vscode.open:
vscode.open - Opens the provided resource in the editor. Can be a text or binary file, or an http(s) URL. If you need more control over the options for opening a text file, use vscode.window.showTextDocument instead.
uri - Uri of a text document
columnOrOptions - (optional) Either the column in which to open or editor options, see vscode.TextDocumentShowOptions
label - (optional)
(returns) - no result
In keybindings.json I created following statements:
{
"key": "numpad4",
"command": "vscode.open",
"args": "/home/user/myproject/README.md"
},
{
"key": "numpad6",
"command": "vscode.open",
"args": ["/home/user/myproject/README.md", "1"]
},
Now when I press numpad4, it works perfectly, the readme file opens. But when I press numpad6, I get a notification:
Unable to open '': An unknown error occurred. Please consult the log for more details.
Am I passing parameters in a wrong way? Why it does not detect a filename? And I do not see whare to view a log.
Additional info:
VS codium version: 1.66.2.
I saw a cli option -r, --reuse-window, but it has not control of in which view I want to open a file.
I saw a similar question, but there the author wants to do it from extension, while I would prefer to not write an extension for this problem. Also, as documentation says, I think I do not need vscode.window.showTextDocument, as vscode.open should be enough for my task.
Here is an enum list for available ViewColumn values: https://code.visualstudio.com/api/references/vscode-api#ViewColumn
you can use the extension HTML Related Links use command htmlRelatedLinks.openFile
{
"key": "numpad6",
"command": "htmlRelatedLinks.openFile",
"args": {
"file": "${workspaceFolder}/README.md",
"method": "vscode.open",
"viewColumn": 1
}
}
Combining the #rioV8's solution of using HTML Related Links and #Mark's solution for combining two commands into one, the resulting keybindings.json is the following:
{
"key": "numpad5",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{ "command": "htmlRelatedLinks.openFile",
"args": {
"file": "/home/user/myproject/problem1.py",
"method": "vscode.open",
"viewColumn": 1,
}
},
{ "command": "htmlRelatedLinks.openFile",
"args": {
"file": "/home/user/myproject/problem1.txt",
"method": "vscode.open",
"viewColumn": 2,
}
},
]
}
},
How do I delete the custom profile "New 1" as shown in the picture?
I'm using Zsh, so I suggest configuration
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh",
"icon": "star",
"env": {
"TESTING_VAR": "abc"
}
}
},
"terminal.integrated.defaultProfile.osx": "zsh",
As described in the Configuring profiles section of the VS Code docs), when you create a custom terminal profile, it adds an entry to your settings.json file:
The recommended way to create a new profile is to run the Terminal: Select Default Profile command and activate the configure button on the right side of the shell to base it on. This will add a new entry to your settings that can be tweaked manually in your settings.json file.
...
"terminal.integrated.profiles.windows": {
"PowerShell -NoProfile": {
"source": "PowerShell",
"args": ["-NoProfile"]
}
},
"terminal.integrated.profiles.linux": {
"zsh (login)": {
"path": "zsh",
"args": ["-l"]
}
}
So open your User settings and you'll find the terminal.integrated.profiles.<OS> setting with an added entry for your custom profile under your corresponding OS. It is better to open the settings.json file directly, or open it from the UI "Edit settings.json":
settings UI
settings.json
"terminal.integrated.profiles.osx": {
"1": {
"path": "bash"
}
}
(I'm using a Mac OS so I have it in .osx. It gets added correspondingly to .linux or .windows)
To remove your custom profile:
Simply delete your custom profile ("1": {...}) from terminal.integrated.profiles
Reload/Restart VS Code
When VS Code reloads/restarts, the removed custom profiles should be gone. (It seems these custom profiles are read and loaded one-time during VS Code startup).
Note though that this only works for custom profiles. For built-in profiles, you'll have to set the profile to null instead:
To remove profile entries from the dropdown, set the name of the profile to null. For example, to remove the Git Bash profile on Windows, use this setting:
"terminal.integrated.profiles.windows": {
"Git Bash": null
}
Is it possible to create a new file inside the same folder as the file that is active in the editor by the time the keyboard shortcut ctrl+n for explorer.newFile is hit?
I can tell if it's the correct folder by looking at the breadcrumbs and mostly have the file explorer toggled off. The command, however, seems to create a file inside the last folder opened or focused by the file explorer.
You can do this by overloading that explorer.newFile command's default keybinding to run a task instead. Here is the keybinding:
{
"key": "ctrl+n",
"command": "workbench.action.tasks.runTask",
"args": "newFile",
"when": "editorFocus" // must have an editor focused
},
And the task (in tasks.json):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "newFile",
"command": "touch '${fileDirname}/${input:fileName}' && code '${fileDirname}/${input:fileName}'",
"type": "shell",
"problemMatcher": [],
},
],
"inputs": [
{
"type": "promptString",
"id": "fileName",
"description": "Complete my file name.",
"default": "new file name"
}
]
}
That will create a file in the same directory as the active editor's directory - it will ask you for the filename - and then open it.
I used the bash command touch to create the file (if it doesn't already exist), if you are using a different shell you will need the equivalent of touch.
I'm tried each of the following keybindings.json ctrl+enter "command" settings to enable execution of two commands when the .py[thon] script editor extension is active. The single commands work but none of the attempts to associate multiple commands or a macros reference containing multiple commands, work. The later generates a "command 'macros.<macro defined in settings.json>' not found."
Any pointers on how I get multiple commands attached to a single keyboard shortcut definition, specifically what's wrong with how i'm setting up macros approach?
// %appdata%\Code\User\keybindings.json
[
{
"key": "ctrl+enter",
//"command": "python.execSelectionInTerminal",
//"command": "cursorDown",
//"command": [ "python.execSelectionInTerminal", "cursorDown" ],
//"command": "python.execSelectionInTerminal, cursorDown",
//"command": "python.execSelectionInTerminal && cursorDown",
"command": "macros.pythonExecuteLineAndMoveToNextOne",
"when": "editorTextFocus && editorLangId == 'python'"
}
]
// %appdata%\Code\User\settings.json
{
"macros": {
"pythonExecuteLineAndMoveToNextOne": [
"python.execSelectionInTerminal",
"cursorDown"
]
},
.
. .
. . .
}
Adding duplicate of answer raised in comments so it can be accepted to change state of this question to answered.
The fix was to add the #name:macros #publisher:geddski vscode extension which was mentioned in the article i used to arrive at the multi-step macro definitions. It wasn't initially clear that it was a requirement.