Am thinking whether it's achievable to make shortcut key-bindings on user
sublime key-map, so that one-key-triggered "find reg expression and replace-all"
if can assign custom keybinding will save a lot of work.
For example can be applied to make below answer into shortcut:
Sublime Text 2: How to delete blank/empty lines
Thanks to Joyas's answer, Below is the usage that could achieve the above task.
1.install RegReplace plugin for sublime 2/3
2.configure user reg replace function
{
"replacements": {
"remove_blank_lines": {
"find" : "(^\n)",
"replace": "",
"greedy_replace": true
}
}
}
3.configure key bindings
{
"keys": ["ctrl+shift+y"],
"command": "reg_replace",
"args": {"replacements": ["remove_blank_lines"]}
}
go to font-end press shortcut and it works so good!
There is already a post here about custom shortcut in sublime text.
In order to change the default shortcut for find and replace, you just have to edit de the line
{ "keys": ["super+shift+f"], "command": "show_panel", "args":
{"panel": "find_in_files"} }
in you preferences.
Furthermore, if you want to create a keybinding in order to "find and replace" a specific sequence of character, it seems that there is the plugin RegReplace to do so.
Related
I would like to wrap my selected html within a tag in VS code.
How do I do that?
Embedded Emmet could do the trick:
Select text (optional)
Open command palette (usually Ctrl+Shift+P)
Execute Emmet: Wrap with Abbreviation
Enter a tag div (or an abbreviation .wrapper>p)
Hit Enter
Command can be assigned to a keybinding.
This thing even supports passing arguments:
{
"key": "ctrl+shift+9",
"command": "editor.emmet.action.wrapWithAbbreviation",
"when": "editorHasSelection",
"args": {
"abbreviation": "span",
},
},
Use it like this:
span.myCssClass
span#myCssId
b
b.myCssClass
A quick search on the VSCode marketplace: https://marketplace.visualstudio.com/items/bradgashler.htmltagwrap.
Launch VS Code Quick Open (Ctrl+P)
paste ext install htmltagwrap and enter
select HTML
press Alt + W (Option + W for Mac).
As I can't comment, I'll expand on Alex's fantastic answer.
If you want the Sublime-like experience with wrapping, open up the Keyboard Shortcuts (command ⌘/Ctrl+shift+P > Preferences: Open Keyboard Shortcuts (JSON)) and add the following object:
{
"key": "alt+w",
"command": "editor.emmet.action.wrapWithAbbreviation",
"when": "editorHasSelection && editorTextFocus"
}
Which will bind the Emmet wrap command to option
⌥/Alt+W when text is selected.
You can also use the UI to do this, open the Keyboard Shortcuts menu and search for "emmet wrap with abbreviation" to add the shortcut.
Open Keyboard Shortcuts by typing ⌘ Command+k ⌘ Command+s or Code > Preferences > Keyboard Shortcuts
Type emmet wrap
Click the plus sign to the left of "Emmet: Wrap with Abbreviation"
Type ⌥ Option+w
Press Enter
imo there's a better answer for this using Snippets
Create a snippet with a definition like this:
"name_of_your_snippet": {
"scope": "javascript,html",
"prefix": "name_of_your_snippet",
"body": "<${0:b}>$TM_SELECTED_TEXT</${0:b}>"
}
Then bind it to a key in keybindings.json E.g. like this:
{
"key": "alt+w",
"command": "editor.action.insertSnippet",
"args": { "name": "name_of_your_snippet" }
}
I think this should give you exactly the same result as htmltagwrap but without having to install an extension.
It will insert tags around selected text, defaults to <b> tag & selects the tag so typing lets you change it.
If you want to use a different default tag just change the b in the body property of the snippet.
With VSCode 1.47+ you can simply use OPT-w for this.
Utilizing built-in functionality to trigger emmet, this is the easiest way:
Select your text/html.
Shift+Option+w
In the emmet window opened in the command palette, type in the tag or wrapping code you need.
Enter
Voila
Many commands are already attached to simple ctrl+[key], you can also do chorded keybinding like ctrl a+b.
(In case this is your first time reading about chorded keybindings: They work by not letting go of the ctrl key and pressing a second key after the first.)
I have my Emmet: Wrap with Abbreviation bound to ((ctrl) (w+a)).
In windows: File > Preferences > Keyboard Shortcuts ((ctrl) (k+s))> search for Wrap with Abbreviation > double-click > add your combonation.
I just installed htmltagwrap from extension marketplace and used ALT-W to wrap the tags (Windows Version).
There is a fast typing of the solution.
Open the command palette (usually Ctrl+Shift+P)
Preferences: Open Keyboard Shortcuts (JSON)
Add this snap code
{
"key": "ctrl+`",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "~~${TM_SELECTED_TEXT/^([\\t]*).*$/$1/}${TM_SELECTED_TEXT/^[\\t]*(.*)$/$1/}${TM_SELECTED_TEXT/^([\\t]*).*$/$1/}~~"
},
}
You select any text and press ctrl+`
result:
~~YourText~~
How do I set a shortcut for inserting console.log() and wrapping it around my current selection in Sublime Text without installing any plugin or extra third-party stuff?
So I found the answer. Thought I'd add it here for others:
Go to Sublime Text 2 > Preferences > Key Bindings - User and add this JSON to the file:
[
{ "keys": ["alt+d"],
"command": "insert_snippet",
"args": {
"contents": "console.log(${1:}$SELECTION);${0}"
}
}
]
Inserts a console.log() at the current cursor position on pressing alt+d.
Reference: https://gist.github.com/harthur/2951063
I am trying to use code folding command, but the short-cut is not working. I have to use menu, which is not convenient.
What does the "," mean in short cut? I tried the short cut on both side of the ",". They all not working. command + K is not working.
I think the reason is there is conflict here:
Then I try to configure a unique short cut key, which is also not woriking. Here is my configure
[
{"keys":["super+;"],"command": "run_macro_file", "args": {"file":"Packages/User/Semicolon.sublime-macro"}},
{ "keys": ["ctrl+enter"], "command": "open_in_browser"},
{ "keys": ["alt+d"], "command": "goto_definition" },
{ "keys": ["command+9"], "command": "fold_level_9" } // here is the definition
]
Can you please tell me what I did wrong?
How do I make the code folding short-cut work?
Thank you!
⌘K, ⌘3 means you hit ⌘K, release both buttons, then hit ⌘3. Sublime allows these compound keyboard commands, and recognizes it as different from ⌘3 alone. Try removing your custom shortcut keys (as they may be interfering with other commands) and seeing if the built-in commands work now.
In vim, in command mode, I'm quite fond of using the space bar to center the current line on screen. nmap <space> zz maps this functionality to the space bar in my gvimrc file. I'm trying to bind the same functionality to the space bar in Sublime Text 2:
{ "keys": ["space"], "command": "show_at_center", "args": {"command_mode": true} }
The line above doesn't work. Can anyone tell me why, and what will? I have Sublime's Vintage mode enabled. Also, I have “scroll_past_end”: true in my Sublime Text 2 User Preference Settings so that even the last lines of a file can be centered.
Try adding the following to Packages/User/Vintage/Default (<your OS>).sublime-keymap:
{ "keys": ["space"], "command": "show_at_center",
"context":
[
{ "key": "setting.command_mode"}
]
}
I can't test it right now, but it should work :)
EDIT - it doesn't. See below...
UPDATE
So I've searched far and wide, and it seems that, for whatever reason, you cannot assign an action to space on its own. Assigning it to Altspace works just fine:
{ "keys": ["alt+space"], "command" : "center_on_cursor",
"context": [{"key": "setting.command_mode"}] }
as does assigning it to a function key (F7, for example), or even to a single letter, like M. However, space on its own only results in the cursor moving one character forward when in command mode. The good news is that hitting z,z (lowercase) whilst in command mode will do what you originally wanted to do, which is center the cursor (as does CtrlL, as you indicated). Unfortunately, it is not possible to remap this action onto the spacebar on its own. The programmed action of space in command mode,
set_motion {"clip_to_line": true, "motion": "vi_move_by_characters", "motion_args": {"extend": true, "forward": true, "visual": false}}
just seems to be hard-programmed in. Sorry.
When using Sublime Text 2 we tend to open the side bar to navigate thru files/folders in our projects. For that we can use the hotkey ctrl+k ctrl+b (in windows).
However, once we're in the side bar, we can't use it with keyboard (arrows for instance). We have to stick using it with our own mouse...
Just a note: I installed SideBarEnhancements plugin, but I didn't find anything that could solve my problem.
Any solution you might know?
You can type Ctrl+0 (Ctrl+Zero) to focus on the side bar.
Then you'll be able to move selection among files with arrow keys and to open the selected file hitting Enter, without touching the mouse.
Another useful shortcut: ctrl + k Together with ctrl + b will show/hide the sidebar. Make sure you hit K and B in the right order
I didn't find any other complete answers, so I pulled together information from various answers and added a bit of my own.
Ctrl+K, Ctrl+B: toggle the sidebar
Ctrl+K+B: shorted form of the above (make sure you hit K and B in the right order)
Ctrl+0: switch focus to the sidebar (if open)
Up/Down: navigate file list
Right: expand a directory
Left: collapse a directory/navigate to parent directory
Enter: open a file
#Santiago Agüero The part you were missing was that the sidebar needs focus before the arrow keys will work (Ctrl+0).
As far as I can know, these shortcuts all work in Sublime 3, as well as Sublime 2.
One caveat: these assume you are using the default keybindings. But you can easily customize the keybindings by opening Preferences > Key Bindings - User and copying over lines from Preferences > Key Bindings - Default, changing the keys value as needed. E.g.,
{ "keys": ["ctrl+k", "ctrl+b"], "command": "toggle_side_bar" },
{ "keys": ["ctrl+0"], "command": "focus_side_bar" },
P.S. To get the fancy-looking keyboard glyphs, use the <kbd> HTML tag. E.g., <kbd>Key</kbd> will turn into Key. (Thanks to https://meta.stackexchange.com/questions/5527) :)
Summary
Ctrl + 0 will navigate to your sidebar. By default you can navigate the folders with your arrow keys. If you prefer 'Vim' type settings, you can avoid using the arrow keys by remapping your keys to the typical Vim settings (hjkl).
h will minimize/open a folder
j will navigate down (i.e. down arrow)
k will navigate up (i.e. up arrow)
l will open up a folder
Enter will open the file
Key mappings
To set this up, open Preferences > Key Bindings - User and add the following:
{ "keys": ["h"], "command": "move", "args": {"by": "characters", "forward": false}, "context":
[ {"key": "control", "operand": "sidebar_tree"} ] },
{ "keys": ["j"], "command": "move", "args": {"by": "lines", "forward": true}, "context":
[ {"key": "control", "operand": "sidebar_tree"} ] },
{ "keys": ["k"], "command": "move", "args": {"by": "lines", "forward": false}, "context":
[ {"key": "control", "operand": "sidebar_tree"} ] },
{ "keys": ["l"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[ {"key": "control", "operand": "sidebar_tree"} ] }
Ctrl+p is also really useful for opening files without using the mouse.
If you open a folder in Sublime all the files in that folder (and files in contained folders) will show up in the search you get with Ctrl+p.
Just hit Ctrl+p and start typing parts of the filename and you'll get a list of matches.
For sublime text 3 use Ctrl+K+B
Press and hold Ctrl then press and holdK and then press B
For Sublime Text 2 (and also Sublime Text 3) on Windows use Ctrl+0 to focus on the side bar, and use Ctrl+1 or Esc key to focus on the editor. And if it didn't work, use those 0 and 1 keys that exist in the numeric keys row under the function keys row rather than those 0 and 1 keys that exist in the numeric keypad of the keyboard. This image may demonstrate better: http://en.wikipedia.org/wiki/File:Qwerty.svg
In Sublime Text2, press keys in following format "Ctrl+k+b" and it will work on ubuntu.
It worked on my machine (v14.04 LTS)
I actually had the same issue, the fact that I had to trigger the reveal in side bar and then move focus to the sidebar, alongside with the fact that if the file's folder was already unfolded the focus would move to the sidebar's top, all this made me write a new plugin to reveal the file in sidebar and then move focus to there, making it a lot easier to navigate the sidebar with keyboard arrows, give it a try =)
https://github.com/miguelgraz/FocusFileOnSidebar
Another option is to use the FileBrowser package, which gives you many more actions and can be modified to your needs.
https://packagecontrol.io/packages/FileBrowser
Ctrl + 0 will focus on the side bar.
Ctrl + 1 will focus on the editor in the 1st window.
Ctrl + 2 will focus on the editor in the 2nd window etc.
Sandeep made a great post about improving the keyboard shortcut toggling side bar on/off.Go to Preference->Key Binding and enter:
[
{ "keys": ["ctrl+\\"], "command": "toggle_side_bar" },
]
save it and then press "ctrl+\" to toggle the sidebar.
In Linux new keybindings are saved in ~/.config/sublime-text-3/Packages/User/'Default (Linux).sublime-keymap'.
You have to add a folder to the Sublime Text window in order to navigate via the sidebar. Go to File -> Open Folder... and select the highest directory you want to be able to navigate.
enter this shortcode on Preference->Key Binding
[
{ "keys": ["ctrl+\\"], "command": "toggle_side_bar" },
]
now save it press
"ctrl+\\" for toggle the sidebar