cristobalito
cristobalito

Reputation: 4282

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

Upvotes: 2

Views: 277

Answers (4)

Drew
Drew

Reputation: 30708

You can also bookmark the position: C-x r m.

That will add a bookmark object to your bookmark file: the value of variable bookmark-default-file, which is "~/.emacs.bmk" by default.

You can copy that and send it to your colleague for use in his own bookmark file.

C-x r b will take you directly to a bookmark. See the Emacs manual, node Bookmarks.

Upvotes: 0

JB.
JB.

Reputation: 42154

The buffer name is usually the same as the file name, and by default the line number is on the modeline as well.

I don't know of a pre-existing command that would directly report both using the format you describe, but it's pretty trivial to write your own if the modeline isn't enough.

Upvotes: 1

Trey Jackson
Trey Jackson

Reputation: 74480

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

Upvotes: 5

seh
seh

Reputation: 15269

A couple of functions come to mind:

Neither of them print the name of the buffer's underlying file, if any, but you could write an interactive function that would do so if the mode line display doesn't suit your needs.

Upvotes: 0

Related Questions