3

In a VIM script file, you have access to <sfile> as demonstrated in this question and answers.. A few examples from this answer:

" Absolute path of script file:
let s:path = expand('<sfile>:p')

" Absolute path of script file with symbolic links resolved:
let s:path = resolve(expand('<sfile>:p'))

" Folder in which script resides: (not safe for symlinks)
let s:path = expand('<sfile>:p:h')

TMUX supports the ability to source additional using source-file (see this StackOverflow question and answer regarding for splitting TMUX configuration files).

Now, to my queston. Let's say in my example in my ~/.tmux.conf I have a line like below

source-file ~/some-workspace-dir/my-tmux.conf

I'd like to be able to refer to files relative to my-tmux.conf inside of my-tmux.conf.

If I add a run-shell "pwd", it just points out the home directory at ~/.tmux.confrather than~/some-workspace-dir, the directory containing my-tmux.conf`.

My question is how do you get the current path of a file being sourced by TMUX source-file (without parsing the sourced file)?

mkobit
  • 43,979
  • 12
  • 156
  • 150

2 Answers2

2

I'd suggest translating the file you're calling from a tmux config to a shell script, and calling it via run-shell instead of source-file - that way you can use e.g. dirname/basename to derive the path of the sourced file.

Converting a tmux config to a shell script is usually as easy as prefixing each command with the path to tmux, i.e. starting with hello_world.tmux:

command-prompt -p "enter a name: " "display-message 'hello %1!'"

...this becomes hello_world.sh:

#!/bin/bash
/usr/local/bin/tmux command-prompt -p "enter a name: " "display-message 'hello %1!'"

Here is a script that demonstrates some introspection in action:

#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASENAME=$(basename "$0")
tmux split-window -v
sleep 1
tmux send-keys -t :.1 "cat $CURRENT_DIR/$BASENAME"

If you run this shell script from inside tmux, it will determine its own path on the filesystem, then create a new pane inside tmux, and sends a cat command to the shell running inside that pane, printing the shell script's own source code.

Here is an Asciinema screencast, demoing the script in action: https://asciinema.org/a/418522

atomicstack
  • 729
  • 4
  • 8
0

The correct answer seems to have changed since this was first answered. tmux 3.2 added an option which trivializes the issue ^1 ^2. In the file config file which you want to locate (i.e.: ~/.tmux.conf or ~/.config/tmux/tmux.conf):

run-shell "#{d:current_file}/path/to/plugin.tmux"
#            | ^ crux of the issue
#            ^ dirname

The man page for tmux indicates the command given to run-shell is formatted using tmux's templating syntax ^3. The FORMATS section indicates there is a variable named current_file which may be used to locate the currently executing configuration file. Additionally, the prefix d: can be used to get the dirname of that file. Hence, #{d:current_file} will get the configuration directory.