21

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

Aglavra
  • 213
  • 1
  • 2
  • 4

5 Answers5

26

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

M-x count-words-region
Stefan
  • 26,154
  • 3
  • 46
  • 84
Kirill Yunussov
  • 453
  • 5
  • 8
  • The keyboard shortcut is `M-=`. In spacemacs, `count-region` uses the keyboard shortcut `SPC x c` which is an alias for `count-words-region` – Nate Dec 13 '21 at 20:31
6

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))))))
Malabarba
  • 22,878
  • 6
  • 78
  • 163
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).

wvxvw
  • 11,222
  • 2
  • 30
  • 55
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.

Drew
  • 75,699
  • 9
  • 109
  • 225
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)))))
Dan
  • 32,584
  • 6
  • 98
  • 168
  • 2
    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
  • 1
    Ah. You may want to try [`size-indication-mode`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Optional-Mode-Line.html#Optional-Mode-Line), or modifying the [`mode-line-position`](http://www.gnu.org/software/emacs/manual/html_node/elisp/Mode-Line-Variables.html) variable instead, then. – Dan Mar 13 '15 at 13:25