# How can I pass the project root variable from kak-lsp into a shell command

**URL:** <https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706>\
**Category:** Help\
**Tags:** lsp\
**Created:** [June 20, 2025, 5:11pm UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706 "2025-06-20T17:11:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![IronKing127](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ironking127/32/1558_2.png) [@IronKing127](https://discuss.kakoune.com/u/IronKing127)\
**Post date:** [June 20, 2025, 5:11pm UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/1 "2025-06-20T17:11:42Z")

</div>

Hello I wanted to make it easier to open files in the current project. Right now I am using a one program one job Unix philosophy workflow using Hyprland as the tiling system, ranger as an in terminal file explorer, and obviously Kakoune and Kak-Lsp as the text editor. I wanted to make opening files in new windows of the same projects easier since I have to do it so much and figured the best way would be to open a new window with ranger opened to the root directory of the project. It is pretty easy to do this with the command: `hyrpctl dispatch exec kitty ranger ${insert filepath to project root here}` The only thing that I cannot figure out is how to get the project root of the current buffer with an lsp open from kak-lsp. Since kak-lsp is added into the configuration through `eval %sh{kak-lsp}` I tried to search the output of the kak-lsp command to see what it was doing and I did find a `declare-option -hidden str lsp_project_root` which I throught is what I need but when I opened a cpp file where the lsp was working and ran `debug options` to see if it was working I only got `* lsp_project_root: ''` where the project root doesn’t seem to be set at all despite the lsp clearly working and finding all includes. So my question is simply what is the proper way to get the project root from kak-lsp so I can pass it into a command called by a keybinding. In case the set up of the project is important most my projects look exactly like this:

- “Project name” directory
  - “.git” directory
  - “debug” CMake debug build directory
  - “release” CMake release build directory
  - “include” include directory
  - “src” source directory
  - “CMakeLists.txt” only cmake file in the project
  - “compile\_commands.json” A symbolic link to the compile commands that CMake generates in the debug build directory.

---

<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:** [June 21, 2025, 11:13am UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/2 "2025-06-21T11:13:30Z")

</div>

For finding git project root I use this shell expansion:

```sh
%sh{git rev-parse --show-toplevel}

```

Personally I don’t use LSP but it should be able to provide you the project root.  
Though as far as I recall, it depends on both the LSP server and LSP client to support the workspace feature.

> **[Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_symbol)**
>
> This document describes the 3.17.x version of the language server protocol. An implementation for node of the 3.17.x version of the protocol can be found here.

---

<div class="post-metadata">

**Author:** ![IronKing127](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ironking127/32/1558_2.png) [@IronKing127](https://discuss.kakoune.com/u/IronKing127)\
**Post date:** [June 25, 2025, 5:36pm UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/3 "2025-06-25T17:36:09Z")

</div>

Hello thank you for your response what I want is this exact behavior (to walk up the parent directory and return the first match but for all of the entrys in the lsp servers “root\_globs” variables so that it would work for projects which I don’t have git(which I have a lot of) I tried to make a script to find it (which is why I took so long to respond) but can’t figure out how to walk up the file directory without cding. And if anyone does know the lsp, I found a lsp\_send command but I don’t know how to get information back from it and it is not documented on the kak-lsp github. Thank you for your time.

---

<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:** [June 29, 2025, 8:09am UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/4 "2025-06-29T08:09:10Z")

</div>

Yeah this is indeed not currently exposed by LSP.

It would be fairly easy and probably reasonable to have kak-lsp set the `lsp_project_root` option for all LSP-enabled buffers… though a naive implementation would leave it intermittently stale.

(In theory we could also expose this as `kak-lsp --project-root $kak_quoted_opt_lsp_servers $kak_quoted_buffile`, so it’s “usable” outside the editor but that seems ugly and out-of-place.  
kak-lsp is not meant to be called from outside Kakoune, so the preferred mode of interaction is via Kakoune commands/options.)

For now, if you want to use the exact LSP root-globs semantics (as opposed to `jj workspace root || git rev-parse --show-toplevel`),  
I recommend copying the logic that kakoune-lsp uses. Try this:

```shell
find_root() {
    project_root=$(dirname "$kak_buffile")
    if ["$project_root" = .]; then
        exit # Ignore scratch buffers.
    fi
    until cd "$project_root" 2>/dev/null; do
        project_root=${project_root%/*}
        project_root=/${project_root#/}
    done
    for glob; do
        until ["$(pwd -P)" = /]; do
            set -- $glob
            if [-e "$1"]; then
                break 2
            fi
            cd ..
        done
        cd "$project_root"
    done
    printf %s "$PWD"
}
# The subshell means that the working-directory changes won't affect the parent shell
root=$( find_root CMakeLists.txt 'compile_commands.*' .git )
do-something-with "$root"

```

---

<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:** [June 29, 2025, 8:14am UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/5 "2025-06-29T08:14:30Z")

</div>

I’ve moved away from ranger (they still didn’t merge the fix preventing glitches on focus notification sequences, which are requested by Kakoune). Instead I mostly use this:

```auto
define-command -override my-find-edit -params 1 %{edit -- %arg{@}}
complete-command -menu my-find-edit shell-script-candidates %{ find -type f | sed s,^./,, }
define-command -override my-git-edit -params 1 %{
	edit -existing -- %arg{@}
}
complete-command -menu my-git-edit shell-script-candidates %{ git ls-files || jj --ignore-working-copy file list }
define-command -override my-git-edit-root -params 1 %{
	edit -existing -- %arg{@}
}
complete-command -menu my-git-edit-root shell-script-candidates %{ git ls-files "$(project-root)" || (cd "$(project-root)" && jj --ignore-working-copy file list) }

```

---

<div class="post-metadata">

**Author:** ![IronKing127](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ironking127/32/1558_2.png) [@IronKing127](https://discuss.kakoune.com/u/IronKing127)\
**Post date:** [July 11, 2025, 12:55pm UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/6 "2025-07-11T12:55:43Z")

</div>

Thank you I finally got it to work the end result looks like this:

```auto
define-command openProjectRootTab %{eval %sh{
		find_root() {
		    project_root=$(dirname "$kak_buffile")
		    if ["$project_root" = .]; then
		        exit # Ignore scratch buffers.
		    fi
		    until cd "$project_root" 2>/dev/null; do
		        project_root=${project_root%/*}
		        project_root=/${project_root#/}
		    done
		    for glob; do
		        until ["$(pwd -P)" = /]; do
		            set -- $glob
		            if [-e "$1"]; then
		                break 2
		            fi
		            cd ..
		        done
		        cd "$project_root"
		    done
		    printf %s "$PWD"
		}
		    hyprctl dispatch exec kitty ranger $(find_root CMakeLists.txt 'compile_commands.*' .git)
	}
}
map global open t ':openProjectRootTab<ret>'-docstring 'Open project in new tab of hyprland'

```

May I ask what a focus notification sequence is and what kind of glitches it causes with ranger. I looked up “kakoune focus notification sequence” and didn’t see anything that explains it, Thank you. Sorry for the long response time my schedule is quite hectic nowadays.

---

<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:** [July 11, 2025, 2:27pm UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/7 "2025-07-11T14:27:13Z")

</div>

[Disable terminal focus reporting by krobelus · Pull Request #2964 · ranger/ranger · GitHub](https://github.com/ranger/ranger/pull/2964) though I can’t reproduce the problem right now. Maybe it’s fixed, should be easy to verify, let me see…

---

<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:** [July 11, 2025, 10:03pm UTC](https://discuss.kakoune.com/t/how-can-i-pass-the-project-root-variable-from-kak-lsp-into-a-shell-command/2706/8 "2025-07-11T22:03:34Z")

</div>

confirming the ranger issue has been fixed, my bad

the nice thing about this project-root-finding script is that it only needs POSIX shell, it doesn’t depend on anything in kakoune
