# Open selection in new buffer

**URL:** <https://discuss.kakoune.com/t/open-selection-in-new-buffer/2407>\
**Category:** Help\
**Created:** [February 21, 2024, 3:42pm UTC](https://discuss.kakoune.com/t/open-selection-in-new-buffer/2407 "2024-02-21T15:42:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hjagu09](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/hjagu09/32/1447_2.png) [@Hjagu09](https://discuss.kakoune.com/u/Hjagu09)\
**Post date:** [February 21, 2024, 3:42pm UTC](https://discuss.kakoune.com/t/open-selection-in-new-buffer/2407/1 "2024-02-21T15:42:27Z")

</div>

I would like to be able to take a piece of text and open it in a new buffer. I have this right now:

```kak
define-command chunk %{
	execute-keys '"Oy'
	edit -scratch "chunk"
	execute-keys '"Op'
	format
}

```

It kinda works but it doesn’t format properly and i don’t get any syntax highlighting as it doesn’t set the new buffers file type. How could i make it so that the new buffer gets the same file type as the one i called the command in?

---

<div class="post-metadata">

**Author:** ![krobelus](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/krobelus/32/616_2.png) [@krobelus](https://discuss.kakoune.com/u/krobelus)\
**Post date:** [February 21, 2024, 5:45pm UTC](https://discuss.kakoune.com/t/open-selection-in-new-buffer/2407/2 "2024-02-21T17:45:07Z")

</div>

(untested)

```auto
define-command chunk %{
	execute-keys '"Oy'
	evaluate-commands %exp{
		edit -scratch chunk
		set-option buffer filetype %opt{filetype}
	}
	execute-keys '"Op'
	format
}

```

if your Kakoune version doesn’t support `%exp{foo}` then use `"foo"` instead

---

<div class="post-metadata">

**Author:** ![ftonneau](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ftonneau/32/295_2.png) [@ftonneau](https://discuss.kakoune.com/u/ftonneau)\
**Post date:** [February 21, 2024, 5:58pm UTC](https://discuss.kakoune.com/t/open-selection-in-new-buffer/2407/3 "2024-02-21T17:58:56Z")

</div>

I am not sure that Krobelus’ solution works, because it tries to save the filetype once the scratch buffer is already open (with an empty filetype). Perhaps it is safer to save the filetype information in a dedicated option and while still in the first buffer:

```auto
declare-option str scratch_type

define-command chunk %{
    set-option global scratch_type %opt(filetype)
    # ^-- use global scope so that the value of scratch_type persists
    # in the newly created buffer
    execute-keys '"Oy'
    edit -scratch chunk
    execute-keys '"Op'
    set-option buffer filetype %opt(scratch_type)
    format
}

```

Edit: I just tried and Krobelus’ solution does work! My bad ☹

---

<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:** [February 21, 2024, 8:42pm UTC](https://discuss.kakoune.com/t/open-selection-in-new-buffer/2407/4 "2024-02-21T20:42:09Z")

</div>

Interesting, wasn’t aware of `%exp{}` qutoed string, which is the magic why it works. It recursively expands it’s contents, so the expansion of `%opt{filetype}` is done before evaluating the commands.

---

<div class="post-metadata">

**Author:** ![ftonneau](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ftonneau/32/295_2.png) [@ftonneau](https://discuss.kakoune.com/u/ftonneau)\
**Post date:** [February 21, 2024, 9:08pm UTC](https://discuss.kakoune.com/t/open-selection-in-new-buffer/2407/5 "2024-02-21T21:08:32Z")

</div>

Yes, this is what does the trick. Hence the solution with `%exp` is short and elegant.
