Bash Windows 10 CMD/Powershell缓冲区仅在放大控制台窗口时调整;收缩时不会。修理

Bash Windows 10 CMD/Powershell缓冲区仅在放大控制台窗口时调整;收缩时不会。修理,bash,shell,powershell,vim,windows-console,Bash,Shell,Powershell,Vim,Windows Console,我有一个看似简单的问题,但我的谷歌fu让我失望: 最近,我开始在我的工作流程中更多地使用CMD/Powershell,用于很多事情,包括VIM、编译和Bash(Window的Ubuntu子系统)。这些都在Windows的shell界面中运行(如果这是正确的术语,那么它与Console 2/ConEmu不同,后者只是围绕shell的包装,不会改变其行为) 我的问题是: 将外壳窗口的大小调整得更大时,缓冲区会调整大小以适应它。 但是,当重新调整窗口大小为较小时,缓冲区保持较大,滚动条出现,我必须使用

我有一个看似简单的问题,但我的谷歌fu让我失望:

最近,我开始在我的工作流程中更多地使用CMD/Powershell,用于很多事情,包括VIM、编译和Bash(Window的Ubuntu子系统)。这些都在Windows的shell界面中运行(如果这是正确的术语,那么它与Console 2/ConEmu不同,后者只是围绕shell的包装,不会改变其行为)

我的问题是: 将外壳窗口的大小调整得更大时,缓冲区会调整大小以适应它。 但是,当重新调整窗口大小为较小时,缓冲区保持较大,滚动条出现,我必须使用滚动条查看整个窗口

我不想要这种行为的后半部分,因为它会导致像VIM这样的程序出现问题,而且很烦人

实际上,我喜欢Windows10中的传统控制台行为,就好像它不能动态调整大小一样,它可以最小化或最大化,缓冲区可以随着这两个操作一起调整大小。但是,我不能简单地启用此选项,因为某些东西(如Bash)需要现代的windows10控制台,并且不会在传统模式下运行

有没有其他方法可以让缓冲区根据CMD或Powershell的窗口大小正确调整大小,或者使用不同于CMD的设置运行Powershell(因此我可以在其中一个中启用旧版控制台,而不是在另一个中启用旧版控制台--它们当前似乎共享相同的设置)

此外,如果有任何替代的修复程序或控制台外壳可以解决这个问题,请随时提出建议!我试过Mintty,它几乎完美,但弄乱了我在vim中的寄存器/剪贴板设置。

  • 正如您所发现的,切换到Windows 10上的旧版控制台(通过控制台快捷方式的属性对话框、选项卡选项、复选框
    使用旧版控制台(需要重新启动)
    很不幸,是一个全局设置(影响所有未来的控制台窗口,而不管在其中运行shell)

  • Windows 10的在使用鼠标调整大小时总是将缓冲区宽度设置为窗口宽度,从而避免了水平滚动的需要(当您使用鼠标缩小窗口时,传统控制台保留原来较大的缓冲区宽度,此时会出现一个水平滚动条;作为旁白:传统控制台不允许使用鼠标扩大窗口)

    • 如果您的快捷方式文件在未处于传统模式时没有以这种方式运行,请重新创建快捷方式文件
注意:上述术语“旧版”和“新控制台”指的是基于旧版
conhost.exe
的控制台窗口的整体模式,与现代的后续控制台不同


因此,如果以下任何一项适用,则本答案的剩余部分仅具有意义:

  • 您正在使用传统控制台——可能是因为您选择在Windows 10上这样做,或者是因为您运行的是Windows 8.1或更低版本,并且需要一个简单的命令来解决水平滚动问题

  • 您希望通过快捷方式文件修改控制台窗口的启动维度

  • 您对通过编程调整控制台窗口大小的脚本感兴趣


当你使用鼠标缩小窗口时,没有简单的UI修复程序可以避免水平滚动——除了使用窗口的系统菜单的
属性
对话框使缓冲区宽度与窗口匹配之外,这很麻烦

下面是一个基于鼠标调整大小后可以运行的命令,用于解决水平滚动问题:

  • PowerShell

      [console]::BufferWidth = [console]::WindowWidth
    
    因此,您不必每次都键入它,将其放入函数中,例如,
    fixwin
    ,然后将其添加到您的
    $PROFILE
    (初始化脚本):

  • cmd.exe

      [console]::BufferWidth = [console]::WindowWidth
    
    使用
    模式
    不是一个选项,因为它会将您的缓冲区高度设置为窗口高度,因此您将丢失后滚缓冲区-请参阅

    但是,您可以使用DOSKEY宏临时调用上述PowerShell命令:

      doskey fixwin=powershell -noprofile -command "[console]::bufferwidth = [console]::windowwidth"
    
    因此,您不必在每个会话中定义宏,将命令保存到批处理文件中,例如文件夹
    %USERPROFILE%
    中的
    .cmdrc.cmd
    ,然后修改用于启动
    cmd.exe
    的快捷方式文件,如下所示:

    • 打开快捷方式文件的属性对话框(通过快捷菜单,通过右键单击;对于任务栏项目,右键单击与任务栏相关的快捷菜单,然后右键单击表示基础快捷方式文件的倒数第二个项目)

    • 在快捷方式选项卡中,将文本框目标的现有内容替换为以下内容:
      %windir%\system32\cmd.exe/k”%USERPROFILE%\.cmdrc.cmd“


作为事后调整窗口大小的替代方法,您可以使用快捷方式文件启动具有预配置维度的控制台,,该控制台可用于旧控制台和新控制台:

  • 创建指向感兴趣的可执行文件(
    cmd.exe
    powershell.exe
    )的快捷方式文件

  • 启动快捷方式

  • 使用窗口的系统菜单的
    属性
    对话框设置所需的窗口和缓冲区尺寸

下次启动该快捷方式文件时,以前配置的尺寸标注应再次生效

要记住窗口位置,请根据需要定位窗口,再次打开系统菜单的
属性
对话框,取消选中
让系统定位窗口
,然后单击
确定


cmd.exe
  @powershell.exe -executionpolicy unrestricted -noprofile -file "%~dpn0.ps1" %*
<#
.SYNOPSIS
Resizes the console window.

.DESCRIPTION
Resizes the current console window to the specified width (in columns)
and/or height (in lines.

Note that the window position doesn't change; i.e., the top-left corner of
the window remains in place, and the width / height expands or shrinks.

To only change the width, specify a single value; e.g.: 120

To only change the height, specify 0 for the width; e.g.: 0 60

Specify neither if you don't want to resize, but want to ensure that the
buffer width is set to the same value as the window width, so as to avoid
the need for horizontal scrolling.
This is convenient after having resized the window with the mouse.

Note that specifying values that are too large causes an error.


.PARAMETER Width
The new width (in columns) to resize the current console window to.

.PARAMETER Height
The new height (in lines) to resize the current console window to.

.PARAMETER KeepBufferWidth
By default, the buffer width is always set to the resulting window width, 
so as to avoid the need for horizontal scrolling. 
Use this switch if you want to keep the current buffer width.

.EXAMPLE
> rw 100 50
Makes the current console window 100 columns wide, and 50 lines tall.

> rw 100
Makes the current console window 100 columns, without changing the height.

> rw 0 70
Makes the current console window 70 lines tall, without changing the width.

> rw
Doesn't perform resizing, but ensures that the buffer width equals the
window width, so as to prevent horizontal scrolling.

#>

[cmdletbinding()]
param(
  [uint16] $Width,
  [uint16] $Height, 
  [switch] $KeepBufferWidth
)

if ($width) { [console]::WindowWidth = $width }
if ($height) { [console]::WindowHeight = $height }
# Unless asked not to, always set the buffer width to the window width to 
# prevent horizontal scrolling.
if (-not $KeepBufferWidth) { [console]::BufferWidth = [console]::WindowWidth }