# Piped command information on file started from

**URL:** <https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141>\
**Category:** Help\
**Created:** [October 14, 2022, 1:17pm UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141 "2022-10-14T13:17:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthon](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@anthon](https://discuss.kakoune.com/u/anthon)\
**Post date:** [October 14, 2022, 1:17pm UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/1 "2022-10-14T13:17:40Z")

</div>

I was trying to destinguish the behaviour of a command run on a selection (using `|snip`) based on the type of file the selection was made on. I checked the environment variables availabe to the `snip` program, but none seem to be set by kakoune. Is it possible to at least pass on the filename, either as environment variable.

An extra commandline option (like `--file abc.ext`) added to the invocation of `snip` would work as well, but I would not like to have that added to every command invocation.

---

<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:** [October 14, 2022, 2:04pm UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/2 "2022-10-14T14:04:56Z")

</div>

See [`:doc expansions shell-expansions`](https://github.com/mawww/kakoune/blob/master/doc/pages/expansions.asciidoc#shell-expansions) for all the environment variables that Kakoune can set for external commands - although by default it doesn’t set any unless it sees them mentioned in the command line.

I’m not familiar with this `snip` program or what environment variables it might consult.

---

<div class="post-metadata">

**Author:** ![anthon](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@anthon](https://discuss.kakoune.com/u/anthon)\
**Post date:** [October 14, 2022, 2:44pm UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/3 "2022-10-14T14:44:08Z")

</div>

snip is a in-house program that finds a code snippet based on the argument you give it. I can modify it to look at any environment variable.

If I knew the filename or the full path based on **any** environment variable I could select e.g. Python only snipppets when the filename ends in `.py`.

I currently dump all environment variables that `snip` receives to a log file, and I don’t see any difference when adding `# kak_session` on the commandline as suggested in the doc you refer to.

---

<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:** [October 15, 2022, 2:29am UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/4 "2022-10-15T02:29:39Z")

</div>

If I launch Kakoune with `kak -n` (to prevent it from loading any configuration or the standard library), and then press:

```
%|env # kak_session

```

…and hit Enter, then I get a list of environment variables, and `$kak_session` is in the list. Does that not work for you? What version of Kakoune are you using?

---

<div class="post-metadata">

**Author:** ![anthon](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@anthon](https://discuss.kakoune.com/u/anthon)\
**Post date:** [October 15, 2022, 7:54am UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/5 "2022-10-15T07:54:28Z")

</div>

Yes this works. Thanks for the pointers, that helped me progress. I originally tried the full echo … example and that doesn’t work in |.  
There was another problem with my `snip` command getting the comment `# kakoune` and not working properly because of that.  
And I realise I might have typed fgrep out of habit (instead of plain grep) and then `^kak_` will of course not match. I should switch to the recommended `grep -F` instead of using fgrep at all, but 40 years of musclememory is not easily erased.

I am going to look into how to use `define-command` to create a snip command that invokes the external utiltiy and has some env. variable names in its body.  
Thanks again

---

<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:** [October 15, 2022, 9:13pm UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/6 "2022-10-15T21:13:41Z")

</div>

`rc/tools/format.kak` in the Kakoune repo uses this pattern:

```auto
define-command snip %{
	evaluate-commands -save-regs | %{
		set-register '|' %{
			# kak_buffile
			# kak_opt_filetype
			snip
		}
		execute-keys -draft |<ret>
	}
}

```

---

<div class="post-metadata">

**Author:** ![anthon](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@anthon](https://discuss.kakoune.com/u/anthon)\
**Post date:** [October 16, 2022, 8:40am UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/7 "2022-10-16T08:40:06Z")

</div>

@krobelus That looked promising, and when I added `-params 2`, executing `:snip` would require two parameters. But I couldn’t get these parameters passed on to the external program:

```auto
define-command snip -params 2 %{
    evaluate-commands -save-regs | %{
        set-register '|' %{
            # kak_buffile
            # kak_opt_filetype
            snip $1 $2
        }
        execute-keys -draft |<ret>
    }
}

```

I tried quoting ( `"$1"` ) inserting curly braces (`${1}`), which didn’t get me the parameters in the program, and quoting the whole (`"snip $1 $2"), resulted in command not found (as seen in `_debug_`.

I checked out the source for kakoune to see if there were slightly more complicated `set-register '|'` examples that also set parameters but couldn’t find any so far.

The filename and file type do get passed on nicely.

---

<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:** [October 16, 2022, 11:04am UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/8 "2022-10-16T11:04:38Z")

</div>

> [@anthon](#):
>
> ```auto
> evaluate-commands -save-regs | %{
> set-register '|' %{
> # kak_buffile
> # kak_opt_filetype
> snip $1 $2
> }
> 
> ```

That’s an [open issue](https://github.com/mawww/kakoune/issues/4723) that only comes up seldomly. Not sure if we should change it.  
A workaround is to use an option or a register as intermediary:

```kak
evaluate-commands -save-regs |a %{
  set-register a %arg{1}
  set-register | %{
      snip "$kak_reg_a"
  }
  [...]
}

```

You probably also want to define completions for your command, using something like

```
complete-command -menu snip shell-script-candidates %{
set -- $(snip --list-possible-arguments)
    printf '%s\n' "$@"
}

```

For even tighter integration, there are snippet plugins (like kak-lsp) that expose snippets as autocompletions. When a completion is accepted, the snippet is expanded. That’s a [bit more involved](https://discuss.kakoune.com/t/intro-to-kakoune-completers/1198) but probably still well simpler than in other editors.

---

<div class="post-metadata">

**Author:** ![anthon](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@anthon](https://discuss.kakoune.com/u/anthon)\
**Post date:** [October 16, 2022, 5:18pm UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/9 "2022-10-16T17:18:37Z")

</div>

The `set-register` trick worked. I was not sure if I needed to set a different register for each argument or could combine the two, but the latter worked ( `set-register %arg{1} %arg{2}` ).

The completion would be nice, the utility already has a `--list` option to list such possible completions. But I haven’t got that work yet.

```auto
/Users/anthon/.config/kak/kakrc:76:1: 'complete-command': no such command
/opt/homebrew/Cellar/kakoune/2021.11.08/share/kak/kakrc:29:1: 'evaluate-commands': 131:1: 'source': 76:1: 'complete-command': no such command
error while parsing kakrc:
    1:1: 'source': 29:1: 'evaluate-commands': 131:1: 'source': 76:1: 'complete-command': no such command

```

---

<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:** [October 16, 2022, 5:45pm UTC](https://discuss.kakoune.com/t/piped-command-information-on-file-started-from/2141/10 "2022-10-16T17:45:00Z")

</div>

> [@anthon](#):
>
> but the latter worked ( `set-register %arg{1} %arg{2}` ).

(note that instead of `set-register a %arg{1} %arg{2}` you can write `set-register a %arg{@}`)  
FYI there’s a hidden gotcha with passing both arguments in a single register. If one of them contains a space, you need special quoting to break them apart again (POSIX sh doesn’t really have arrays except “$@”). Here’s the customary pattern:

```kak
set-register a arg 'arg with spaces'
execute-keys %{!eval set -- "$kak_quoted_reg_a"; printf '[%s]\n' "$@"<ret>}

```

inserts

```auto
[arg]
[arg with spaces]

```

> ‘complete-command’: no such command

sorry this is not yet in the released version. I think mawww wants to tag a release soon.  
You can use the equivalent `define-command -shell-script-candidates` switch to define shell completions (or you can update to latest git master, also a good choice).
