e1e0.net

sources for e1e0 website
git clone https://git.e1e0.net/e1e0.net.git
Log | Files | Refs

tmux-splitw-on-current-folder.md (2562B)


      1 Title: Split window on current working directory on tmux
      2 Author: paco
      3 Date: 2020-03-14
      4 Type: article
      5 
      6 _Update 2020-06-13: [@pertho][2] (thanks !!) points out that
      7 `#{pane_current_path}` is back on tmux for OpenBSD, so there's no need for all
      8 this.  My key bindings now are:_
      9 
     10 ```
     11 bind | split-window -c '#{pane_current_path}' -h
     12 bind - split-window -c '#{pane_current_path}' -v
     13 ```
     14 
     15 _This is outdated, then.  I leave it here for archive purposes_
     16 
     17 In 99% of the occasions that I use `split-window` on `tmux(1)` I want the
     18 working directory to be the same as the pane I was in.
     19 
     20 If you use Linux, FreeBSD, MacOSX (and maybe others), this can be achieved
     21 really easily with:
     22 
     23 ```
     24 tmux split-window -c '#{pane_current_path}'
     25 ```
     26 
     27 You can use `-v` or `-h` as you need and bind it to a key so the access is
     28 easy.
     29 
     30 Problem is that `#{pane_current_path}` is not present on the tmux that ships in
     31 base for OpenBSD (the OS I use for everything on a daily basis).
     32 This seems to be a deliberate choice, so one has to work around it if one needs
     33 this feature.
     34 
     35 While talking with the pros a bit about it, they came up with a solution for
     36 this that covers all my needs.
     37 
     38 It involves calling an external program to get the working directory given the
     39 PID.  One can do this with `ps(1)`, but the output is limited to 40 characters,
     40 which is less than ideal.  That's why Stuart Henderson sent
     41 [a small program that does the job][1].
     42 
     43 Then we need a shell script that executes the `getcwd` program and composes the
     44 tmux command to be executed.  It can be as simple as:
     45 
     46 ```
     47 #!/bin/sh
     48 
     49 [ -z "${TMUX}" -o $# -lt 1 ] && exit 0
     50 
     51 panel_cwd=$(getcwd ${1})
     52 shift
     53 
     54 exec /usr/bin/tmux split-window -c "${panel_cwd}" "$@"
     55 ```
     56 
     57 And finally a couple of bindings to complete the setup:
     58 
     59 ```
     60 bind | run-shell -b "~/bin/my-tmux-splitw #{?pane_active,#{pane_pid},} -h"
     61 bind - run-shell -b "~/bin/my-tmux-splitw #{?pane_active,#{pane_pid},} -v"
     62 ```
     63 
     64 Those are based on an idea from Sebastien Marie.  The bindings are a mnemonic
     65 I use since a long time ago.  Bar resembles vertical split and the dash
     66 horizontal.
     67 
     68 The magic here comes from the format passed to the script (which is effectively
     69 the PID of the active pane).
     70 
     71 This construction is like a ternary operator.  If the variable (`pane_active`
     72 in this case) exists and is not zero, the first alternative (after the first
     73 comma) will be taken.  If not, it will be the second one (empty in this case).
     74 Check `tmux(1)`, in particular the `FORMATS` section for more info.
     75 
     76 [1]: https://github.com/sthen/getcwd
     77 [2]: https://bsd.network/@pertho