0

I have these automcmds that work nicely for renaming tmux windows with the current filename (from here):

autocmd BufEnter * call system("tmux rename-window " . expand("%:t"))
autocmd VimLeave * call system("tmux rename-window bash")

I also want to add an additional lines to also change the colour of the window name, with something like this:

tmux set -a window-status-current-style "fg=#{@vimfile}"

How do I add the additional tmux command?

2

1 Answer 1

2

In general, it's not a problem to have multiple autocmd for the same event. Moreover, they are guaranteed to execute in the very same order. So you can write it this way:

augroup tmux | au!
autocmd BufEnter * call system("tmux rename-window " . expand("%:t"))
autocmd BufEnter * !tmux set -a window-status-current-style "fg=\#{@vimfile}"
augroup end
  • the first command has to use expand() (and so system()) not to fail on empty file/buffer name;
  • the second command has an extra backslash \# because of the Vim's command-line parser;

Note however that in this case you spawn tmux process twice (as well as shell process) per each BufEnter which is suboptimal.

Instead of this, you can use a semicolon to pass multiple tmux commands per a single invocation. It's also possible to take care of an empty file name like this:

augroup tmux | au!
autocmd BufEnter * call system(printf('tmux rename-window %s\;
    \ set -a window-status-current-style "fg=#{@vimfile}"',
    \ empty(@%) ? 'Noname' : fnamemodify(@%, ':t')))
augroup end

(Yet another backslash \; is needed for the shell's parser).

3
  • Thanks, but this isn't working and I get some sort of message on startup saying :!tmux rename-window init.vim\; set -a window-status-current-style "fg=#{@vimfile}" Press ENTER or type command to continue I am using neovim v0.5.1 if that makes any difference here.
    – paradroid
    Oct 30, 2021 at 12:06
  • I also get this error message when starting with no file: Error detected while processing BufEnter Autocommands for "*": E499: Empty file name for '%' or '#', only works with ":p:h": !tmux rename-window %:t\; set -a window-status-current-style "fg=\#{@vimfile}"
    – paradroid
    Oct 30, 2021 at 12:09
  • @paradroid Indeed. File name modifiers do not like empty names when used on the cmdline (not as an expression). I revised the answer.
    – Matt
    Oct 30, 2021 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.