我打电话给git获取顶级目录(根据 Is there a way to get the git root directory in one command? )。
(let ((tmpbuffer (get-buffer-create (make-temp-name "git"))))
(call-process "git" nil tmpbuffer nil "rev-parse" "--show-toplevel")
(with-current-buffer tmpbuffer
(with-output-to-string
(princ (buffer-string))
(kill-buffer))))
但是返回的字符串中有一个尾随换行符。我不知道如何摆脱它。
答案 0 :(得分:14)
我想你可以做到
(replace-regexp-in-string "\n$" ""
(shell-command-to-string "git rev-parse --show-toplevel"))
答案 1 :(得分:7)
如果您只想删除输出末尾的换行符,请使用
(replace-regexp-in-string "\n\\'" ""
(shell-command-to-string "git rev-parse --show-toplevel"))
接受的答案还会通过单个换行符("\n\n"
)替换输出中的换行符对("\n"
),因为$
匹配字符串末尾或换行符后,而\\'
仅匹配字符串末尾。
答案 2 :(得分:3)
假设它存储在变量output-string
中,可以用以下方式删除最后的换行符:
(substring output-string 0 -1)
采用shell-command
方式,看起来像这样:
(substring
(shell-command-to-string "git rev-parse --show-toplevel")
0 -1)
答案 3 :(得分:0)
如果您对 awesome 外部软件包感到满意,那么。
使用s-trim
ELISP> (shell-command-to-string "light -G")
"60.00\n"
ELISP> (s-trim (shell-command-to-string "light -G"))
"60.00"