简体   繁体   English

如何使用ZSH ZLE将文本插入到输入提示中

[英]How to insert text into input prompt using ZSH ZLE

I'd like to have a keybinding, that would paste text at the cursor within a prompt. 我想要一个键绑定,它会在提示符下粘贴光标处的文本。

For example: 例如:

Prompt: ls _ 提示: ls _

Press CTRL-Z 按CTRL-Z

Prompt: ls textGoesHere 提示: ls textGoesHere

I'm creating a ZSH ZLE widget, but I can't figure out how to achieve the above. 我正在创建一个ZSH ZLE小部件,但我无法弄清楚如何实现上述目标。

You can access the command buffer from within widgets with the parameters BUFFER , LBUFFER and RBUFFER . 您可以使用参数BUFFERLBUFFERRBUFFER部件中访问命令缓冲区。 BUFFER contains the whole command, while LBUFFER only contains the part left of the current cursor position and RBUFFER the part to the right of the cursor. BUFFER包含整个命令,而LBUFFER仅包含当前光标位置左侧的部分, LBUFFER包含光标RBUFFER的部分。 These parameters can also be modified. 这些参数也可以修改。

If you want to insert some text at the cursor position, you can just prepend the desired text to RBUFFER : 如果要在光标位置插入一些文本,可以将所需的文本添加到RBUFFER

addText () {
    text_to_add="textGoesHere"
    RBUFFER=${text_to_add}${RBUFFER}
}
zle -N addText
bindkey '^Z' addText

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

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