6

say you have that:

stuff
    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)

stuff
    indented stuffâ–ˇ   # commented stuff

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

Dan
  • 32,584
  • 6
  • 98
  • 168
v.oddou
  • 163
  • 5

2 Answers2

5

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
whitespace."
  (interactive)
  (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)))
Dan
  • 32,584
  • 6
  • 98
  • 168
4

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.

clemera
  • 3,401
  • 13
  • 40