2

I have a seemingly simple question, but my google-fu is failing me:

I have started using CMD / Powershell more in my workflow recently, for a myriad of things including VIM, compiling, and Bash (Window's Ubuntu Subsystem). These all run in window's shell interface (if that is the right term for it? As distinct from Console 2/ConEmu, which are just wrappers around the shell and don't change its behavior).

Here's my problem: When adjusting the shell window size larger, the buffer resizes to fit it. However, when readjusting the window size to be smaller, the buffer stays large and scroll bars appear, and I have to use the scroll bars to view my entire window.

I don't want the latter half of this behavior, as it causes problems with programs like VIM, and is just annoying.

I actually like legacy console behavior in windows 10, as though it cannot be dynamically resized, it can be minimized or maximized and the buffer resizes along with both actions. However, I cannot simply enable this option as some things such as Bash require the modern windows 10 console and won't run in legacy mode.

Are there any other ways to have the buffer resize correctly along with the size of the window for either CMD or Powershell, or alternatively, to run Powershell with different settings than CMD (so I could have legacy console enabled in one, and not in the other -- they seem to share the same settings currently).

Additionally, if there are any alternative fixes or console shells that might solve this, feel free to suggest these as well! I've tried Mintty, and it's almost perfect, but messes up my register/clipboard settings in vim.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 2
    This Q is not about programming as defined for StackOverflow. It **may** be more appropriate on http://superuser.com or another StackExchange site. Use the `flag` link at the bottom of your Q and ask the moderator to move it. Please don't post the same Q on 2 different sites. ***Please*** read http://stackoverflow.com/help/how-to-ask http://stackoverflow.com/help/dont-ask and http://stackoverflow.com/help/mcve before posting more Qs here. Good luck. – shellter Feb 16 '17 at 03:09
  • The shell is still a programming tool. Some of the questions about your programming environment are still on topic – Matt Feb 16 '17 at 03:35
  • shellter, I have read both, and was under the impression that my question is still on topic as it's about the tools that I use when programming, moreover, I have seen many questions about cmd / bash / vim here on SO. Am I mistaken / still doing things wrong? I'm open to correction / input, but I'd like to understand & confirm the need to move this question if it is necessary. According to the page on "what kind of questions can I ask here?", my question should fall under the third option, "software tools commonly used by programmers..." – Luke Dupont Feb 16 '17 at 04:37

1 Answers1

3
  • As you've discovered, switching to the legacy console on Windows 10 (via a console shortcut's Properties dialog, tab Options, checkbox Use legacy console (requires relaunch) is, unfortunately, a global setting (affects all future console windows, irrespective of the shell run in them).

  • Windows 10's new console, always sets the buffer width to the window width when you resize with the mouse, avoiding the need for horizontal scrolling (whereas the legacy console retains the original, larger buffer width when you make a window narrower with the mouse, at which point a horizontal scrollbar appears; as an aside: the legacy console doesn't allow making a window wider using the mouse).

    • If a shortcut file of yours doesn't behave that way while not in legacy mode, recreate the shortcut file.

Note: The terms legacy and new console above refer to modes of the legacy conhost.exe-based console windows overall, as distinct from their modern successor, Windows Terminal.


Therefore, the remaining part of this answer is only of interest, if any of the following apply:

  • you're using the legacy console - either because you've opted to do so on Windows 10 or because you're running on Windows 8.1 or below - and want a simple command to fix the horizontal scrolling issue.

  • you want to modify the startup dimensions of your console window by way of shortcut files.

  • you're interested in a script that programmatically resizes a console window.


There's no simple UI fix to avoid the horizontal scrolling when you narrow a window using the mouse - short of using the window's system menu's Properties dialog to make the buffer width match the window with, but that's cumbersome.

Here is a command you can run after mouse-based resizing to fix the horizontal scrolling issue:

  • PowerShell:

      [console]::BufferWidth = [console]::WindowWidth
    

    So you don't have to type this every time, put it in a function, say, fixwin, and add it to your $PROFILE (initialization script):

      function fixwin { [console]::BufferWidth = [console]::WindowWidth }
    
  • cmd.exe:

    Using mode is not an option, because it would set your buffer height to the window height as well, so you'd lose your scroll-back buffer - see this answer.

    You, can, however, call the above PowerShell command ad-hoc, using a DOSKEY macro:

      doskey fixwin=powershell -noprofile -command "[console]::bufferwidth = [console]::windowwidth"
    

    So you don't have to define the macro in every session, save the command to a batch file, say, .cmdrc.cmd in folder %USERPROFILE%, then modify the shortcut file that you use to start cmd.exe as follows:

    • Open the shortcut file's Properties dialog (via the shortcut menu, by right-clicking; for a taskbar item, right-click for the taskbar-related shortcut menu, then right-click the second to last item representing the underlying shortcut file).

    • In the Shortcut tab, replace the existing content of text box Target with the following:
      %windir%\system32\cmd.exe /k "%USERPROFILE%\.cmdrc.cmd"


As an alternative to resizing a window after the fact, you can use a shortcut file to launch a console with preconfigured dimensions, which works with both the legacy and the new consoles:

  • Create a shortcut file pointing to the executable of interest (cmd.exe or powershell.exe).

  • Launch the shortcut.

  • Use the window's system menu's Properties dialog to set the desired window and buffer dimensions.

The next time you launch that shortcut file, the previously configured dimensions should take effect again.

To also remember the window position, position the window as desired, open the system menu's Properties dialog (again), uncheck Let system position window, and click OK.


cmd.exe / PowerShell script for fixing the buffer-width issue and/or programmatically resizing the window:

If you follow the instructions below, you'll be able to:

  • Simply execute rw (for resize window) after having used the mouse to narrow the window, so as to make the buffer width the same as the window width.

  • Alternatively, call rw <new-width> [<new-height>] to resize your window programmatically, which also sets the buffer width to the window width.

Instructions:

  • Pick or add a directory in your %PATH% in which to place the scripts below.

  • Create wrapper batch file rw.cmd with the following content:

      @powershell.exe -executionpolicy unrestricted -noprofile -file "%~dpn0.ps1" %*
    
  • Create PowerShell script rw.ps1 with the following content (ignore the broken syntax-highlighting):

<#
.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 }
mklement0
  • 382,024
  • 64
  • 607
  • 775