简体   繁体   English

如何使用命令或部分命令预填充终端提示符?

[英]How can I prepopulate a terminal prompt with a command or partial command?

I would like to write a command/function that will prepopulate my terminal prompt with a partial command or text.我想编写一个命令/函数,用部分命令或文本预填充我的终端提示符。 I need to prefix my git commit messages and I was thinking it would be useful for this (and other uses) if I could prefill the prompt with a partial command rather than always needing to retype the same thing.我需要在我的 git 提交消息前加上前缀,我认为如果我可以用部分命令预填充提示而不是总是需要重新键入相同的内容,这将对这个(和其他用途)有用。

> gencommit
> git commit -m "ABC-123: 

In the code snippet above, git commit -m "ABC-123: is placed on the command line and I would simply be able to add a commit message and execute the command.在上面的代码片段中, git commit -m "ABC-123:被放置在命令行上,我可以简单地添加一条提交消息并执行命令。

echo is the only thing that comes to mind but that does not do what I want, If gencommit used echo it would look something like this. echo是唯一想到的东西,但它没有做我想要的,如果gencommit使用 echo 它看起来像这样。

> gencommit
git commit -m "ABC-123:
> 

As far as I know, bash doesn't provide any way to write to the Readline buffer programatically.据我所知, bash没有提供任何方式以编程方式写入Readline 缓冲区。 ( zsh does, as an aside.) One alternative is to make gencommit a Readline macro. zsh做,顺便说一句。)一种替代方法是使gencommit成为 Readline 宏。 For example, add this to your .inputrc file (creating the file if necessary):例如,将其添加到您的.inputrc文件(如有必要,创建该文件):

"gencommit": "git commit -m \"ABC-123:"

One thing about his approach that I find disconcerting is that because a macro is text that gets replaced immediately, Readline won't actually echo the text you are typing to the screen until it has determined what macro you are typing, so while you are typing gencommit , nothing appears on your screen until you finish, then the expansion text appears.关于他的方法,我觉得令人不安的一件事是,因为宏是立即被替换的文本,Readline 实际上不会将您输入的文本回显到屏幕上,直到它确定您输入的是什么宏,所以当您输入时gencommit ,在完成之前屏幕上不会出现任何内容,然后会出现扩展文本。 Worse, if you make a mistake while typing, you have to cancel the command and start over.更糟糕的是,如果您在键入时出错,您必须取消命令并重新开始。

Better is to define an alias更好的是定义一个别名

alias gencommit='git commit -m "ABC-123:'

and, after typing gencommit on the command line, use the shell-expand-line Readline function (bound to M-! by default; M is the meta key, which is either Alt or Esc, depending on your platform and terminal emulator settings) to expand it to the replacement text.并且,在命令行中输入gencommit后,使用shell-expand-line Readline function(默认绑定到M-! -!; M是元键,可以是 Alt 或 Esc,具体取决于您的平台和终端仿真器设置)将其扩展为替换文本。 (This tries to expand anything on the command line: aliases, parameters, history expansions, etc. There is a dedicated alias-expand-line function that only expands aliases, but it is unbound by default. I leave it as an exercise to bind this command to an unused key sequence if you like.) (这会尝试在命令行上扩展任何内容:别名、参数、历史扩展等。有专用的alias-expand-line function扩展别名,但默认情况下未绑定。我将其作为练习留给绑定如果您愿意,可以将此命令添加到未使用的键序列。)


As I alluded to zsh , the command in that shell would simply be正如我提到的zsh , shell 中的命令只是

print -z 'git commit -m "ABC-123:'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM