8

I'm used to have display of the number of selected characters in the mode line when there is an active selection (in vim).

Is it possible to achieve this in emacs?

I managed to display the column index besides the line index, which is something... But not quite enough. I also looked at the mode line format documentation, but without success.

I also found smart mode line but I think that it doesn't offer the feature as of now.

2
  • 1
    "Is it possible to achieve [feature] in emacs?" Yes. Here's some elisp to start you off: (abs (- (point) (mark))) Also take a look at M-x describe-variable RET mode-line-format RET.
    – nanny
    Nov 18, 2014 at 21:57
  • thanks for the hint! Yes I can see I can modify the modeline with setq-default mode-line-format however the abs is just not appearing. I tried adding (:eval and then it shows *invalid* Nov 18, 2014 at 22:10

3 Answers 3

9

Use library Mode-Line Position (modeline-posn.el).

It shows you the size of the selection (active region) in the mode line, using the region face. It optionally tells you also the number of selected characters or characters and lines, etc.

1
  • That's it thanks! Does require me to turn on size-indication-mode (which might be a good thing, must get used to it). Will try this first, will probably stick to it. Nov 18, 2014 at 22:24
1

The following code could be a good start for you.

(setq mode-line-format '("%e" (:eval (number-to-string (abs (- (point) (mark)))))))

This sets the mode line to display the number of selected characters. It only updates after some events, e.g. C-l.

To finish this off you'd just want to add the specified form to an existing mode-line-format variable.

1
  • ahh so it's the number-to-string that I was missing when I got "invalid".. good to know! I'll try the ready-made script for now.. Thank you! Nov 18, 2014 at 22:26
1

I ended up making my own function, I will leave it here in case anyone is interested in the future:

(defun mode-line-region-chars (prefix middle subfix)
  (if (use-region-p)
      (let ((characters (abs (- (region-end) (region-beginning))))
            (lines (abs (- (line-number-at-pos (region-end))
                           (line-number-at-pos (region-beginning))))))
        (concat prefix
                (number-to-string characters)
                (if (> lines 0)
                    (concat middle (number-to-string lines)))
                subfix))))

If you have selected 51 characters across 4 lines, and the arguments are "(Characters: ", " Lines: ", ")", it will return "(Characters: 51 Lines: 4)".

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.