I am trying to use the Sublime Text Plugin RegReplace package so I can highlight a line of text and replace spaces with dashes.
Goal
Hello My Name is Jesse, Highlight, CTRL + . replaces with:
Hello-My-Name-is-Jesse -- for the purpose for saving time creating markdown links.
Currently Working, But Runs Entire File
I would really like this to only replace the text I have highlighted wit a hotkey, is this possible?
I have this in reg_replace_rules.sublime-settings
{
"replacements": {
"replace_spaces_with_dash": {
"find" : "(\\s)",
"replace": "-",
"greedy": true,
"case": false
}
}
}
Here is my User Hotkeys
{
"keys": ["ctrl+."],
"command": "reg_replace",
"args": {"replacements": ["replace_spaces_with_dash"]}
},
However, this replaces the entire file with spaces. I am missing something I hope, I am not sure the scope. Any help would be appreciated.
Is there a way to paste a multi-cursor (Ctrl+d, Ctrl+d, ... Ctrl+C) select, stripped of its newlines?
If [...] represents the highlight, and ⦙ the cursor:
The ⦙[red].
The ⦙[blue].
The ⦙[green].
And if I pasted I'd get:
red
blue
green⦙
but instead I want
redbluegreen⦙
Is this possible?
Save the following script #:
/Packages/Paste Without NewLines/paste_without_newlines.py
import sublime, sublime_plugin
class paste_without_newlines( sublime_plugin.TextCommand ):
def run( self, edit ):
clipboard = sublime.get_clipboard()
clipboard = clipboard.replace( "\n", "" )
sublime.set_clipboard( clipboard )
self.view.run_command( "paste" )
To execute via Command Palette > Paste Without NewLines, add the following code #:
/Packages/Paste Without NewLines/Default.sublime-commands
[
{
"caption": "Paste Without NewLines",
"command": "paste_without_newlines",
},
]
To execute via Ctrl + Shift + Alt + V, add the following code #:
/Packages/Paste Without NewLines/Default.sublime-keymap
[
{
"keys": ["ctrl+shift+alt+v"],
"command": "paste_without_newlines",
},
]
For anyone else stumbling across this, there's a sublime text package that solves this exact problem, it's called Paste PDF Text Block.
You can then Ctrl+Alt+v the text you want to copy from a pdf file into a new file in Sublime Text.
Worked a treat, and is a good solution if you don't know enough about making your own packages in sublime text, like me :(
I work with files containing names to other files: stacktraces, documentation, etc.
I often need to jump to a specific file/position and to do that I select part of line containing filename, copy it, open 'go to' window and paste it and press enter.
It's working fine, but it pollutes my clipboard.
Are there any solutions to open 'go to' window with text selected already inserted there?
I've checked default keymap and found nothing like that.
Nothing built in, but you can do it with a plugin.
import sublime_plugin
class GoToFileCommand(sublime_plugin.WindowCommand):
def run(self):
window = self.window
view = window.active_view()
regions = view.sel()
if len(regions) == 1:
if not regions[0].empty():
window.run_command("show_overlay",
{"overlay": "goto", "show_files": True,
"text": view.substr(regions[0])})
return
window.run_command("show_overlay", {
"overlay": "goto", "show_files": True})
Then simply rebind ctrl/cmd+p to go_to_file.
is there a way to assign a keyboard shortcut to a specific color scheme in Sublime Text 2? In Emacs it's easy to define a function that toggles "night-mode" color scheme and assigns it to a keyboard shortcut, I was wondering if you can also do it in ST2.
Piotr
Try something like this, in your user key binding:
{
"keys": ["YOUR_SHORTCUT"],
"command": "set_setting",
"args":
{
"setting": "color_scheme",
"value": "Packages/Color Scheme - Default/Solarized (Light).tmTheme"
}
}
Of course, change Packages/Color Scheme - Default/Solarized (Light).tmTheme to whatever theme you prefer.
If you want a toggle between two color schemes, you can create a plugin (Tools/New Plugin...):
import sublime, sublime_plugin
class ToggleColorSchemeCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
scheme1 = args["color_scheme_1"]
scheme2 = args["color_scheme_2"]
current_scheme = self.view.settings().get("color_scheme")
new_scheme = scheme1 if current_scheme == scheme2 else scheme2
self.view.settings().set("color_scheme", new_scheme)
and save it in your Packages/User directory.
Then add a key binding like this:
{
"keys": ["YOUR_TOGGLE_SHORCUT"], "command": "toggle_color_scheme",
"args":
{
"color_scheme_1": "Packages/Color Scheme - Default/Solarized (Light).tmTheme" ,
"color_scheme_2": "Packages/Color Scheme - Default/Solarized (Dark).tmTheme"
}
}
If you don't want to bother with editing config files you can install SchemeCycle.
Then cycle color schemes with F8 and Shift+F8. With 2 themes (Dark / Light) it acts as toggling.
If you prefer Command Palette check Norris's answer or try ColorSchemeSelector with : Select Color Scheme command, it will not pollute your pallete as much as Schemr.
Visualization AKA screens:
To change global color scheme settings (not just the current view), alter Riccardo's answer like this:
import sublime, sublime_plugin
class ToggleColorSchemeCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
s = sublime.load_settings("Preferences.sublime-settings")
scheme1 = args["color_scheme_1"]
scheme2 = args["color_scheme_2"]
current_scheme = s.get("color_scheme", scheme1)
new_scheme = scheme1
if current_scheme == scheme1:
new_scheme = scheme2
if current_scheme == scheme2:
new_scheme = scheme1
s.set("color_scheme", new_scheme)
sublime.save_settings("Preferences.sublime-settings")
Make sure all of your views are set to one color scheme before using this!
This should work no matter what your color schemes are currently set to under "Preferences.sublime-settings - User" and "Preferences.sublime-settings - Default".
I gleaned this information from Schemrs code: https://github.com/benweier/Schemr/blob/master/schemr.py
and the font-changing code: "Packages\Default\font.py"
I just found this nice little plugin:
https://github.com/skt84/Schemr
Which doesn't exactly allow you to bind, but gives you a Command Palette access instead, which does just perfect for me. (Just in case anyone stumbled on this via google like myself.)
To support multiple color schemes one would alter Riccardos answer like so:
class ToggleColorSchemeCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
scheme1 = args["color_scheme_1"]
scheme2 = args["color_scheme_2"]
scheme3 = args["color_scheme_3"]
current_scheme = self.view.settings().get("color_scheme")
new_scheme = scheme1
if current_scheme == scheme1:
new_scheme = scheme2
if current_scheme == scheme2:
new_scheme = scheme3
self.view.settings().set("color_scheme", new_scheme)
There is a way to change a color scheme along with a theme with Camaleon plugin.
If someone interested I just created a plugin to toggle between N different theme and colorscheme configurations. Check it out here:
https://gist.github.com/andresbravog/9429793
In Visual Studio I can press Ctrl+K+D to indent everything so the code is structured nicely and readable. Is there a shortcut in Sublime 2 to do the same?
You can find it in Edit → Line → Reindent, but it does not have a shortcut by default.
You can add a shortcut by going to the menu Preferences → Keybindings → User, then add there:
{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }
(example of using the F12 key for that functionality)
The config files use JSON-syntax, so these curly braces have to be placed comma-separated in the square-brackets that are there by default. If you don't have any other key-bindings already, then your whole Keybindings → User file would look like this, of course:
[
{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false}}
]
The reindent command only works on the currently selected lines unless the "single_line" argument is set to false.
{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }
Now, pressing f12 will reindent the entire document.
No one seems to love mac re-indentation, So here How I do it:
[
{ "keys": ["command+shift+i"], "command": "reindent"}
]
In Preferences > Key Binding - User
One more extra tip:
add
{ "keys": ["command+0"], "command": "focus_side_bar" }
to have sidebar file tree view navigation using keyboard.
Note:
Add , at the end of each {}, if you have more than one {} set of objects
There is no default shortcut for reindenting a file. However you can create one by following eznme's answer above.
You can also use the Command Palette by pressing:
Control+Shift+P (or ⌘+Shift+P on a Mac)
Type the first few characters of Reindent e.g: rein
Press Enter to run the command(The first command at the top should now show Indentation: Reindent Lines)
For those interested it is easy to change but for a lover of Netbeans and the auto-format you can change the key binding from F12 to ctrl+shift+F to use your beloved key binding. Sad part is that you have to select all to format the entire file. Netbeans still has the upper hand on that. If anyone knows how to overcome that limitation I'm all ears. Otherwise happy reindenting (auto-formating).
To indent with the same keys like Visual Studio Ctrl+K+D (I am a Visual Studio user so I am used to this combination) I suggest:
[
{ "keys": ["ctrl+k", "ctrl+d"], "command": "reindent", "args": {"single_line": false} }
]
Write this on Preferences>Key Bindings - User
It is very simple. Just go to Edit=>Line=>Reindent
Netbeans like Shortcut Key
Go to Preferences > Key Bindings > User and add the code below:
[
{ "keys": ["ctrl+shift+f"], "command": "reindent", "args": {"single_line": false} }
]
Usage
Ctrl + Shift + F
Select all code that you intend to indent, then hit Ctrl + ] in Sublime text to indent.
For macOS users, use command + ] to indent, and command + [ to un-indent.
code formatter.
simple to use.
1.Install
2.press ctrl + alt + f (default)
Thats it.
Beside of the inbuilt 'reindent' function, you can also install other plugins, such as SublimeAStyleFormatter and CodeFormatter. These plugins are better for their specify language.
Just in case this stop working for anyone like me, in OS X, the command key is identified as superso it should be able to do something like this:
[
{
"keys": ["super+i"],
"command": "reindent",
"args": {
"single_line":
false}
}
]
in this case using command+i is going to indent your whole code (eclipse like :) )
I used to use Alt + Shift + F in NetBeans, I checked and there isn't any collision in the default keymap array of sublime, so I added it to my sublime and I'm using it without any problem.
For those who like the default key binding for IntelJ IDEA, select Preferences > Settings - User:
And paste in the following to have the command + shift + l shortcut for auto indent:
[
{ "keys": ["command+shift+l"], "command": "reindent"}
]
You can add a shortcut by going to the menu Preferences → Keybindings → User, then add there:
{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }
For Auto-Formatting in Sublime Text 2: Install Package: Tag from Command Palette, then go to Edit -> Tag -> Auto-Format Tags on Document
Select everything, or whatever you want to re-indent and do Alt+ E+L+R.
This is really quick and painless.
This is my configuration for sublime-keymap:
[
{
"keys": [",+=+="],
"command": "reindent",
"args": {
"single_line": false
}
}
]
For vim people, just use ,== to reindent the whole file.
{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }
You can get the reindent option by using the above code
Steps:
Open Sublime Text.
Open Preferences.
Open Key Bindings -User.
Put below code:
[{"keys": ["ctrl+shift+c"], "command": "reindent"},]
I use CtrlShiftC and you also use other key shortcut.