简体   繁体   English

是否可以在bash中复制vim的zz效果?

[英]Is it possible to replicate the effect of vim's `zz` in bash?

In Vim, executing zz in normal mode will take the line the cursor is currently on, and move that line, together with the cursor, to the vertical center of the current window. 在Vim中,在正常模式下执行zz将采用光标当前所在的行,并将该行与光标一起移动到当前窗口的垂直中心。

Is there a way to replicate this behavior in bash , to move the current command prompt to the vertical center of the screen, and scroll the command buffer along with it? 有没有一种方法可以在bash复制此行为,将当前命令提示符移动到屏幕的垂直中心,并与之一起滚动命令缓冲区?

I am posting on StackOverflow instead of Unix/Linux because I am open to solutions that require writing custom code if bash does not natively support this. 我发布在StackOverflow而不是Unix/Linux因为如果bash本身不支持此功能,我愿意要求编写自定义代码的解决方案。

Vim controls the entire area of the screen. Vim控制屏幕的整个区域。 Bash is only responsible for the input line. Bash仅负责输入行。 It has no idea about the contents of the rest of the screen, which is/was under control of other programs, and cannot reposition it. 它不知道屏幕其余部分的内容,该内容受其他程序控制,因此无法重新定位。

Note that the contents of the screen nornally cannot be read by a program running on it. 请注意,屏幕上的内容通常无法由其上运行的程序读取。 The only way to know what's on the screen is to start from a clean state and account for every single character and terminal command printed. 知道屏幕上内容的唯一方法是从干净状态开始,并考虑打印的每个字符和终端命令。 Vim does just that; Vim就是这样做的。 Bash does not and can not. Bash不能也不能。

nm has already explained that bash itself cannot do it. nm已经解释了bash本身无法做到。 However, you still might be able to send commands directly to the underlying terminal to achieve an effect similar to what you want. 但是,您仍然可以直接将命令发送到基础终端,以实现与所需功能类似的效果。 For example, assuming that your bash input line is somewhere below the middle of the screen, the following will scroll down by sending a couple of empty lines and then putting you into the middle of the screen: 例如,假设您的bash输入行位于屏幕中间下方的某处,则以下内容将向下滚动,方法是发送几行空行,然后将您置于屏幕中间:

 declare -i L; declare -i M; L=`tput lines`/2; M=$L-1; for (( c=1; c<=$L; c++ )); do echo; done; tput cup $M

Note that this doesn't work when your somewhere in the upper half of the screen (it will put you into the middle of the screen, but will not scroll back). 请注意,当您位于屏幕上半部分的某个位置时,此功能不起作用(它将使您进入屏幕中间,但不会向后滚动)。

For more info see man tput and man terminfo . 有关更多信息,请参见man tputman terminfo

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

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