Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
Samuel Stiles Avatar asked May 21 '13 16:05

Samuel Stiles


People also ask

How do you jump to a line in Sublime Text?

In Sublime Text, you can quickly jump to any line in the code. Hit Ctrl–G (Mac and Windows). Type in a line number and hit Return/Enter to go to that line.

How do I move the cursor to the end of a line in Sublime Text?

However eol moves the caret to the first non-indentation character in the line, rather than to the beginning of the line. To move to the beginning/end of the line, I found ctrl+a and ctrl+e to work fine, respectively.

How do you move a line up and down in Sublime Text?

CTRL-SHIFT-UP (PC) or CTRL-⌘-UP (Mac) moves a line up, while CTRL-SHIFT-DOWN (PC) or CTRL-⌘-DOWN (Mac) moves a line down.

How do you go to the end of a line of text?

Generally use the Home key to move to the beginning of input field and End key to move to the end. You can also use Ctrl + arrow key to move direction one word at a time.


1 Answers

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!

like image 79
DadNapper Avatar answered Nov 08 '22 14:11

DadNapper