I need to get the full path of the file that I'm editing with emacs.
我需要用emacs编辑文件的完整路径。
- Is there a function for that?
- 有函数吗?
- If not, what would be the elisp function for getting that?
- 如果不是,得到这个的elisp函数是什么?
- How can I copy the result (path name) to a clipboard so that I can reuse it?
- 如何将结果(路径名)复制到剪贴板,以便重用它?
I'm using Mac OS X and Aqumacs.
我用的是Mac OS X和Aqumacs。
(setq filepath (get-fullpath-current-file)) ???
(copy-to-clipboard 'filepath) ???
ADDED
(defun show-file-name () "Show the full path file name in the minibuffer." (interactive) (message (buffer-file-name)) (kill-new (file-truename buffer-file-name)) ) (global-set-key "\C-cz" 'show-file-name)
Combining the two answers that I got, I could get what I want. Thanks for the answers. And some more questions.
结合我得到的两个答案,我可以得到我想要的。谢谢你的答案。和一些更多的问题。
- What's for (file-truename)?
- (file-truename)是什么?
- Can I copy the path name to System(OS)'s clipboard, not the kill ring so that I can use the info with the other apps?
- 我是否可以将路径名复制到System(OS)的剪贴板,而不是kill ring,这样我就可以在其他应用程序中使用这个信息了?
11 个解决方案
#1
78
It's the built-in function buffer-file-name
that gives you the full path of your file.
它是内置的函数缓冲文件名称,它提供文件的完整路径。
The best thing to do is to have your emacs window to always show your system-name and the full path of the buffer you're currently editing :
最好的做法是让emacs窗口始终显示系统名称和当前正在编辑的缓冲区的完整路径:
(setq frame-title-format
(list (format "%s %%S: %%j " (system-name))
'(buffer-file-name "%f" (dired-directory dired-directory "%b"))))
You can also do something like this :
你也可以这样做:
(defun show-file-name ()
"Show the full path file name in the minibuffer."
(interactive)
(message (buffer-file-name)))
(global-set-key [C-f1] 'show-file-name) ; Or any other key you want
#2
54
To borrow from Jérôme Radix's answer, if you just want to quickly see the file path of the current buffer, you can do M-: buffer-file-name
.
借用Jerome Radix的答案,如果您只想快速查看当前缓冲区的文件路径,您可以做M-: buffer-file-name。
#3
33
My trick is to do a C-x C-f
like to open a file, it wil prefill the minibuff with the current file path, C-g
to quit. Faster than M-: buffer-file-name
but far far uglier than any other methods.
我的诀窍是做一个C-x C-f喜欢打开一个文件,它将用当前的文件路径预填充minibuff, C-g要退出。比M-: buffer-file-name快,但比任何其他方法都要丑得多。
#4
14
The direct implementation of what you want is:
您想要的直接实现是:
(defun copy-full-path-to-kill-ring ()
"copy buffer's full path to kill ring"
(interactive)
(when buffer-file-name
(kill-new (file-truename buffer-file-name))))
That said, I find it incredibly useful to be able to get the full path of what is in the minibuffer, and this is what I use:
也就是说,我发现能够得到微型缓冲区中所有内容的完整路径是非常有用的,这就是我所使用的:
(define-key minibuffer-local-completion-map "\C-r" 'resolve-sym-link)
(defun resolve-sym-link ()
"Try to resolve symbolic links into true paths."
(interactive)
(beginning-of-line)
(let* ((file (buffer-substring (point)
(save-excursion (end-of-line) (point))))
(file-dir (file-name-directory file))
(file-true-dir (file-truename file-dir))
(file-name (file-name-nondirectory file)))
(delete-region (point) (save-excursion (end-of-line) (point)))
(insert (concat file-true-dir file-name))))
And then if I want it in the clipboard, I just kill the line (C-a C-k). But we could easily copy the truename to the clipboard in the above command, just change the last line to be:
如果我想把它放到剪贴板中,我就把直线(C-a C-k)但是我们可以很容易地将truename复制到上面命令的剪贴板上,只需将最后一行改为:
(insert (kill-new (concat file-true-dir file-name)))))
The new part is the call to 'kill-new
which puts the string in the kill ring.
新的部分是对“kill-new”的调用,它将字符串放到kill ring中。
#5
4
No need for extra function, just
不需要额外的功能
M-! pwd
#6
3
I have the following code already in use for a long time. It copies the full file path to the kill ring when I press the middle mouse button on the buffer name in the mode-line. It copies just the buffer name to the kill-ring when I press shift-mouse-2 on the buffer-name in the mode-line.
我已经使用了很长时间的代码。当我在模式行中按下缓冲区名称上的鼠标中键时,它将完整的文件路径复制到kill ring。当我在模式行中的缓冲名上按shift-mouse-2时,它只将缓冲区名复制到杀人环。
(defun copy-buffer-file-name (event &optional bufName)
"Copy buffer file name to kill ring.
If no file is associated with buffer just get buffer name.
"
(interactive "eP")
(save-selected-window
(message "bufName: %S" bufName)
(select-window (posn-window (event-start event)))
(let ((name (or (unless bufName (buffer-file-name)) (buffer-name))))
(message "Saved file name \"%s\" in killring." name)
(kill-new name)
name)))
(define-key mode-line-buffer-identification-keymap [mode-line mouse-2] 'copy-buffer-file-name)
(define-key mode-line-buffer-identification-keymap [mode-line S-mouse-2] '(lambda (e) (interactive "e") (copy-buffer-file-name e 't)))
#7
1
Can I copy the path name to System(OS)'s clipboard, not the kill ring so that I can use the info with the other apps?
我是否可以将路径名复制到System(OS)的剪贴板,而不是kill ring,这样我就可以在其他应用程序中使用这个信息了?
You can if you shell out to something like xclip (Linux), pbcopy (Mac), putclip (Cygwin).
你可以用xclip (Linux), pbcopy (Mac), putclip (Cygwin)之类的东西。
I personally use wrapper scripts c
and p
for copy and paste respectively, the first reading from standard input, the latter writing to standard output. That way, this works on all my development platforms:
我个人使用包装脚本c和p分别进行复制和粘贴,首先读取标准输入,然后写入标准输出。这样,这在我所有的开发平台上都适用:
(shell-command (format "echo '%s' | c" buffer-file-name))
I find this more reliable and configurable than using the Emacs clipboard support. For example, my c
command copies the input to all 3 clipboards on Linux (primary, secondary, clipboard), so I can paste with either Ctrl-V or middle click.
我发现这比使用Emacs剪贴板支持更可靠和可配置。例如,我的c命令将输入复制到Linux上的所有3个剪贴板(主、次、剪贴板),所以我可以用Ctrl-V或中间单击粘贴。
#8
1
C-x C-b
shows a list of buffers and the file path for each buffer where applicable.
C-x C-b显示了一个缓冲区列表,以及适用于每个缓冲区的文件路径。
#9
1
C-x C-d
, also callable via M-x list-directory
, will show you the directory for your current file, and you only need to hit the "Enter" key to clear the minibuffer. Additional details are available here.
C-x C-d也可以通过M-x list-directory调用,它将显示当前文件的目录,您只需按下“Enter”键就可以清除minibuffer。这里有更多的细节。
#10
0
The simplest way and would be
最简单的方法
(buffer-name)<(C-x)(C-e)> for the file name to appear in the echo area
(buffer-name)<(C-u)(C-x)(C-e)> would print the location <here>
Borrowing from Trey Jackson I came up with this:
从特雷·杰克逊那里我想到了这个:
(defun buffer-kill-path ()
"copy buffer's full path to kill ring"
(interactive)
(kill-new (buffer-file-name)))
You can find more information on site
你可以在网站上找到更多的信息。
#11
0
copy-buffer-file-name-as-kill
from [0] does exactly what you need I think. It also has the option to copy just directory name, or just file name.
[0]中的copy-buffer-file-name-as-kill完全符合您的需求。它还可以选择只复制目录名或文件名。
[0] http://www.emacswiki.org/emacs/download/buffer-extension.el
[0]http://www.emacswiki.org/emacs/download/buffer-extension.el
#1
78
It's the built-in function buffer-file-name
that gives you the full path of your file.
它是内置的函数缓冲文件名称,它提供文件的完整路径。
The best thing to do is to have your emacs window to always show your system-name and the full path of the buffer you're currently editing :
最好的做法是让emacs窗口始终显示系统名称和当前正在编辑的缓冲区的完整路径:
(setq frame-title-format
(list (format "%s %%S: %%j " (system-name))
'(buffer-file-name "%f" (dired-directory dired-directory "%b"))))
You can also do something like this :
你也可以这样做:
(defun show-file-name ()
"Show the full path file name in the minibuffer."
(interactive)
(message (buffer-file-name)))
(global-set-key [C-f1] 'show-file-name) ; Or any other key you want
#2
54
To borrow from Jérôme Radix's answer, if you just want to quickly see the file path of the current buffer, you can do M-: buffer-file-name
.
借用Jerome Radix的答案,如果您只想快速查看当前缓冲区的文件路径,您可以做M-: buffer-file-name。
#3
33
My trick is to do a C-x C-f
like to open a file, it wil prefill the minibuff with the current file path, C-g
to quit. Faster than M-: buffer-file-name
but far far uglier than any other methods.
我的诀窍是做一个C-x C-f喜欢打开一个文件,它将用当前的文件路径预填充minibuff, C-g要退出。比M-: buffer-file-name快,但比任何其他方法都要丑得多。
#4
14
The direct implementation of what you want is:
您想要的直接实现是:
(defun copy-full-path-to-kill-ring ()
"copy buffer's full path to kill ring"
(interactive)
(when buffer-file-name
(kill-new (file-truename buffer-file-name))))
That said, I find it incredibly useful to be able to get the full path of what is in the minibuffer, and this is what I use:
也就是说,我发现能够得到微型缓冲区中所有内容的完整路径是非常有用的,这就是我所使用的:
(define-key minibuffer-local-completion-map "\C-r" 'resolve-sym-link)
(defun resolve-sym-link ()
"Try to resolve symbolic links into true paths."
(interactive)
(beginning-of-line)
(let* ((file (buffer-substring (point)
(save-excursion (end-of-line) (point))))
(file-dir (file-name-directory file))
(file-true-dir (file-truename file-dir))
(file-name (file-name-nondirectory file)))
(delete-region (point) (save-excursion (end-of-line) (point)))
(insert (concat file-true-dir file-name))))
And then if I want it in the clipboard, I just kill the line (C-a C-k). But we could easily copy the truename to the clipboard in the above command, just change the last line to be:
如果我想把它放到剪贴板中,我就把直线(C-a C-k)但是我们可以很容易地将truename复制到上面命令的剪贴板上,只需将最后一行改为:
(insert (kill-new (concat file-true-dir file-name)))))
The new part is the call to 'kill-new
which puts the string in the kill ring.
新的部分是对“kill-new”的调用,它将字符串放到kill ring中。
#5
4
No need for extra function, just
不需要额外的功能
M-! pwd
#6
3
I have the following code already in use for a long time. It copies the full file path to the kill ring when I press the middle mouse button on the buffer name in the mode-line. It copies just the buffer name to the kill-ring when I press shift-mouse-2 on the buffer-name in the mode-line.
我已经使用了很长时间的代码。当我在模式行中按下缓冲区名称上的鼠标中键时,它将完整的文件路径复制到kill ring。当我在模式行中的缓冲名上按shift-mouse-2时,它只将缓冲区名复制到杀人环。
(defun copy-buffer-file-name (event &optional bufName)
"Copy buffer file name to kill ring.
If no file is associated with buffer just get buffer name.
"
(interactive "eP")
(save-selected-window
(message "bufName: %S" bufName)
(select-window (posn-window (event-start event)))
(let ((name (or (unless bufName (buffer-file-name)) (buffer-name))))
(message "Saved file name \"%s\" in killring." name)
(kill-new name)
name)))
(define-key mode-line-buffer-identification-keymap [mode-line mouse-2] 'copy-buffer-file-name)
(define-key mode-line-buffer-identification-keymap [mode-line S-mouse-2] '(lambda (e) (interactive "e") (copy-buffer-file-name e 't)))
#7
1
Can I copy the path name to System(OS)'s clipboard, not the kill ring so that I can use the info with the other apps?
我是否可以将路径名复制到System(OS)的剪贴板,而不是kill ring,这样我就可以在其他应用程序中使用这个信息了?
You can if you shell out to something like xclip (Linux), pbcopy (Mac), putclip (Cygwin).
你可以用xclip (Linux), pbcopy (Mac), putclip (Cygwin)之类的东西。
I personally use wrapper scripts c
and p
for copy and paste respectively, the first reading from standard input, the latter writing to standard output. That way, this works on all my development platforms:
我个人使用包装脚本c和p分别进行复制和粘贴,首先读取标准输入,然后写入标准输出。这样,这在我所有的开发平台上都适用:
(shell-command (format "echo '%s' | c" buffer-file-name))
I find this more reliable and configurable than using the Emacs clipboard support. For example, my c
command copies the input to all 3 clipboards on Linux (primary, secondary, clipboard), so I can paste with either Ctrl-V or middle click.
我发现这比使用Emacs剪贴板支持更可靠和可配置。例如,我的c命令将输入复制到Linux上的所有3个剪贴板(主、次、剪贴板),所以我可以用Ctrl-V或中间单击粘贴。
#8
1
C-x C-b
shows a list of buffers and the file path for each buffer where applicable.
C-x C-b显示了一个缓冲区列表,以及适用于每个缓冲区的文件路径。
#9
1
C-x C-d
, also callable via M-x list-directory
, will show you the directory for your current file, and you only need to hit the "Enter" key to clear the minibuffer. Additional details are available here.
C-x C-d也可以通过M-x list-directory调用,它将显示当前文件的目录,您只需按下“Enter”键就可以清除minibuffer。这里有更多的细节。
#10
0
The simplest way and would be
最简单的方法
(buffer-name)<(C-x)(C-e)> for the file name to appear in the echo area
(buffer-name)<(C-u)(C-x)(C-e)> would print the location <here>
Borrowing from Trey Jackson I came up with this:
从特雷·杰克逊那里我想到了这个:
(defun buffer-kill-path ()
"copy buffer's full path to kill ring"
(interactive)
(kill-new (buffer-file-name)))
You can find more information on site
你可以在网站上找到更多的信息。
#11
0
copy-buffer-file-name-as-kill
from [0] does exactly what you need I think. It also has the option to copy just directory name, or just file name.
[0]中的copy-buffer-file-name-as-kill完全符合您的需求。它还可以选择只复制目录名或文件名。
[0] http://www.emacswiki.org/emacs/download/buffer-extension.el
[0]http://www.emacswiki.org/emacs/download/buffer-extension.el