在Emacs lisp中,对ls命令使用调用过程的正确方法是什么?
我想在emacs lisp中执行以下shell命令:在Emacs lisp中,对ls命令使用调用过程的正确方法是什么?,emacs,lisp,Emacs,Lisp,我想在emacs lisp中执行以下shell命令: ls -t ~/org *.txt | head -5 我在以下方面的尝试: (call-process "ls" nil t nil "-t" "~/org" "*.txt" "| head -5") 导致 ls: ~/org: No such file or directory ls: *.txt: No such file or directory ls: |head -5: No such file or directory 任
ls -t ~/org *.txt | head -5
我在以下方面的尝试:
(call-process "ls" nil t nil "-t" "~/org" "*.txt" "| head -5")
导致
ls: ~/org: No such file or directory
ls: *.txt: No such file or directory
ls: |head -5: No such file or directory
任何帮助都将不胜感激。问题是像
~
、*
和|
这样的令牌不会被ls
程序处理/扩展。由于令牌未被处理,ls
将查找字面上称为~/org
的文件或目录、字面上称为*.txt
的文件或目录以及字面上称为|head-5
的文件或目录。因此,您收到的关于“无此类文件或目录”的错误消息
这些令牌由shell处理/扩展(如bourneshell/bin/sh或Bash/bin/Bash).从技术上讲,标记的解释可以是特定于shell的,但大多数shell至少以相同的方式解释一些相同的标准标记,例如,
意味着将程序端到端地连接到几乎所有shell。作为反例,Bourne shell(/bin/sh)不进行~
tilde/home目录扩展
如果要获得扩展,必须让调用程序像shell一样自行进行扩展(很难),或者在shell中运行ls
命令(更简单):
所以
编辑:澄清了一些问题,比如提到
/bin/sh
不进行~
扩展。根据您的用例,如果您发现自己想要执行shell命令并经常在新的缓冲区中提供输出,您还可以使用shell命令
功能。在您的示例中,它看起来像这样:
(shell-command "ls -t ~/org *.txt | head -5")
但是,要将其插入当前缓冲区,需要使用类似于
(通用参数)的方法手动设置当前前缀arg
,这有点麻烦。另一方面,如果您只想在某个地方获得输出并处理它,shell命令
将与其他任何命令一样工作。我认为您不需要手动设置当前前缀arg
。shell命令
采用两个可选参数输出缓冲区
error buffer
。我看到了,但注意到如果指定output buffer
为当前缓冲区,它会先擦除缓冲区,然后推入命令的结果,我认为这通常是不可取的。我还没有尝试过,所以我不能确定。如果希望输出到当前缓冲区,可以使用一个既不是缓冲区也不是nil的参数作为输出缓冲区
。然后插入输出,而不是替换当前内容。例如(shell命令“ls”t)
对于记录,还有(插入(shell命令到字符串“echo'shell命令yay'))
,这有时是一种更好的方法。@eatload-gald能够提供帮助-R.P.Dillon的shell命令
似乎也是一个不错的选择。
(call-process "/bin/bash" nil t nil "-c" "ls -t ~/org *.txt | head -5")
(shell-command "ls -t ~/org *.txt | head -5")