Anyway to calculate it? (either in lines or pixels)


4|                   | <- top line
5|                   |
6|                   |
7| ■ <- cursor here. |

Here it is 3 (or 4, if you prefer to include both lines).

2 Answers 2


I don't know if there is a straightforward way to know that, but you can have:

  • (windows-text-height) , which is the amount of lines in current window
  • (line-number-at-pos) will return the line number in this buffer.
  • (move-to-window-line) will be able to move the point to the first visible line using argument 0.

Knowning this writing a piece of elisp code wich will return the desired distance in lines and letting the point where it was is pretty trivial.

window-line-height will return the height in pixels of a given line. If you're not using variable fonts it can be used to have the pixel length too, but I'm not sure at all of its accuracy.

  • +1 - Thanks. It's just a little roundabout. I think Emacs might have stored such similar values that can be used directly, rather than by moving cursor this way to calculate the distance.
    – shynur
    Jul 29, 2023 at 16:27
  • if it does it should be internally, as usually the used value is the cursor position in the buffer, AFAIK the window handling part is written in C and I don't know what it exposes to elisp
    – Muihlinn
    Jul 29, 2023 at 19:58
  • "if it does it should be internally" -- You can take a look at my answer. I see no need to hide this value from the user, which only introduces troubles.
    – shynur
    Aug 28, 2023 at 10:55
(count-lines (window-start) (point))

Better method:

(keymap-global-set "C-c p"
  (lambda ()
    "Report the distance in lines & pixels."
    (let ((posn (posn-at-point)))
      (message "%s; %spx"
               (cdr (posn-col-row posn))
               (cdr (posn-x-y     posn))))))
  • 1
    For pixels see here Aug 24, 2023 at 19:24
  • It computes the distance from the 'current' beginning of the window, not the 'very' beginning, so I think it is similar to your question. (I did not fully understand your 'sketch' b.t.w., but now I do :) Aug 25, 2023 at 14:14
  • 1
    @dalanicolai: "It computes the distance from the 'current' beginning of the window, not the 'very' beginning, so I think it is similar to your question." -- I tested it again, this time more carefully. Yes, it is. Thank you! To avoid overlap between these 2 questions in terms of pixels, I changed my question's title to take only the number of lines into account. ||| Feel free to edit my answer to include the link you mentioned in the comment.
    – shynur
    Aug 25, 2023 at 16:02
  • I think it is fine as it is... with the link here in the comments (especially after you edited your question :) Thanks for checking and for updating the question... Aug 25, 2023 at 20:00
  • 1
    @dalanicolai: In fact, the linked post has some shortcomings -- it seems to calculate some extra distance. I updated my answer; this time it is perfect.
    – shynur
    Aug 28, 2023 at 10:52

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.