Search code examples
emacsbookmarks

Emacs: Determine location in file


Say I've been browsing a source file in emacs and I've noticed something that I'd like to bring to the attention of a colleague. Is there an easy way (i.e. command) to get the file name and location of the point, e.g. if I'm on line 21 in foo.cpp

c:\temp\foo.cpp:21

Solution

  • This function does what you want. It displays the information as a message, and adds it to the kill-ring (for easy pasting).

    (defun get-file-line ()
      "show (and set kill-ring) current file and line"
      (interactive)
      (unless (buffer-file-name)
        (error "No file for buffer %s" (buffer-name)))
      (let ((msg (format "%s:%d"
                         (file-truename (buffer-file-name))
                         (line-number-at-pos))))
        (kill-new msg)
        (message msg)))