Skip to content

Commit

Permalink
feat(tmux): add binding for new-window with navigation
Browse files Browse the repository at this point in the history
For the longest time, I've hade the following binding in my `tmux.conf`:

```tmux
bind c new-window -c "#{pane_current_path}" \; \
  command-prompt -p " Window name:" -I "#{window_name}" "rename-window '%%'"
```

A new window is created, using the same path as my current pane, and I'm
presented with a prompt to give the window a meaningful name, with the default
being 'zsh'. This is great when I need another window for the project I'm
working on, but falls a little short when I want to navigate to a new project.

I frequently use `z` with `fzf` to navigate to
project directories. When opening a window for a separate project, I end up
going through these steps:

- Hit `prefix + c`
- Type the name of the project (to name the window)
- Type `z <project name>` to navigate to the project directory

I want to eliminate the need to type the project name twice and came up with
this mapping:

```tmux
bind z new-window \; command-prompt -p \
  " Window name:" "rename-window '#{?%1,%1,zsh}' \; send-keys z ' %1' C-m"
```

Here, we're reading the project name with `command-prompt`, using the input as
the window name (unless it's blank, in which case we use a format conditional
to substitute 'zsh' as a default name). The input is also used in a send-keys
command to execute `z` when the shell loads, navigating to the project
directory.
  • Loading branch information
eliasnorrby committed Apr 22, 2021
1 parent dee3d83 commit b93fb24
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions shell/tmux/tmux.conf
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ set -g renumber-windows on # renumber windows when a window is closed
# Prompt to rename window right after it's created
# set-hook -g after-new-window 'command-prompt -I "#{window_name}" "rename-window '%%'"'
bind c new-window -c "#{pane_current_path}" \; command-prompt -p " Window name:" -I "#{window_name}" "rename-window '%%'"
bind z new-window \; command-prompt -p " Window name:" "rename-window '#{?%1,%1,zsh}' \; send-keys z ' %1' C-m"

# set-hook -g after-new-session 'command-prompt -I "#{session_name}" "rename-session '%%'"'
bind-key C new-session \; command-prompt -p " Session name:" -I "#{session_name}" "rename-session '%%'"
Expand Down

0 comments on commit b93fb24

Please sign in to comment.