Commands to auto-size window width to fit content

I typically have 2 side-by-side windows open while coding, and try to maintain an 80 character line limit that allows the lines in either split to be rendered without wrapping. However, when working on a large codebase with other contributors, I’m often viewing files with long lines that exceed the width of a split.

To compensate for this I put together a few commands w/ associated mappings to allow dynamic window resizing to fit the code I’m looking at.

These do revolve around tmux, but I think most of the code should be useable for any terminal/window manager, where you’d just have to replace the tmux resize-pane command to whatever’s required for your setup.

  • fit-width-to-longest-selected-line (Given user-mode mapping: x)

    • the current pane width will be expanded to the longest line in your selection, which at a minimum is the current line
    • this is limited by the max_window_width option
  • fit-width-to-longest-line-in-buffer (Given user-mode mapping: X)

    • the current pane width will be expanded to the longest line in the entire buffer
  • Some ideas for taking it further…

    • fit-width-to-longest-visible-line
    • Include shrink-to-fit logic (currently it just expands the window)
    • Call command in a hook(s) to resize windows in a completely automated fashion

Here is the Kakscript:

declare-option int max_window_width 150

declare-option int current_gutter_width
declare-option int current_view_width
define-command -override fit-width-to-longest-selected-line \
-docstring 'expand window width to fit selected lines' %{
    set-option window current_gutter_width %sh{
        # the 2 is for a gutter symbol + the line following the line number
        printf "%s" $((${#kak_buf_line_count} + 2))
    set-option window current_view_width %val{window_width}
    set-option -remove window current_view_width %opt{current_gutter_width}
    evaluate-commands -draft %{
        try %{ execute-keys "x<a-s><a-k>.{%opt{current_view_width}}<ret>" }
        echo -debug %sh{
            max() {
                echo "$@" | tr ' ' '\n' | sort -nr | head -n 1
            min() {
                echo "$@" | tr ' ' '\n' | sort -n | head -n 1
            longest_line=$(max $kak_selections_length)
            if [ "$longest_line" -ge "$kak_opt_current_view_width" ]; then
                fit_width=$(($longest_line + $kak_opt_current_gutter_width))
                tmux resize-pane -x $(min $fit_width $kak_opt_max_window_width)
map global user x ":fit-width-to-longest-selected-line<ret>" \
-docstring 'expand window width to fit selected lines'

define-command -override fit-width-to-longest-line-in-buffer \
-docstring 'expand window width to fit all lines in buffer' %{
    # this is wrapped in a command to restore the mark register
    execute-keys 'Z%:fit-width-to-longest-selected-line<ret>z'
map global user X ":fit-width-to-longest-line-in-buffer<ret>" \
-docstring 'expand window width to fit longest line in buffer'