# Tag stack

**URL:** <https://discuss.kakoune.com/t/tag-stack/1254>\
**Category:** Recipes & Guides\
**Created:** [September 8, 2020, 10:50am UTC](https://discuss.kakoune.com/t/tag-stack/1254 "2020-09-08T10:50:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![listentolist](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/listentolist/32/416_2.png) [@listentolist](https://discuss.kakoune.com/u/listentolist)\
**Post date:** [September 8, 2020, 10:50am UTC](https://discuss.kakoune.com/t/tag-stack/1254/1 "2020-09-08T10:50:33Z")

</div>

One feature from Vim that I was missing in Kakoune is the tag stack. Here is a very basic implementation:

```auto
declare-option str-list tagstack ""
declare-option str tagstack_cmd "ctags-search"

define-command -override -params 1 tagstack-push %{
  set-option global tagstack "%val{buffile}" "%val{cursor_line}" \
    "%val{cursor_column}" "%arg{1}" %opt{tagstack}
  try %{
    %opt{tagstack_cmd} "%arg{1}"
  } catch %{
    tagstack-pop
    fail "tag not found %arg{1}"
  }
}

define-command -override tagstack-pop %{
  evaluate-commands %sh{
    eval "set -- $kak_quoted_opt_tagstack"
    if ["$1"]; then
      echo edit "$1" "$2" "$3"
      shift 4
    else
      echo fail "reached bottom of tag stack"
    fi
    echo set global tagstack "$@"
  }
}

define-command -override tagstack-print %{
  info -title "tag stack" -- %sh{
    eval "set -- $kak_quoted_opt_tagstack"
    count=$((($# / 4)))
    while ["$1"]; do
      printf "%2d\t%-30s\t%s:%s:%s\n" $count "$4" "$1" "$2" "$3"
      count=$((count - 1))
      shift 4
    done
  }
}

# suggested mapping for those, who do not use the t command
# map global normal t "<a-i>w: tagstack-push <c-r>.<ret>"
# map global normal T ": tagstack-pop<ret>"
# map global normal <a-t> ": tagstack-print<ret>"

```

It actually doesn’t do very much except for saving the position from where i jumped to a tag. It is kind of like a second jump list. But this covers about 99% of my use cases for the Vim tag stack. You can change the `tagstack_cmd` to any command you want, that takes an argument and does a jump.

---

<div class="post-metadata">

**Author:** ![robertmeta](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/robertmeta/32/833_2.png) [@robertmeta](https://discuss.kakoune.com/u/robertmeta)\
**Post date:** [September 8, 2020, 12:11pm UTC](https://discuss.kakoune.com/t/tag-stack/1254/2 "2020-09-08T12:11:52Z")

</div>

Awesome little commands, I would caution people on binding over “t” as that is a fairly heavy used command for many of us.

---

<div class="post-metadata">

**Author:** ![listentolist](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/listentolist/32/416_2.png) [@listentolist](https://discuss.kakoune.com/u/listentolist)\
**Post date:** [September 8, 2020, 4:55pm UTC](https://discuss.kakoune.com/t/tag-stack/1254/3 "2020-09-08T16:55:23Z")

</div>

Yes, thank you! I updated the comment about it. I used `t` in Vim very much, but in Kakoune it is not as useful because `<a-.>` does not jump to the next match and you cannot jump back. And in most cases `<a-]>` does exactly the same thing that I used `t` for in Vim.
