Split current buffer to a new window

It was irritating me, how new command causes seemingly random buffer to open up, so I created a command which creates identical copy of current window.

define-command split -params 0 -docstring \
"Creates new client with current buffer" \
  new %sh{
    line=$(echo "$kak_window_range" | cut -d' ' -f 1)
    column=$(echo "$kak_window_range" | cut -d' ' -f 2)
    printf 'buffer -- %s;' "$kak_bufname"
    printf 'execute-keys %s jvt;' "$line"
    printf 'select -- %s;' "$kak_selection_desc"
    printf 'execute-keys V %s <esc>;' "$(printf "%""$column""s" | tr ' ' 'l')"

I can’t guarantee that windows will be identical in case if your windows manager doesn’t ensure that the new kakoune window is equally sized as the original.

Personally I use tiling WM, which divides old windows space in half, to make space for the new buffer. It works great with such setup.

Edit: Took inspiration from @Screwtapello’s suggestion and also upgraded command to take in account the view’s horizontal alignment.

Before you’ve mentioned it, I’ve actually never noticed that new may use buffer different from the active one. That’s interesting

I haven’t tested this, but maybe that command could be implemented as something like:

define-command split %{
    new eval buffer %val{bufname} ';' select %val{selection}

This looks neat, but doesn’t account for matching the view of the old window. It also reminded that my command didn’t account for view’s horizontal position.

I ended up using select approach and few other new tricks to create a better version of the split command. It should also be more readable now. Updated the original post!