From 8a682e3a89af8f1a1fa6153eb47e0551315f63e3 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 7 Nov 2024 09:43:46 -0600 Subject: [PATCH] Add Open New Splits To The Current Directory as a tmux TIL --- README.md | 3 +- ...pen-new-splits-to-the-current-directory.md | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tmux/open-new-splits-to-the-current-directory.md diff --git a/README.md b/README.md index 52dbe8b..b77fa53 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1499 TILs and counting..._ +_1500 TILs and counting..._ --- @@ -1354,6 +1354,7 @@ _1499 TILs and counting..._ - [Kill The Current Session](tmux/kill-the-current-session.md) - [List All Key Bindings](tmux/list-all-key-bindings.md) - [List Sessions](tmux/list-sessions.md) +- [Open New Splits To The Current Directory](tmux/open-new-splits-to-the-current-directory.md) - [Open New Window With A Specific Directory](tmux/open-new-window-with-a-specific-directory.md) - [Organizing Windows](tmux/organizing-windows.md) - [Paging Up And Down](tmux/paging-up-and-down.md) diff --git a/tmux/open-new-splits-to-the-current-directory.md b/tmux/open-new-splits-to-the-current-directory.md new file mode 100644 index 0000000..8411712 --- /dev/null +++ b/tmux/open-new-splits-to-the-current-directory.md @@ -0,0 +1,37 @@ +# Open New Splits To The Current Directory + +I typically work in one project per tmux session. When I create a given tmux +session, the default directory is for that project. All new windows and pane +splits will open at that default directory. This generally is the default +behavior I want. + +One caveat: I often open a new window within an existing session that I want +anchored to another directory. This could be because I'm working in a monorepo +and I need to work from a subdirectory for a specific package or app. Or it +could be that I'm temporarily digging into another project and it isn't worth +create a whole new session. + +Regardless of the reason, I run into a bit of friction with tmux's defaults. + +First I open the new window and `cd` to another project. After some working, I +need to open a split pane, maybe to run a project command like a build or dev +server. Hitting `prefix-"` (horizontal split) or `prefix-%` (vertical split) +opens a pane with the shell defaulting back to the original directory, not the +current directory. + +The trick to fixing this bit of friction is overriding the directory of pane +splits. I can do that by adding the following to my `~/.tmux.conf`: + +``` +# Pane splits should open to the same path as the current pane +bind '"' split-window -v -c "#{pane_current_path}" +bind % split-window -h -c "#{pane_current_path}" +``` + +Make sure to run `tmux source-file ~/.tmux.conf` to apply these config changes. + +The `pane_current_path` is called a "Format" in tmux parlance. It resolves to +the absolute path of the current pane's current directory. You can find all the +formats in the manpage with this command: `man tmux | less +'/^FORMATS'`. You +can also show yourself that this format resolves to what you expect by running +`tmux display-message -p '#{pane_current_path}'`.