# Split current buffer to a new window

**URL:** <https://discuss.kakoune.com/t/split-current-buffer-to-a-new-window/1014>\
**Category:** Recipes & Guides\
**Tags:** buffer\
**Created:** [May 13, 2020, 8:59am UTC](https://discuss.kakoune.com/t/split-current-buffer-to-a-new-window/1014 "2020-05-13T08:59:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cipharius](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/cipharius/32/184_2.png) [@cipharius](https://discuss.kakoune.com/u/cipharius)\
**Post date:** [May 13, 2020, 8:59am UTC](https://discuss.kakoune.com/t/split-current-buffer-to-a-new-window/1014/1 "2020-05-13T08:59:54Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![andreyorst](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/andreyorst/32/29_2.png) [@andreyorst](https://discuss.kakoune.com/u/andreyorst)\
**Post date:** [May 13, 2020, 9:32am UTC](https://discuss.kakoune.com/t/split-current-buffer-to-a-new-window/1014/2 "2020-05-13T09:32:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [May 13, 2020, 10:31am UTC](https://discuss.kakoune.com/t/split-current-buffer-to-a-new-window/1014/3 "2020-05-13T10:31:24Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![cipharius](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/cipharius/32/184_2.png) [@cipharius](https://discuss.kakoune.com/u/cipharius)\
**Post date:** [May 13, 2020, 12:27pm UTC](https://discuss.kakoune.com/t/split-current-buffer-to-a-new-window/1014/4 "2020-05-13T12:27:33Z")

</div>

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!
