commit 968d51928adf5f31845e5d77844ee5254fb1d5cf
parent b8d84ca9d57a0c036feb71f05edd7e2bf198bebb
Author: Paco Esteban <paco@e1e0.net>
Date: Sat, 14 Mar 2020 19:44:12 +0100
new article about tmus splitw
Diffstat:
3 files changed, 68 insertions(+), 0 deletions(-)
diff --git a/src/gophermap b/src/gophermap
@@ -12,6 +12,7 @@ Sometimes I write things so I do not forget ...
o--o-- Random (usually tech) stuff
+0Split window on current working directory on tmux /tmux-splitw-on-current-folder.md.txt e1e0.net 70
0upsc (NUT) Prometheus exporter /upsc-prometheus-exporter.md.txt e1e0.net 70
0Notes on Vim register /vim-registers.md.txt e1e0.net 70
0Encrypt a USB flash drive on OpenBSD. /openbsd-encrypt-usb-flash-drive.md.txt e1e0.net 70
diff --git a/src/index.html b/src/index.html
@@ -1,4 +1,5 @@
<ul>
+<li><a href="/tmux-splitw-on-current-folder.html" title="2020-03-14">Split window on current working directory on tmux</a></li>
<li><a href="/upsc-prometheus-exporter.html" title="2020-01-17">upsc (NUT) Prometheus exporter</a></li>
<li><a href="/vim-registers.html" title="2019-12-06">Notes on Vim registers</a></li>
<li><a href="/openbsd-encrypt-usb-flash-drive.html" title="2019-08-24">Encrypt a USB flash drive on OpenBSD.</a></li>
diff --git a/src/tmux-splitw-on-current-folder.md b/src/tmux-splitw-on-current-folder.md
@@ -0,0 +1,66 @@
+# Split window on current working directory on tmux
+2020-03-14
+
+In 99% of the occasions that I use `split-window` on `tmux(1)` I want the
+working directory to be the same as the pane I was in.
+
+If you use Linux, FreeBSD, MacOSX (and maybe others), this can be achieved
+really easily with:
+
+```
+tmux split-window -c '#{pane_current_path}'
+```
+
+You can use `-v` or `-h` as you need and bind it to a key so the access is
+easy.
+
+Problem is that `#{pane_current_path}` is not present on the tmux that ships in
+base for OpenBSD (the OS I use for everything on a daily basis).
+This seems to be a deliberate choice, so one has to work around it if one needs
+this feature.
+
+While talking with the pros a bit about it, they came up with a solution for
+this that covers all my needs.
+
+It involves calling an external program to get the working directory given the
+PID. One can do this with `ps(1)`, but the output is limited to 40 characters,
+which is less than ideal. That's why Stuart Henderson sent
+[a small program that does the job][1].
+
+Then we need a shell script that executes the `getcwd` program and composes the
+tmux command to be executed. It can be as simple as:
+
+```
+#!/bin/sh
+
+[ -z "${TMUX}" -o $# -lt 1 ] && exit 0
+
+panel_cwd=$(getcwd ${1})
+shift
+
+exec /usr/bin/tmux split-window -c "${panel_cwd}" "$@"
+```
+
+And finally a couple of bindings to complete the setup:
+
+```
+bind | run-shell -b "~/bin/my-tmux-splitw #{?pane_active,#{pane_pid},} -h"
+bind - run-shell -b "~/bin/my-tmux-splitw #{?pane_active,#{pane_pid},} -v"
+```
+
+Those are based on an idea from Sebastien Marie. The bindings are a mnemonic
+I use since a long time ago. Bar resembles vertical split and the dash
+horizontal.
+
+The magic here comes from the format passed to the script (which is effectively
+the PID of the active pane).
+
+This construction is like a ternary operator. If the variable (`pane_active`
+in this case) exists and is not zero, the first alternative (after the first
+comma) will be taken. If not, it will be the second one (empty in this case).
+Check `tmux(1)`, in particular the `FORMATS` section for more info.
+
+_Have any comments ? Send an email to the [comments address][999]._
+
+[1]: https://git.e1e0.net/utils/file/getcwd/getcwd.c.html
+[999]: mailto:comments@e1e0.net?Subject=Split%20window%20on%20current%20working%20directory%20on%20tmux