Friday, October 28, 2011

Eclipse shortcuts for Sublime Text 2

I am a Linux guy. I used to pimp gedit with all plugins under the sun in an effort to transform it into TextMate, but no matter what I try it keeps being an awkward coding experience. Still on the search for alternatives, last month I discovered Sublime Text 2 and its entire new way of understanding text editing.

Sublime Text is just perfect for lightweight coding. I am not going to get into the details, since there are better places documenting this (see for example this list of tips and tricks). It is the perfect tool for times where a full-blown IDE is overkill: after all, it makes little sense to open Eclipse if you are going to spend the next eight hours debugging JavaScript or HTML (Agile and all that).
Now, this is the thing: I work with multiple major projects in the same week, performing different tasks and roles. I switch environments A LOT. Having to think "where am I editing?" before duplicating lines or saving files was messing into my flow.
So, without further ado, behold the

Ultimate eclipse shortcuts for Sublime Text 2

Not exactly rocket science, but still: go to Preferences -> Key bindings - User and paste this:

[
 { "keys": ["shift+enter"], "command": "run_macro_file", "args": {"file": "Packages/Default/Add Line.sublime-macro"} },
 { "keys": ["alt+up"], "command": "swap_line_up" },
 { "keys": ["alt+down"], "command": "swap_line_down" },
 { "keys": ["ctrl+alt+j"], "command": "join_lines" },
 { "keys": ["ctrl+alt+down"], "command": "duplicate_line" },
 { "keys": ["shift+ctrl+r"], "command": "show_overlay", "args": {"overlay": "goto", "show_files": true} },
 { "keys": ["ctrl+shift+s"], "command": "save_all" },
 { "keys": ["ctrl+l"], "command": "show_overlay", "args": {"overlay": "goto", "text": ":"} },
 { "keys": ["shift+ctrl+f4"], "command": "close_all" },
 { "keys": ["shift+ctrl+y"], "command": "lower_case" },
 { "keys": ["shift+ctrl+x"], "command": "upper_case" },
 { "keys": ["ctrl+d"], "command": "run_macro_file", "args": {"file": "Packages/Default/Delete Line.sublime-macro"} }
]

Save, and you will have a selection of finest eclipse shortcuts configured right into your editor. It is as far as I could get without getting into macros. You can help yourself and find the complete list of commands under Preferences -> Key bindings - Global, and complement this with our own discoveries.
If you like this, do not miss our new post about Sublime Text Editor tips for Web Developers.

12 comments:

  1. I've just configured it, although I had to change them slightly to adapt it to the same way my eclipse responds on my Mac. Basically, I've replaced some 'ctrl's with 'super's (Command button on Mac keyboards)

    For Mac Users, you can use this version:

    [
    { "keys": ["shift+enter"], "command": "run_macro_file", "args": {"file": "Packages/Default/Add Line.sublime-macro"} },
    { "keys": ["alt+up"], "command": "swap_line_up" },
    { "keys": ["alt+down"], "command": "swap_line_down" },
    { "keys": ["ctrl+shift+j"], "command": "join_lines" },
    { "keys": ["super+alt+down"], "command": "duplicate_line" },
    { "keys": ["shift+super+r"], "command": "show_overlay", "args": {"overlay": "goto", "show_files": true} },
    { "keys": ["super+shift+s"], "command": "save_all" },
    { "keys": ["super+l"], "command": "show_overlay", "args": {"overlay": "goto", "text": ":"} }
    ]

    ReplyDelete
  2. Is it easy way to duplicate_line in up direction?

    ReplyDelete
  3. You can try recording a macro as "Edit > line > duplicate line" and then "Edit > line > swap line up".

    ReplyDelete
  4. Great dude. That was what I was looking for. Thanks!

    ReplyDelete
  5. Really great! Thanks!!!

    ReplyDelete
  6. To add eclipse style Cmd-D -> Delete line, add this:

    { "keys": ["super+d"], "command": "run_macro_file", "args": {"file": "Packages/Default/Delete Line.sublime-macro"} }

    ReplyDelete
  7. thanks mate, it helped me port my ass from eclipse to sublime! :D

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. thanks for these; the behaviour for the the Duplicate Lines ctrl-alt-down command can be more accurately matched to the Eclipse version (that dupes whole lines, not just the selected text) by defining a macro that selects the lines in question first, then executes the duplicate;

    The macro (based on the delete line example above):

    [
    {"command": "expand_selection", "args": {"to": "line"}},
    {"command": "duplicate_line"}
    ]

    Save this to your User Packages folder (Preferences > Browse Packages > User folder) as Duplicate Lines.sublime-macro, then edit the command keys to give you:

    { "keys": ["ctrl+alt+down", "shift-ctrl-d"], "command": "run_macro_file", "args": {"file": "Packages/User/Duplicate Lines.sublime-macro"} },

    Adding another version that does the ctrl-alt-up version of the shortcut is a bit fiddle because, by default, it only swaps the selected lines one line, up, not n times where n is the number of lines duplicated

    ReplyDelete

Something on your mind?

Note: Only a member of this blog may post a comment.