如何删除从过程输出换行?(How do I delete the newline from a pr

2019-07-17 16:11发布

我叫混帐得到顶层目录(根据是否有办法让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))))

但是有返回的字符串的换行符。 我不知道如何摆脱它。

Answer 1:

我觉得你可以做

(replace-regexp-in-string "\n$" "" 
              (shell-command-to-string "git rev-parse --show-toplevel"))


Answer 2:

如果你只是想在的输出,使用了最后删除换行符

(replace-regexp-in-string "\n\\'" "" 
  (shell-command-to-string "git rev-parse --show-toplevel"))

该接受的答案也取代换行符对( "\n\n"由单一的换行符(输出) "\n" ),因为$字符串的结束或一个换行符后匹配,而\\'只匹配该字符串的结尾。



Answer 3:

假设它被存储在一个可变output-string ,在结束时的最终换行符可以除去这种方式:

(substring output-string 0 -1)

随着shell-command的方式,它应该是这样的:

(substring
  (shell-command-to-string "git rev-parse --show-toplevel")
  0 -1)


文章来源: How do I delete the newline from a process output?
标签: elisp