I'd like to change the default behaviour of pressing C-e so that, If I have an inline comment, pressing C-e should first go to the end of line and pressing it again should go to the beginning of the first non-space character in the inline comment like so :-

(insert code here) ;; |inline comment.

How can I achieve this (I know very little elisp to be able to code this :( )? This was inspired by smarter-move-beginning-of-line which toggles between the first non-space character in a line and the beginning of line.


I bumped into this package in MELPA which does exactly what I wanted :- mwin

| improve this answer | |

Something like this should do the trick:

(defun impaktor-move-end-of-line ()
  (when (comment-search-forward (line-end-position) t)
    (goto-char (match-beginning 0))
    (skip-syntax-backward " " (line-beginning-position))))

To this function, you'll probably need to give a shortcut, something like

(global-set-key (kbd "<f2> e") 'impaktor-move-end-of-line)

Courtesy of EmacsWiki

| improve this answer | |
  • hmm.. this just moves the cursor to the end of the code before the beginning of the comment. Can I have the behaviour like I described in my post where the cursor would move to the first non white space character in the inline comment? – Chakravarthy Raghunandan Oct 2 '16 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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