以下代码(使用 zsh 功能,但原理也可用于其他 shell)定义了两个 shell 函数,prompt_middle
并且prompt_restore
.
第一个函数通过在提示下方强制添加适当数量的空行,使提示始终位于终端中间上方。后一个功能恢复正常行为。
您可以将这些功能分配给某些快捷方式或使用某些逻辑在这两种模式之间切换。
# load terminfo modules to make the associative array $terminfo available
zmodload zsh/terminfo
# save current prompt to parameter PS1o
PS1o="$PS1"
# calculate how many lines one half of the terminal's height has
halfpage=$((LINES/2))
# construct parameter to go down/up $halfpage lines via termcap
halfpage_down=""
for i in {1..$halfpage}; do
halfpage_down="$halfpage_down$terminfo[cud1]"
done
halfpage_up=""
for i in {1..$halfpage}; do
halfpage_up="$halfpage_up$terminfo[cuu1]"
done
# define functions
function prompt_middle() {
# print $halfpage_down
PS1="%{${halfpage_down}${halfpage_up}%}$PS1o"
}
function prompt_restore() {
PS1="$PS1o"
}
Run Code Online (Sandbox Code Playgroud)
$halfpage_up/down
就我个人而言,我不会在两种模式之间切换,而是使用一种更简单的方法(您需要上面的定义):
magic-enter () {
if [[ -z $BUFFER ]]
then
print ${halfpage_down}${halfpage_up}$terminfo[cuu1]
zle reset-prompt
else
zle accept-line
fi
}
zle -N magic-enter
bindkey "^M" magic-enter
Run Code Online (Sandbox Code Playgroud)
这会检查当前命令行是否为空(请参阅我的其他答案),如果是,则将提示符移至终端中间。现在,您可以再按一次该ENTER
键来快进提示(或者您可能想将其称为“双击”,类似于“双击”)。