4

Is there a shortcut to add a semicolon to the end of the current line of code?

For instance, M-; inserts a comment at the end of the current line of code. Is there anything like that for a semicolon?

Currently, I press C-e to move the cursor to the end and type the semicolon ;.

I was wondering if there is a quicker way.

6
  • 2
    Welcome to emacs.stackexchange.com! After my typing 100 WPM (without errors) at age 16 (IBM selectric), and having been a high volume litigation secretary for 10+ years, and doing all of the typing for 20+ years for my own small law firm, I can tell you that moving to the end of the line with one command and then typing a semicolon is pretty darn quick! You are not likely going to save more than 5 to 10 minutes (at best) over the course of an entire lifetime combining all of that into one function .... but, "yes", of course it is possible to write a function that does exactly what you want.
    – lawlist
    Commented Jul 14, 2020 at 5:44
  • 1
    Would you like Emacs to bring you right back to where you were when you type the magic command to insert a semicolon at the end of the line, or would you like to remain there at the end of the line immediately following the semicolon, or would you like to be able to do both (perhaps with a C-u to separate the options)?
    – lawlist
    Commented Jul 14, 2020 at 5:56
  • Please clarify what you find non-quick enough about C-e ;
    – Stefan
    Commented Jul 14, 2020 at 15:30
  • Maybe quick wasn't the right wording. Sometimes I simply forget and having a shortcut key to append a semicolon to the end of the line would seem helpful.
    – mrbean
    Commented Jul 14, 2020 at 21:41
  • 1
    Looks like I might not be the only one who has thought about this. reddit.com/r/vim/comments/adgof9/… github.com/iquiw/smart-semicolon
    – mrbean
    Commented Jul 14, 2020 at 21:43

1 Answer 1

6

If you ever want to add shortcuts functionality where you have control over the exact behavior, it's worth learning a little emacs-lisp.

This kind of functionality is trivial to write.

(global-set-key (kbd "<f12>")
  (lambda ()
    (interactive)
    ;; Keep cursor motion within this block (don't move the users cursor).
    (save-excursion
      ;; Typically mapped to the "End" key.
      (call-interactively 'move-end-of-line)
      (insert ";"))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.