# How to execute shell commands with the current file name?

**URL:** <https://discuss.kakoune.com/t/how-to-execute-shell-commands-with-the-current-file-name/156>\
**Category:** Help\
**Created:** [November 4, 2018, 9:12am UTC](https://discuss.kakoune.com/t/how-to-execute-shell-commands-with-the-current-file-name/156 "2018-11-04T09:12:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SegFault](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/segfault/32/1478_2.png) [@SegFault](https://discuss.kakoune.com/u/SegFault)\
**Post date:** [November 4, 2018, 9:12am UTC](https://discuss.kakoune.com/t/how-to-execute-shell-commands-with-the-current-file-name/156/1 "2018-11-04T09:12:32Z")

</div>

I have been using Vim for 2 years now. I am now switching to Kakoune. When I work with my latex files, I have a my Ctrl+Space key mapped to a sh command that will compile the latex file, (like this line in .vimrc);

```
autocmd FileType tex nnoremap <C-@> :w<Enter>:!xelatex -shell-escape -mode=nonstopmode %<Enter>

```

or sometimes when I am experimenting or quick prototyping, I compile and run my C codes, via this line in .vimrc

```
autocmd FileType c nnoremap <C-@> :w<Enter>:!clang -o run % && ./run<Enter>
autocmd FileType java nnoremap <C-@> :w<Enter>:!javac % && java %:r<Enter>

```

What are their kakrc equivalent? I am very very new to Kakoune and I have no idea what Kakoune specific things like hooks, highlighters, faces etc are. Someone please give me the codes that I can directly yank-paste in my kakrc and get working. And get into docs eventually as I get comfterble.

Also, if you don’t know what .vimrc line meant,

> `autocmd FileType java` if the FileType is java, `nnoremap` non-recursively map, `<C-@>` Ctrl+Space to `:w<Enter>` save the file, and execute `:!javac % && java %:r<Enter>` where `%` is the currently open filename and `%:r` is the filename without the extensions.

---

<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:** [November 4, 2018, 11:15am UTC](https://discuss.kakoune.com/t/how-to-execute-shell-commands-with-the-current-file-name/156/2 "2018-11-04T11:15:28Z")

</div>

A Kakoune “hook” is like a Vim “autocmd”: when a particular event happens with a particular parameter, a particular set of commands get run.

In particular, `autocmd FileType blah somecommand` is like:

```
hook global WinSetOption filetype=blah "somecommand"

```

The “global” there is the “scope” of the hook. “global” means the hook is present for every buffer, you could also say “buffer” (the hook is present for a single buffer, for every window it’s displayed in) or “window” (the hook is present for a single buffer in a single window).

Instead of having “map”, “nmap”, “imap”, etc. Kakoune’s “map” command takes a mode argument; instead of having “map” and “noremap” _all_ of Kakoune’s maps behave like Vim’s “noremap”. So the translation of “nnoremap key keys” is:

```
map global normal key keys

```

The translation of the commands to run will be something like:

```
: w<ret>: nop %sh{ javac %val{buffile} && java "$(basename "$kak_buffile" .java)" }<ret>

```

To explain:

- `:w` is just like Vim, of course, but note the space between `:` and `w`, which prevents this command from polluting your prompt history (when you press `<up>` at the prompt).
- Kakoune doesn’t have a `!` command to directly execute a shell command, but the `%sh{}` construct expands to the standard output of running a command (like `$()` in shell), and the `nop` command ignores all its arguments.
- As you can see, Kakoune doesn’t use `%` to mean “the current buffer file” but the `%val{}` syntax which expands to various useful bits of Kakoune state. The same information is available in shell expansions via the `$kak_*` environment variables. See [:help expansions](https://github.com/mawww/kakoune/blob/master/doc/pages/expansions.asciidoc) for more.

So the snippet to paste into your `kakrc` would be something like:

```
hook global WinSetOption filetype=java %{
    map window normal <c-@> %{: w<ret>: nop %sh{ javac %val{buffile} && java "$(basename "$kak_buffile" .java)" }<ret>}
}

```

…except I can’t actually trigger a mapping with `<c-@>`, or `<c-space>`, or `<c-^>` or anything like that. I can type `<c-^>` on the command line with the `<c-v>` prefix (just like Vim) and I can type it in the document, but I can’t make `<c-space>` do anything at all. Quite possibly nobody’s tried to map it before; I can’t even find an issue for it. I guess I’ll file one.

**EDIT:** Filed [#2553](https://github.com/mawww/kakoune/issues/2553).

---

<div class="post-metadata">

**Author:** ![SegFault](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/segfault/32/1478_2.png) [@SegFault](https://discuss.kakoune.com/u/SegFault)\
**Post date:** [November 4, 2018, 2:38pm UTC](https://discuss.kakoune.com/t/how-to-execute-shell-commands-with-the-current-file-name/156/3 "2018-11-04T14:38:55Z")

</div>

```auto
hook global WinSetOption filetype=tex %{
        map window normal <c-l> %{: w<ret>: nop %sh{xelatex -interaction batchmode -shell-escape %val{buffile} } }
 }

```

Following what you said, I changed my kakrc to add the above lines and noting happened. Note just to experiment, I decided to use `<c-l>` instead of `<c-a>`.

---

<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:** [November 5, 2018, 2:58am UTC](https://discuss.kakoune.com/t/how-to-execute-shell-commands-with-the-current-file-name/156/4 "2018-11-05T02:58:32Z")

</div>

For better or for worse, the filetype for `*.tex` files is `latex`, not `tex`. Try using the pattern `filetype=latex`.

---

<div class="post-metadata">

**Author:** ![nonumeros](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/n/90ced4/32.png) [@nonumeros](https://discuss.kakoune.com/u/nonumeros)\
**Post date:** [February 7, 2019, 10:02am UTC](https://discuss.kakoune.com/t/how-to-execute-shell-commands-with-the-current-file-name/156/5 "2019-02-07T10:02:56Z")

</div>

This can be easily accomplished by having the following on the init file

```
 map global normal <c-x> ':w<ret> $ latex $kak_buffile' 

```

`c-x` is available and If other options from latex are needed then customize
