我调用git获取toplevel目录(根据 有没有办法在一个命令中获取git根目录? ).
(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))))
Run Code Online (Sandbox Code Playgroud)
但是返回的字符串中有一个尾随换行符.我不知道如何摆脱它.
sli*_*nov 14
我想你能做到
(replace-regexp-in-string "\n$" ""
(shell-command-to-string "git rev-parse --show-toplevel"))
Run Code Online (Sandbox Code Playgroud)
如果您只想在输出的最后删除换行符,请使用
(replace-regexp-in-string "\n\\'" ""
(shell-command-to-string "git rev-parse --show-toplevel"))
Run Code Online (Sandbox Code Playgroud)
接受的答案还用"\n\n"单个换行符("\n")替换输出中的换行符对(),因为$匹配在字符串的末尾或换行符之后,而\\'只匹配字符串末尾的匹配.
小智 5
假设它存储在一个变量中output-string,最后的换行符可以这样删除:
(substring output-string 0 -1)
Run Code Online (Sandbox Code Playgroud)
按照这种shell-command方式,它会看起来像这样:
(substring
(shell-command-to-string "git rev-parse --show-toplevel")
0 -1)
Run Code Online (Sandbox Code Playgroud)