You are not logged in.

#1 2015-01-29 20:36:35

flannelhead
Member
From: Finland
Registered: 2014-03-30
Posts: 55
Website

Quickly open projects in tmux with split window configuration

When I work on a coding project, I often have a window for it in tmux. The window is split horizontally into two panes. On the left there's vim, on the right there's just shell for running commands. This is a bit tedious to set up everytime I want to work on a project, so I created a little helper script for it.

The first script is called tmuxide.

#!/bin/sh

dir=${1:-$PWD}
name=${2:-$dir}

session="$USER-ide"
window="$session:$name"

# Create a new session if there isn't one
tmux has-session -t $session 2> /dev/null || tmux new-session -d -s $session
# Select the project's window if it exists, else create it
tmux select-window -t $window 2> /dev/null || {
    tmux new-window -c $dir -t $session -n $name 'vim .'
    tmux setw -t $window allow-rename off > /dev/null
    tmux split-window -t $window -d -c $dir -h -p 45
}
tmux -2 attach -d -t $session

The project directory and name are given as parameters. This looks for a session called $USER-ide and creates it if it's not found. Then, it tries to select the window with the project name. If it doesn't find it, a new window with the previously described split configuration is opened. Last, it attaches to the IDE session. So I can open a project view for instance in my dotfiles folder: `tmuxide $HOME/.dotfiles dotfiles`, where the second "dotfiles" is the name given to the project window.

To complement this, I created a convenience script called project. It's inspired by a feature called "Project Quick Open" found in some text editors.

#!/bin/sh

root=${PROJECT_ROOT:-"$HOME/projects"}
prj=$1

[ -z $prj ] && {
    ls -d $root/*/ | awk -F'/' '{print $(NF-1)}' | column
    printf "\n"
    read -e -p "Open project: " prj
}

dir="$root/$prj"
[ ! -d $dir ] && exit 0

tmuxide $dir $prj

This looks for the environment variable $PROJECT_ROOT. I can now supply the name of any folder under $PROJECT_ROOT: eg. if I run now `project metaballs`, it looks for the folder $PROJECT_ROOT/metaballs and opens a tmuxide window in that folder with the name "metaballs". If no project name is given as a parameter, the script shows a directory listing and prompts the user for a project.

The scripts become especially powerful when bound to a hotkey. For example, I'm binding the project script with sxhkd in my bspwm environment like this:

super + shift + p
        bspc rule -a URxvt -o desktop=code && urxvtc -e project

Now the powerful environment is just a keypress away.

I'm sure you all have your own workflows with vim, tmux etc. This is mine, and I just thought I'd share it in case someone finds it useful. Especially, the detection of existing windows and directing the commands to the right session in the tmuxide script was somewhat non-trivial (well, for me at least).

I'll appreciate any thoughts and ideas for improvement.

Edit: more descriptive title.

Last edited by flannelhead (2015-01-29 21:04:35)

Offline

Board footer

Powered by FluxBB