say you have that:

    indented stuff   # commented stuff

We already got move-beginning-of-line and back-to-indentation for the left side. Now, for the right side...

I'd love to know if you are aware of a function that goes at the end of the code line (square position)

    indented stuff□   # commented stuff

should be able to understand the comment symbol depending on mode I guess.


2 Answers 2


The package mwim offers this functionality. By setting

(global-set-key (kbd "C-e") 'mwim-end)

you get behavior similar to what you described. The package also offers the corresponding mwim-beginning command which you can add using

(global-set-key (kbd "C-a") 'mwim-beginning)

See the package README for more.


Here's a quick command, lightly tested, that does what you're looking for.

(defun eol-dwim ()
  "Go to the end of the line, ignoring comments and trailing
  (let ((bol (line-beginning-position 1))
        (eol (line-end-position 1)))
    (if (condition-case nil
            (comment-search-forward eol)
          (error nil))
        (re-search-backward comment-start bol nil)
      (end-of-line 1))
    (skip-syntax-backward " " bol)))

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.