# Poor man's surround.kak

**URL:** <https://discuss.kakoune.com/t/poor-mans-surround-kak/1826>\
**Category:** Recipes & Guides\
**Created:** [August 5, 2021, 4:21pm UTC](https://discuss.kakoune.com/t/poor-mans-surround-kak/1826 "2021-08-05T16:21:28Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [August 5, 2021, 4:21pm UTC](https://discuss.kakoune.com/t/poor-mans-surround-kak/1826/1 "2021-08-05T16:21:29Z")

</div>

```auto
define-command -override my-surround -docstring %{
 Surround all selections with the typed character. 
} %{ on-key %{ evaluate-commands %sh{
  left=$kak_key
  right=$kak_key
  pair() {
   ( ["$kak_key" = "$1"] || ["$kak_key" = "$2"] )
   && left=$1 && right=$2
  }
  pair '(' ')' || pair '[' ']' ||
  pair '{' '}' || pair '<lt>' '<gt>'
  printf "execute-keys %%{i%s<esc>a%s<esc>}" "$left" "$right"
}}}

```

---

<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 9, 2022, 9:08am UTC](https://discuss.kakoune.com/t/poor-mans-surround-kak/1826/2 "2022-02-09T09:08:05Z")

</div>

editorial error in the `pair` function (but I can’t edit anymore), a correct version is

```auto
  pair() {
   ( ["$kak_key" = "$1"] || ["$kak_key" = "$2"] ) \
   && left=$1 && right=$2
  }

```

---

<div class="post-metadata">

**Author:** ![alexherbo2](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/alexherbo2/32/1140_2.png) [@alexherbo2](https://discuss.kakoune.com/u/alexherbo2)\
**Post date:** [February 9, 2022, 2:58pm UTC](https://discuss.kakoune.com/t/poor-mans-surround-kak/1826/3 "2022-02-09T14:58:56Z")

</div>

How about using a user mode?

```auto
declare-user-mode surround

define-command declare-surrounding-pair -params 4 -docstring 'declare-surrounding-pair <description> <alias> <opening> <closing>: declare surrounding pair' %{
  map -docstring %arg{1} global surround %arg{2} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
  map -docstring %arg{1} global surround %arg{3} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
  map -docstring %arg{1} global surround %arg{4} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
}

declare-surrounding-pair 'parenthesis block' b ( )
declare-surrounding-pair 'brace block' B { }
declare-surrounding-pair 'bracket block' r []
declare-surrounding-pair 'angle block' a <lt> <gt>

map -docstring 'enter surround mode' global normal q ': enter-user-mode surround<ret>'

```
