35

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?

madth3
  • 7,275
  • 12
  • 50
  • 74
Samuel Stiles
  • 2,118
  • 5
  • 22
  • 27

6 Answers6

76

Best solution for this is recording a macro on Sublime Text and then assigning it to a keyboard shortcut. Follow these steps:

  1. Create a line such as alert('hello') and leave the cursor right after letter 'o'.
  2. Then go to Tools > Record a Macro to start recording.
  3. Press Command+ to go to the end of line.
  4. Press ; and hit Enter
  5. Stop recording the macro by going to Tools > Stop Recording Macro
  6. You can now test your macro by Tools > Playback Macro (optional)
  7. Save your macro by going to Tools > Save Macro (ex: EndOfLine.sublime-macro)
  8. 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"}
    }
    
  9. 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!

DadNapper
  • 2,721
  • 1
  • 17
  • 8
  • what about in windows. instead of command. should it be ctrl? – Vincent Feb 12 '14 at 05:51
  • 6
    @Vincent Instead of Command+RightArrow, you can use the End key – Ace Apr 05 '14 at 16:30
  • 3
    For windows users. To bind the macro to "ctrl+;" use the following code: `{ "keys": ["ctrl+;"], "command": "run_macro_file", "args": {"file": "Packages/User/EndOfLine.sublime-macro"} }` – Hubvill May 09 '15 at 15:10
  • This won't work with Italian keyboards because semicolon is accessible with shift and comma [--->](https://commons.wikimedia.org/wiki/File:Italian_Keyboard_layout.svg), so the combination in this case should be `"ctrl+,"`, or something like that. – gRizzlyGR Aug 01 '17 at 09:48
  • i just log in to thumb up! – mzoz Feb 12 '18 at 09:25
  • 1
    Nice solution! Also a great little insight into Sublime Text Macros. Thank you!! – bananaforscale Feb 23 '20 at 17:15
19

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.

Community
  • 1
  • 1
angerson
  • 7,342
  • 1
  • 21
  • 24
  • I'm glad you understand my request, but so far what you've listed isn't working for me.... --- is there private messaging on stackoverflow? i cant put code in these stupid comments – Samuel Stiles May 22 '13 at 17:17
  • @SamuelStiles That's odd. SO doesn't have a messaging system, but feel free to send me an email (check my [user page](http://stackoverflow.com/users/2259675/protractor-ninja)) and I'll be glad to help. I'll update the answer after we fix things. – angerson May 22 '13 at 17:22
  • I'll do that when I get home from work tonight - talk to you soon! – Samuel Stiles May 22 '13 at 19:38
  • +1 for mentioning macros which is something I use way to little but that is perfect for this case. – Daniel Figueroa May 24 '13 at 22:19
  • @SamuelStiles I found a problem in the keybinding code; `equals` was supposed to be `equal`. Hopefully that will have fixed your problem. – angerson May 24 '13 at 22:46
  • @ProtractorNinja, excellent post. Great references. Just wanted to mention that if you show your status bar in sublime text, it will indicate when you start recording a macro. – Michael Jul 30 '13 at 09:38
13

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!

nonbeing
  • 6,907
  • 6
  • 36
  • 46
  • 1
    Hey I made this! I released an update a few days ago which should allow you to bind custom hotkeys. – mzee99 Jul 04 '15 at 02:11
  • Its better to change the default keybindings - on ST3, ctrl+; means opening the goto anything function (#), i.e., fuzzy search in file. Also, shift + ; ends up as a : on my English keyboard, effectively rendering the second command inaccessible. Apart from that, great little helper! – f0xdx Dec 25 '15 at 04:31
  • Perfect, many thanks to the author, I made a custom shortcut, that's awsome! [ { "keys": ["super+;"], "command": "append_semi_colon", "args": {"enter_new_line": "true"} } ] – Sunding Wei Jul 30 '19 at 07:17
  • This is good one. – Samsul Islam Sep 17 '22 at 06:43
1

Ctrl+Enter will create a new line with the same level of indentation.

I could not find anything like the Ctrl+A you mentioned

Volker E.
  • 5,911
  • 11
  • 47
  • 64
Felipe
  • 440
  • 3
  • 18
  • You can create your own bindings with this [syntax](http://www.sublimetext.com/docs/key-bindings) – Felipe May 21 '13 at 16:55
1

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.

JMichael
  • 75
  • 1
  • 11
0

@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.

skuroda
  • 19,514
  • 4
  • 50
  • 34