开发者

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
开发者_JAVA技巧


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


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.


A couple of functions come to mind:

  • what-line, which is not usually bound to a key.
  • what-cursor-position, which is usually bound to C-x =.

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜