According to this question, I added this :
[
{
"button": "button1",
"count": 1,
"modifiers": ["ctrl"],
"press_command": "drag_select",
"command": "goto_definition"
}
]
To a file which a named Default (Windows).sublime-mousemap in %appdata%\Sublime Text 3\Packages\User .And it works just fine. The only issue here is that the mutiselection feature of sublime text not to work, so I thought I could edit this in such to maintain the multiselection (ctrl + alt + click) or (ctrl + shift + click) :
[
{
"button": "button1",
"count": 1,
"modifiers": ["ctrl + alt"],
"press_command": "drag_select",
"command": "goto_definition"
}
]
But it doesn't seem to work. Any help with this would be great. Thanks.
Try setting it to the following:
[
{
"button": "button1",
"count": 1,
"modifiers": ["ctrl", "alt"],
"press_command": "drag_select",
"command": "goto_definition"
}
]
The "modifiers" array needs to have the individual keys as individual values, separated by commas - ["ctrl", "alt"], instead of having them both together concatenated with a plus +. This is one of the differences between keymaps and mousemaps.
Related
I know how to modify and create code snippets and I know how to modify shortcut keys, but how does one bring those 2 together?
Note that the line below will open a list of snippets defined for the language you are currently using (and you don't want that)
"args": { "snippet": "'$TM_SELECTED_TEXT'" }
Whereas with the below line the snippet given as argument will be executed right away
"args": { "name": "your_snippets_name" }
Here's how I defined a snippet for HTML where I wanted to select a text and when pressing CTRL+B the text to become enclosed in <strong></strong> tags:
"make_strong": {
"prefix": "strong",
"body": [
"<strong>$TM_SELECTED_TEXT${1:}</strong>"
],
"description": "Encloses selected text in <strong></strong> tags"
}
Note the ${1:} above - what this does is that it places the cursor there. This enables you to press CTRL+B at cursor and then have the cursor placed inside the <strong></strong> tags. When selecting a string and pressing CTRL+B, the string will enclosed in <strong> tags and the cursor will be placed before the closing </strong> tag. Pressing TAB at this point, will put your cursor after the closing </strong> tag.
And added in my keybindings.json the following:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": { "name": "make_strong" }
}
UPDATE JUNE 2nd, 2021
Since this is getting lots of views, I am posting some of the snippets I use, maybe it will be useful to someone
{
"key": "ctrl+alt+u",
"command": "editor.action.transformToUppercase"
},
{
"key": "ctrl+alt+l",
"command": "editor.action.transformToLowercase"
},
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_strong" }
},
{
"key": "ctrl+i",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_italic" }
},
{
"key": "ctrl+u",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_underline" }
},
{
"key": "ctrl+alt+p",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_paragraph" }
},
{
"key": "ctrl+shift+space",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_nbsp" }
},
{
"key": "ctrl+enter",
"command": "editor.action.insertSnippet",
"args": { "name": "insert_br" }
},
It would seem that, as of version 1.9, Visual Studio Code can do what you are looking for, no other extensions necessary.
From https://code.visualstudio.com/updates/v1_9#_insert-snippets
"You can now bind your favorite snippets to key bindings. A sample that encloses a selection with single quotes looks like this:"
Add the snippet below to keybindings.json (open Keyboard Shortcuts editor and click on the For advanced customizations open and edit keybindings.json link)
{
"key": "cmd+k",
"command": "editor.action.insertSnippet",
"args": { "snippet": "'$TM_SELECTED_TEXT'" }
}
Here are 3 steps to create a code snippet along with a shortcut.
1. Code -> Preferences -> Keyboard Shortcuts
2. Click on icon for keybindings.json file
3. Add JavaScript objects for Code Snippet/Shortcuts
For example i have created snippets for logging purposes as I mostly work with JavaScript Frameworks.
1.console.log('') with shortcut Control (or Ctrl) ⌃ + l
2.console.warn('') with Control (or Ctrl) ⌃ + w
3.console.error('') with Control (or Ctrl) ⌃ + e
Code:
{
"key": "ctrl+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.log('${TM_SELECTED_TEXT}$1')$2"
}
},
{
"key": "ctrl+w",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.warn('${TM_SELECTED_TEXT}$1')$2"
}
},
{
"key": "ctrl+e",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.error('${TM_SELECTED_TEXT}$1')$2"
}
}
Call Command Palette in View menu
Hit "shortcuts json" and OK
Then append under code blocks
{
"key": "shift+alt+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == 'js'",
"args": {
"snippet": "console.log($1);$0",
}
},
{
"key": "shift+alt+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == 'dart'",
"args": {
"snippet": "print($1);$0",
}
},
If you press Shift+Alt+L in JavaScript then
put "console.log();" to your editor,
And press Shift+Alt+L in Dart then
put "print();" to your editor,
with the same shortcut.
The default behavior of this shortcut ctrl+click is to create multiple cursors, but in my case, it was behaving like goto_definition command, and then I opened mousemap file I saw that the command is set to goto_definition:
{
"button": "button1",
"count": 1,
"modifiers": ["ctrl"],
"press_command": "drag_select",
"command": "goto_definition"
}
And I don't know how? Can anyone tell me how to change it to as it usually be?
Two things worked for me, first, if completely remove the setting, or second, if I replace the setting by this setting:
{
"button": "button1", "count": 2,
"modifiers": ["ctrl"],
"press_command": "drag_select_callback",
"press_args": {"by": "words"}
}
I want to find the command for "Replace" (from the "Find in Files" panel) so that I can assign a keyboard shortcut to it.
Normally I can use sublime.log_commands(True) in the Console Ctrl ` to identify the command, but the Console and Find in Files panels appear on top of each other; so I can't read the Console while I'm using Find in Files.
Control + Alt + Enter
From the default key bindings:
{ "keys": ["ctrl+alt+enter"], "command": "replace_all", "args": {"close_panel": true},
"context": [{"key": "panel", "operand": "replace"}, {"key": "panel_has_focus"}]
}
I know we can configure SublimeText menu via sublime-menu files.
Is it possible to add a root level menu of SublimeText instead of subtree of the root menu?
The reason I ask is SublimeText is all about customize and configuration; and since I access to Sublime Text > Preferences so frequently, that I want the Preferences at not the subtree but the root level, where menu bar shows.
I use SublimeText3.
In ST2/3, configurations are JSON. Create a Main.sublime-menu file in your user directory. This contains a list of object entries. As an example,
[
{
"caption": "Custom",
"mnemonic": "m",
"id": "custom"
}
]
This will create a "Custom" menu (at the top level). Of course, there aren't any sub menu entries so it's a little less useful. Below is a portion of the Menu file from one of my plugins (just to show how to specify a command). I listed under the "Custom" menu as an example.
[
{
"caption": "Custom",
"mnemonic": "m",
"id": "custom",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "AdvancedNewFile",
"children":
[
{
"command": "open_file",
"args": {"file": "${packages}/AdvancedNewFile/README.md"},
"caption": "README"
}
]
}
]
}
]
}
]
I did this in ST3 on W7. I'm unsure how it will behave in other OS's, but I assume it would work.
How to add a custom Menu item in SublimeText 2 .
Any Ideas ??
I see there is a Main.sublime-menu file but dont know how to edit it.
Thanks!
The *.sublime-menu file is simply JSON. You can create a Main.sublime-menu in your user directory and it will be merged with other menu entries. It may be beneficial to look through the Main.sublime-menu files third party plugins have. These are generally much shorter, so may be easier to understand some of the things you need to define in each entry.
edit
You can use the following as a plugin to open notepad with an arbitrary file.
import sublime
import sublime_plugin
import subprocess
import threading
class OpenNotepadCommand(sublime_plugin.TextCommand):
def run(self, edit, filename=None):
th = NotepadThread(filename)
th.start()
class NotepadThread(threading.Thread):
def __init__(self, filename=None):
self.filename = filename
threading.Thread.__init__(self)
def run(self):
if self.filename is not None:
subprocess.call("notepad.exe %s" % self.filename)
else:
subprocess.call("notepad.exe")
When you are creating a menu item use something like the following for the command and arguments.
{
"command": "open_notepad",
"args": { "filename": "<the absolute path here>"}
}
Easier option if what you want is just run a command. Create a file Context.sublime-menu inside your Packages/User directory, and add the following:
[
{ "caption": "<Your caption here>", "command": "exec", "args": {"cmd": ["<your cmd name>", "<arg1>", "<arg2>", <...>]} }
]
Exemple: Adding a menu item to the context menu that just run dir:
[
{ "caption": "List files in current dir", "command": "exec", "args": {"cmd": ["dir"]} }
]
I know this way too late to join the party and add my 2 cents. Anyway, Main.sublime-menu is a file that allows you to add menu items to the top menu i.e [File, Edit, Selection, Find, View, Goto, etc.]
I recently added a new section "Dev" just to figure it out. I also wanted a way to trigger browser previews for a specific browser. Check it out.
[
{
"caption": "Dev",
"mnemonic": "Z",
"id": "dev",
"children": [
{
"caption" : "Previews",
"children": [
{ "caption": "Markdown Live Preview", "command": "new_markdown_live_preview", "id": "markdown_live_preview" },
{ "caption": "Preview in Default Browser", "command": "view_in_browser", "id": "markdown_live_preview" },
{ "caption": "Preview in Firefox", "command": "view_in_browser", "args": { "browser": "firefox" }, "id": "markdown_live_preview" },
{ "caption": "Preview in Chrome","command": "view_in_browser", "args": { "browser": "chrome" }, "id": "markdown_live_preview" },
{ "caption": "Preview in Safari", "command": "view_in_browser", "args": { "browser": "safari" }, "id": "markdown_live_preview" },
]
},
]
}
]
Anyway, this still works in ST3. Just in case anyone stumbles around here.