Netbeans-like keyboard shortcuts in Sublime Text 3

I’ve been using Netbeans for over five years. Though it is so deadly slow, I didn’t manage to change to Sublime Text 3 (which is extremely fast) up until now. Five years of constant use of some program causes that you get used to it very hard and you’re very opponent to any changes.

This blog entry is supposed to help you configure Sublime Text 3 in the way as close to Netbeans as possible. But, Sublime Text 3 is not Netbeans. You’re able to achieve a lot of similar functionality (plus a lot more of things that you could only dream about in Netbeans), but there still be a span of things that are not replicable.

Sublime Text 3 is not Netbeans

Nearly entire article deals with changing default key bindings in Sublime Text 3. But, there are other functionalities in Netbeans, that you’ll miss in base Sublime Text 3 installation. This list includes:

  • Functionality of “jumping” to source definition of class or method, covered in this post
  • Gutter icons for Git and other VCSes, described in Sublime Text 3 and git gutter icons article
  • High-quality automated comments, mentioned here (DocBlockr plugin)
  • auto-selection of entire word and $ variable symbol, when double-clicking word

The last one can be fixed via user configuration in Sublime Text 3 and discussed here.

Keep in mind that keyboards shortcuts, you define works in every edit box across Sublime Text 3, not just in its main editor. If you for example define a shortcut, that enters some snippet (see “Quick HTML shortcuts” below), then pressing it, will insert that snippet everywhere, your cursor is:

  • To editor
  • To search box
  • To “Goto Anywhere” pane (Ctrl+P), to “Command Palette” (Ctrl+Shift+P) etc.

Be aware of that.

Netbeans-line comments

The default shortcuts in Sublime Text 3 for comments are:

  • Ctrl+/ for single line comment and
  • Ctrl+Shift+/ for block comments

To have the same comment toggle shortcut in Sublime Text 3 as in Netbeans (), open Preferences > Key Binding — User and insert following line:

{ "keys": ["ctrl+shift+c"], "command": "toggle_comment" }

Now, you can select line or entire block or have your cursor in single line, without any selection and pressing Ctrl+Shift+C will toggle comment for that part.

It will use Netbeans style of commenting in PHP (//), since we removed:

  • "args": { "block": false } and
  • "args": { "block": true } and
  • two key bindings for one operation, from default key binding file.

However, comments will be toggled with respecting line indent, while in Netbeans they’re always inserted as first characters of each line.

Note, that this is only for simple comment toggling. If you need a powerful comment or documentation blocks, just as you have in Netbeans, you need to install DocBlockr plugin (more details here).

Line operation

My Netbeans had Alt+Shift+Up shortcut for swapping current line with previous one and Alt+Shift+Down for doing the same with next line. And it had both Ctrl+Shift+Up and Ctrl+Shift+Down for duplicating current line below it. In addtion, it had Ctrl+E shortcut to remove entire line without copying it into clipboard (as an opponent to default Ctrl+X, pressed without selection, which removed current line and copied it to a clipboard).

If you want to achieve the same in Sublime Text 3, put these four new lines into Preferences > Key Binding -- User file:

{ "keys": ["ctrl+shift+up"], "command": "duplicate_line" },
{ "keys": ["ctrl+shift+down"], "command": "duplicate_line" },
{ "keys": ["alt+shift+up"], "command": "swap_line_up" },
{ "keys": ["alt+shift+down"], "command": "swap_line_down" },
{ "keys": ["ctrl+e"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Line.sublime-macro"} },

Note that I don’t recall, if these are default shortcuts or if I did them.

Quick HTML shortcuts

This part uses Sublime Text snippets inserted using keyboards shortcuts with insert_snippet command. I had four such shortcuts in Netbeans, for quickly inserting:

  • bold tag (<strong>),
  • emphasis (<em>),
  • new-line (<br />) and
  • Polish print quotation marks (&bdquo;&rdquo;).

Due to limitations of Netbeans (or maybe my lack of knowledge), I only managed to define myself a simple macros (tied to keyboard shortcuts) that simply inserted required tags and placed cursor inside them. I had to enter content manually.

There was no way (at least for me) to have functionality known from many editors that would allow me to select some part of text, press Ctrl+B and get that text surrounded with <strong></strong>.

Since nearly everything is better in Sublime Text 3 than it was in Netbeans, I managed to workaround this problem very easily, with this user key bindings:

{ "keys": ["ctrl+i"], "command": "insert_snippet", "args": {"contents": "${0:$SELECTION}"} },
{ "keys": ["ctrl+b"], "command": "insert_snippet", "args": {"contents": "${0:$SELECTION}"} },
{ "keys": ["ctrl+shift+'"], "command": "insert_snippet", "args": {"contents": "„${0:$SELECTION}”"} },
{ "keys": ["alt+enter"], "command": "insert_snippet", "args": {"contents": ""} },

Equally to above, I had Ctrl+Shift+D shortcut, that was inserting <div></div> tags. With the same limitations, as above.

With power behind Sublime Text 3, I not only managed to workaround these limitations, but also added myself a very cool functionality, with below key binding definition:

{ "keys": ["ctrl+shift+d"], "command": "insert_snippet", "args": {"contents": "${0:$SELECTION}"} },

It works this way:

  • I select text and press Ctrl+Shift+D.
  • Entire selection is surrounded with <div></div>.
  • Next, entire part class="" (first placeholder) is selected by ST3.

I can now:

  • press Del to remove it completely, if I don’t want div tag with class attribute,
  • start typing id=", if I would need that tag with id attribute instead,
  • press Tab to get inside "" (second placeholder) to type in class name.

And this entire cool feature made with just a single line inserted into user configuration. Feel the power of Sublime Text 3! :]

I also had Ctrl+Shift+P, that was inserting a < ?php ?> tag. Since this shortcut is reserved in Sublime Text 3 for an important “Command Palette” functionality, I decided to not touch it and use Ctrl+Alt+P shortcut:

{ "keys": ["ctrl+alt+p"], "command": "insert_snippet", "args": {"contents": "< ?php ${0:$SELECTION} ?>"} },

Because of above, I had to redefine default shortcut for quick project switch to Alt+Shift+P:

{ "keys": ["shift+alt+p"], "command": "prompt_select_workspace" },

You can read more:

The F-keys

If you:

then you probably modified user key bindings to be able to quickly call docs with simple F1 pressing.

I’ve been using Notepad++ before using Sublime Text 3. Along with its very-super-cool F4 shortcut to enable/disable word wrapping. To replicate the same behaviour in Sublime I had to add only one line to my user key bindings file:

{ "keys": ["f4"], "command": "toggle_setting", "args": {"setting": "word_wrap"}},

Here’s the source of above idea.

This isn’t the only “F-key” that I changed among my user key bindings. To make Sublime Text 3 a little bit closer to what I had in Netbeans, I also modified F12 shortcut to bring me “Save as…” dialog:

{ "keys": ["f12"], "command": "prompt_save_as" }

The original (Sublime Text 3’s) key binding for this, was Ctrl+Shfit+S. I needed it for inserting snippets.

Note that I did not need to replace original F12 keyboard shortcut (bind by default to goto_definition command and not used by me) in my user key bindings file. As for every configuration file, Sublime Text uses logic, in which user file overrides default file. So, my redefinition of F12 shortcut simply “disabled” default definition of this key binding.

Other keyboard shortcuts

Anyone can modify other keyboard shortcuts in the way, it is required, since Sublime Text 3 is customizable in nearly every aspect. For example, here I’m adding my own shortcut (Ctrl+Shift+S), which will bring up Tools > Snippets command (very often used by me):

{ "keys": ["ctrl+shift+s"], "command": "show_overlay", "args": {"overlay": "command_palette", "text": "Snippet: "} }

And the only question, that is left, is: how to get proper command for such new user binding?

Well:

  1. Navigate to Packages subfolder in your Sublime Text 3’s installation folder (c:\Program Files\Sublime Text 3\Packages in my case).
  2. Open / unpack Default.sublime-package archive (it is a .zip archived with changed extension).
  3. View Main.sublime-menu file out of it.
  4. Search for proper menu item name (Snippets... in this example).

As you can see, Default.sublime-package file holds the ultimate configuration behind Sublime Text 3.

With modification to any of files contained in this package, you can change actually anything. From menu, through default keyboard and mouse mappings, dialogs, to macros and other settings. You only need a basic understanding of JSON and XML and sometimes — a programming language in Python.

I have also redefined original keyboard shortcut for closing tag from Alt+. to Ctrl+Enter which seemed much more usable for me:

{ "keys": ["ctrl+enter"], "command": "close_tag" },

Country-specific letters

Sublime Text 3 doesn’t make any difference between pressing Right Alt and Ctrl+Left Alt (details)! This is a classic bug, found in many editors, that makes life of non-Latin alphabets users just a little bit nightmare. It causes many functional keyboard shortcuts to block entering contry-specific letters.

Workaround is to paste these two keyboard shortcuts to user key bindings file (Preferences > Key Bindings — User):

{ "keys": ["ctrl+alt+c"], "command": "insert_snippet", "args": {"contents": "ć"} },
{ "keys": ["ctrl+shift+alt+s"], "command": "insert_snippet", "args": {"contents": "Ś"} },

More in my Sublime Text 3 and git gutter icons article.

Summary

For the reference, here is my entire user key bindings configuration file:

[
    { "keys": ["ctrl+shift+c"], "command": "toggle_comment" },
    { "keys": ["ctrl+shift+up"], "command": "duplicate_line" },
    { "keys": ["ctrl+shift+down"], "command": "duplicate_line" },
    { "keys": ["alt+shift+up"], "command": "swap_line_up" },
    { "keys": ["alt+shift+down"], "command": "swap_line_down" },
    { "keys": ["ctrl+e"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Line.sublime-macro"} },

    { "keys": ["f1"], "command": "goto_documentation" },
    { "keys": ["f4"], "command": "toggle_setting", "args": {"setting": "word_wrap"}},
    { "keys": ["f12"], "command": "prompt_save_as" },

    { "keys": ["ctrl+shift+s"], "command": "show_overlay", "args": {"overlay": "command_palette", "text": "Snippet: "} },

    { "keys": ["ctrl+i"], "command": "insert_snippet", "args": {"contents": "<em>${0:$SELECTION}</em>"} },
    { "keys": ["ctrl+alt+p"], "command": "insert_snippet", "args": {"contents": "< ?php ${0:$SELECTION} ?>"} },
    { "keys": ["ctrl+b"], "command": "insert_snippet", "args": {"contents": "<strong>${0:$SELECTION}</strong>"} },
    { "keys": ["ctrl+shift+'"], "command": "insert_snippet", "args": {"contents": "&bdquo;${0:$SELECTION}&rdquo;"} },
    { "keys": ["ctrl+shift+d"], "command": "insert_snippet", "args": {"contents": "<div ${1: class="$2"}>${0:$SELECTION}</div>"} },

    { "keys": ["alt+enter"], "command": "insert_snippet", "args": {"contents": "<br />"} },

    { "keys": ["shift+alt+p"], "command": "prompt_select_workspace" },

    { "keys": ["ctrl+enter"], "command": "close_tag" },
]

Note, that above user key bindings does not include changes made to user key bindings file as a part of auto-pair resolution, described in Force Sublime Text 3 to auto-pair quotes before semicolon article.

If you wish to further customize your copy of Sublime Text 3, then unofficial docs are your true friend and help in that quest. Good luck!

Leave a Reply