Undo a single select_lines - sublimetext2

Right now I have the following mapping to select lines forward:
{ "keys": ["ctrl+alt+down"] , "command": "select_lines", "args": {"forward": true} }
I'd like ctrl+alt+up to simply undo the last ctrl+alt+down. Soft undo doesn't quite work. If you select multiple lines quickly then the undo de-selects them all, instead of just the last one selected.
If there's no out-of-the-box way to do this then maybe I could map ctrl+alt+up to a set of keystrokes that deselects the current line and moves the cursor up one?

this behavior bugged me a lot too, so I've just went and wrote this: https://github.com/kizu/undo_select_lines#readme
This overrides the “soft undo” action in certain context, so it would actually “undo” the single select_lines.
The only downside is that there won’t be redo and the history would be a bit dirty, but whatever while it works.
So, if you didn’t override soft undo in your key bindings, installing this command through git in your Packages:
git clone git://github.com/kizu/undo_select_lines.git
Or download it there.
would fix this problem for you. Otherwise, look at how it’s implemented in the source, so you should replace the keybindings for your “soft undo”.

Related

Mercurial: How to jump back to previous hash? Equivalent of `cd -`

In mercurial, is there a quick way to jump back to the previous hash without typing that hash?
For example if I am at hash abc123 and then do hg update def456, is there a quick generic mercurial command to jump back to abc123 without having to remember I was at abc123?
I'm looking for something analogous to bash's cd - which takes you to the last directory you cd'd out of without having to type out that directory explicitly.
If there isn't one, is it easy to write a little extension where I can type something like hg update - and it will do this? It would be very useful as I jump around a lot.
Thanks!
Mercurial has - at least by default - no provisions to memorize the history of the checked-out revisions.
I also wouldn't count on a completely brain dead way to exist.
I'd first try to hack this feature by means of a hook to hg update where I check the parameter for the special case of '-' and replace it by a stored value. And if update is called without that special value, I'd try to store it in that very file I retrieve it from. But I cannot guarantee that such approach will work.

Smart Indenting of brackets (parenthesis) in sublime text 2

I've got a pretty sweet setup for editing and running maxscript from inside sublime text 2.
The one thing I've been wanting recently is to emulate or copy the behaviour of the curly brackets with the normal round brackets
EDIT:
Sorry - Chrome decided I was finished editing there when I wasn't :(
If I type if (x) then { then enter I will get a nicely formatted block, with the caret now at the arrow
if(x) then {
<-
}
but I cant find where sublime text is hiding it.
I want to copy this behaviour to the normal round brackets () instead of getting
if (x) then (
<-)
I've found where it does this on curly brackets, it's simply in the default key bindings. I copied the section "keys": ["enter"] and replaced the regex with "(" instead of "{".
I also had to copy the built in AddLineInBraces.sublime-macro and add {"command": "left_delete" }, to it:
[
{"command": "insert", "args": {"characters": "\n\n"} },
{"command": "left_delete" },
{"command": "move", "args": {"by": "lines", "forward": false} },
{"command": "move_to", "args": {"to": "hardeol", "extend": false} },
{"command": "reindent", "args": {"single_line": true} }
]
And make the enter keystroke call that macro if the regex matches. This gives the perfect result!
I've actually been doing less MXS the last while so haven't really been playing with it much.
Thanks to Ghoul Fool for the suggestions but after having a look I wanted a simpler solution.
In answer to FrozenKiwi, I have mashed together a few plugins from various places so I can send maxscript to Max from ST2. Here is a link for the heavy lifting. It does involve some work to get it running but is very handy indeed.
The rest is all just dribs and drabs of various ST2 goodness: shift+enter to evaluate selection, ctrl+e to evaluate all, lots (and lots) of macros e.g. "for" + tab will write format "var: %\n" (var as string)
I don't know if I can release it as a package as it's taken from quite a few different sources. I could check the licensing for each thing though I suppose.
My only remaining hurdle is not being able to redirect output from the maxscript listener to ST2, I've trawled the internet and nobody seems to have done it yet, and I'm not keen on trying it myself. I don't even know if Maxscript itself can be used - it might have to be a C++ plugin using the SDK - but my experience thus far with C++ plugins is minimal. I don't doubt it can be done though.

Save panes layout generated by Origami in Sublime Text 2

Thanks to this great plugin : Origami
I am able to obtain the following layout :
How could I save this view to call it from the view/layout menu ?
You can get the layout data from the Auto Save Session.sublime-session file, under the "layout": key. This file is in standard JSON format, and can be opened in ST2 just fine (select View->Syntax->JavaScript->JSON for syntax highlighting if you want). For OSX, this file is (probably, I'm not in front of my Mac at the moment to verify) located in ~/Library/Application Support/Sublime Text 2/Settings - it should be in the same directory as the Packages/ folder where plugins etc. are stored.
So, to make a keyboard shortcut, set up your Origami layout, then perhaps move some files around, search for some text, anything to update the Auto Save Session.sublime-session file. It may already have updated after changing your layout, so check the time stamp to make sure. Then, open the file and search for layout. Copy the contents of the key - the "cells":, "cols": and "rows": keys inside the curly braces, as well as the curly braces themselves. Then, open Sublime Text 2->Preferences->Key Bindings-User and add the following to it (include the square brackets if you don't have anything in this file yet, omit them if you already do. If you already do, make sure to add a comma , after the last curly brace of the item before):
[
{
"keys": ["alt+shift+o"],
"command": "set_layout",
"args":
|
}
]
Set your cursor where I put the | character after "args": (make sure you delete the |) and paste in the contents of the "layout": key from Auto Save Session.sublime-session you copied earlier. Save the file, and you should now have a keyboard shortcut AltShiftO (O for Origami) that will restore your layout for you. If you have more than one layout you'd like to save, repeat the steps above, and just change the "keys": value to another key combination. If you have a lot of plugins, I highly recommend #skuroda's FindKeyConflicts plugin, which is available through Package Control under the same name. With it you can get a full list of all current key mappings, so if you're planning on assigning a new one you can check to see if it's already taken. The plugin does more, as well, so if you're a plugin dev, or just a customization/macro geek like me, it's really quite useful.
As a caveat, with the complexity of the layout you displayed above, the "layout": key is going to be quite large and complex, and is made larger by the fact that each value in the "cells":, "cols": and "rows": keys is on its own line. I don't know enough regex to clean everything up automatically, but I'm sure it can be done.

Hotkey to end the line with a semicolon and jump to a new line in Sublime Text 2

I'm trying to figure out a hotkey at work. I just got this job and I am using a Mac for more or less the first time in my life.
Back home on my Laptop, when using Eclipse, I seem to remember there being a single hotkey which would both:
Add a ; to the end of my current line (no matter where the caret was within said line)
Place my cursor at the beginning of a new line, with the same indentation level as the line I had just added a semicolon to
Does anybody know if this was an Eclipse-specific hotkey, or know of a way to replicate said hotkey in Sublime Text 2?
Best solution for this is recording a macro on Sublime Text and then assigning it to a keyboard shortcut. Follow these steps:
Create a line such as alert('hello') and leave the cursor right
after letter 'o'.
Then go to Tools > Record a Macro to start recording.
Press Command+→ to go to the end of line.
Press ; and hit Enter
Stop recording the macro by going to Tools > Stop Recording Macro
You can now test your macro by Tools > Playback Macro (optional)
Save your macro by going to Tools > Save Macro (ex: EndOfLine.sublime-macro)
Create a shortcut by adding this between the square brackets in your
in your Preferences > Key Bindings - User file:
{
"keys": ["super+;"], "command": "run_macro_file", "args": {"file": "Packages/User/EndOfLine.sublime-macro"}
}
Now, every time you hit Command+;, it will
magically place the semicolon at the end of current line and move the cursor to the next line.
Happy coding!
After reading your question about three times, I finally realized that you were looking for one hotkey to perform both operations. Whoops.
Your request sounds like the Ctrl+Shift+; hotkey of the Smart Semicolon Eclipse plugin. While adding semicolons of a genius-level IQ would probably require an entirely new Sublime Text 2 plugin, you can easily create a smart semicolon–esque key binding with Sublime Text's Macros. I actually didn't know about them until now!
In this case, recording the macro yourself is actually the fastest way to create it, rather than copying and pasting a new file (and now you'll have the experience for making more). First, open a new file and type in your favorite garbage line:
Lord Vetinari's cat|
Then move the caret to anywhere within the line:
Lord Veti|nari's cat
Now, press Ctrl+Q, the hotkey for Tools -> Record Macro. If the status bar is enabled, it will notify you that it is "Starting to record [a] macro". Press End (if you don't have an End key, skip to below), then ;, then Enter. Finally, press Ctrl+Q again to stop recording. When you do, the status bar will display "Stopped recording macro". Check that your macro is working by hitting Ctrl+Shift+Q on a code segment of your choosing.
Just pressing Enter will adjust indentation on the next line accordingly as long as the "auto_indent" setting is set to true. See Preferences -> Settings – Default, line 59.
When you're satisfied, save your new macro with Tools -> Save Macro.... I saved mine as Packages/User/smart-semicolon.sublime-macro. My file looked something like this; feel free to copy it if you can't or won't make the macro manually:
[
{
"args":
{
"extend": false,
"to": "eol"
},
"command": "move_to"
},
{
"args":
{
"characters": ";"
},
"command": "insert"
},
{
"args":
{
"characters": "\n"
},
"command": "insert"
}
]
"extend": false, just means that the macro won't add any text to the working selection. Read more about the options for commands at the Unofficial Docs Commands Page.
Now that you have your macro, we can give it a custom key binding. Add the following lines to your Preferences -> Key Bindings – User file:
{ "keys": ["ctrl+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/smart-semicolon.sublime-macro"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.java" }
]
},
Replace Ctrl+Shift+; with whatever key binding you prefer, save your file, and give it a shot. The "context" array restricts the key binding to Java files (see the Unofficial Docs Key Bindings page for more information on contexts); if you want the key binding to be active everywhere, use this line instead:
{ "keys": ["ctrl+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/smart-semicolon.sublime-macro"} },
This NetTuts+ article has much more information on macros and binding them to keys; I referenced it often. This UserEcho post looks like it has more information on making the insertion more extensible.
You can also use a ready-made sublime package built for this purpose: AppendSemiColon
You can use package control to install it, just search for "AppendSemiColon".
Place a semicolon at the end of the cursor's current line(s) by pressing:
Command+; on OS X
Ctrl+; on Windows and Linux
and
You can also automatically go to the next line at the same time like this:
Command+Shift+; on OS X
Ctrl+Shift+; on Windows and Linux
I've been using this package for a while now, and it works great.
UPDATE:
As the author mentioned in a comment, you can now also change the default hotkeys if you like (personally, I love the defaults). As an example, just change:
[
{ "keys": ["ctrl+;"], "command": "append_semi_colon" },
{ "keys": ["ctrl+shift+;"], "command": "append_semi_colon", "args": {"enter_new_line": "true"} }
]
in your sublime-keymap to use whatever keys you want.
I tested the latest version, and this feature too works fine. There was a bug where extra semicolons were being appended incorrectly upon extra hotkey presses - this minor annoyance has been fixed too. Thanks, MauriceZ/mzee99!
Ctrl+Enter will create a new line with the same level of indentation.
I could not find anything like the Ctrl+A you mentioned
I'm on Sublime Text 3 ... inspired by your post, I've added the following to my .sublime-keymap:
{ "keys": ["ctrl+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/endOfLine.sublime-macro"} },
I'm amazed at how much time this saves.
#Felipe covered the second point. You can mimic the behavior of the first with a simple macro or plugin. I say or because you didn't describe the cursor position after you hit the key combination. Does it move the cursor to the next line? Does it insert a new line? I think you get the idea.
I'm pretty sure eclipse works on OS X as well as windows, so if you are more comfortable with that, why not use it (unless your job requires ST of course)? Most of the key bindings are probably the same/similar (well it's probably command rather than control for shortcuts).
http://www.eclipse.org/downloads/?osType=macosx
If you continue using ST, it's probably worthwhile to learn the basics of plugins. A lot can be built so you have the same behavior as other editors, it just takes some extra effort (through creating the plugin or finding a plugin that does it) up front.

Sublime Text 2 : Rectangular or column select by keyboard only on Mac 10.8.3?

I cannot figure out how to do rectangular selection in Sublime Text 2 using only keyboard. What I seem to always come across is to do ctrl-shift-up or -down (which I assume mean arrow keys), as laid out here: https://stackoverflow.com/a/13796939/1022967
But when I try to use that, I must send an OS-level command, because what happens is that my window slowly recedes and shows me all of the open apps/windows I have going on my Mac (I forget what that's called -- like a dashboard of what I have running). This happens when I use this key chord in apps outside of Sublime Text 2 as well.
Do I have something misconfigured? Could I have alternate key chords to do this?
You do not need to change the Sublime Text key bindings.
Simply go to System Preferences -> Keyboard -> Keyboard Shortcuts, click on Mission Control, and uncheck the boxes next to Mission Control and Application windows, then you will be able to use the default keys for column selection. This is a quick and easy fix, especially if you are not using Mission Control.
Go to the menu:
Sublime Text -> Preferences -> Key Bindings - Default
What are you looking for is:
{ "keys": ["ctrl+shift+up"], "command": "select_lines", "args": {"forward": false} },
{ "keys": ["ctrl+shift+down"], "command": "select_lines", "args": {"forward": true} },
The actual keys might be different, you need to find "select_lines" commands with these arguments.
If you want to change the keys, you need to open Key Bindings - User in the menu, then copy-paste and edit these lines according to your needs.
You will notice the change immediately after saving the file, if it has proper format (JSON), otherwise you'll get an error message.
FWIW, on my MacBook Pro running OS X 10.8.5, I set the keyboard bindings for column selection mode in my "Key Bindings - User" file in Sublime. Setting the key binding at the Mac System level in System Preferences > Keyboard > Keyboard Shortcuts > Application Shortcuts doesn't work.
After looking through the Sublime "Key Bindings - Default" preferences file and my Mac system preferences to make sure I wouldn't break an existing key binding, I went with Ctrl+Alt+Shift+Up and Ctrl+Alt+Shift+Down for column selection.
Here's what I have in my Sublime "Key Bindings - User" file:
{ "keys": ["ctrl+alt+shift+up"], "command": "select_lines", "args": {"forward": false} },
{ "keys": ["ctrl+alt+shift+down"], "command": "select_lines", "args": {"forward": true} }
Hope this helps.
For Mac, you need to use the command key, not control. So vertical select would be Command-Shift-Up/Down, horizontal is Command-Shift-Right/left
Both of the selection types go from where your cursor is currently so make sure your cursor is in the correct place you want it.
Simplest solution, don't need to touch the bindings file.
Hold down ALT key and make a selection using touchpad in 'clicked' state. It only selects the column.