1

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

" 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 with source-file (another StackOverflow question and answer regarding how to split files up). Say for 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 overall question is how do you get the current path of a file being sourced by TMUX source-file (preferably without parsing that file)?

mkobit
  • 39,564
  • 9
  • 145
  • 144

1 Answers1

0

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
  • 609
  • 4
  • 8