12

I need to see, how many characters contains my text, as I type. I found this pages of Emacs Wiki: Word count, and there is a solution very close to what I need: I used the second solution from this page (Show the number of words in the mode-line), because it shows the amount of characters too.

But it doesn't count spaces. Is there any solution, that will show amount of characters including spaces in mode-line? Ideally, with setting target amount of characters (but this is not necessary).

4

The mode-line-format variable already has that capability built-in, the following snippet should add that near the end of your mode-line.

(add-to-list 'global-mode-string '(" %i"))

From the Doc:

%i -- print the size of the buffer.

Technically, this is the size in bytes, so it might not match perfectly the number of chars depending on the encoding you use.

You can also try, M-x size-indication-mode.


If the number of bytes does not satisfy you, you can use the following snippet, which may or may not cause lag as you type on very large buffers.

(add-to-list 'global-mode-string
             '(:eval (format " %s"
                       (let ((tab-width 1))
                         (string-width (buffer-string))))))
9

Alternatively, count words and characters including spaces in a selection:

M-x count-words-region
4

Or you could simply M->C-x =. The relevant info will appear in the minibuffer. (Commands used: end-of-buffer and what-cursor-position).

3

Other answers have given you useful info, I think. You can put whatever you want in the mode-line, and one place to start is size-indication-mode and variable variable mode-line-position.

The other answers generally tell you the current buffer position relative to the start of the buffer (or the start of its current restriction, if it is narrowed).

Sometimes you might want the position relative to some other position. For that, library modeline-posn.el can help (see Mode Line Position).

When the region is not active, it shows you the usual information (well, a bit better). But when it is active it shows you (in the mode-line) the number of characters and lines in the selection (region). So to see the current position relative to some other position, just use C-SPC to set the mark at one of the positions. C-x C-x activates the region, which shows you its size in characters (and lines) in the mode-line.

2

Unless I'm misunderstanding, you could just use point-max to get the number of characters in the buffer:

(defun count-chars ()
  (interactive)
  (save-restriction
    (widen)
    (message "%s characters" (1- (point-max)))))
  • 1
    It works, but this is not exactly what I want: I want to have number of character always before my eyes, while I'm typing, not getting it "on demand". – Aglavra Mar 13 '15 at 13:05
  • @Aglavra add it to your mode-line. – Jordon Biondo Mar 13 '15 at 13:24
  • Ah. You may want to try size-indication-mode, or modifying the mode-line-position variable instead, then. – Dan Mar 13 '15 at 13:25

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.