Skip to content

Instantly share code, notes, and snippets.

@scmx
Last active December 14, 2023 15:16
Show Gist options
  • Save scmx/f32287a8931733fe6a64036779dffb22 to your computer and use it in GitHub Desktop.
Save scmx/f32287a8931733fe6a64036779dffb22 to your computer and use it in GitHub Desktop.
proj shell script project opener using fzf with tmux and tmuxinator

proj shell script project opener using fzf with tmux and tmuxinator

proj-fzf-project-opener-tmux-nvim

First you need a script like this in your terminal. It will detect all your projects, format them with a date when last used, sort them and pass through fzf. When a project has been selected it will cd to that directory and by default run tn which is another script for creating a new tmux session from the current directory. If you run proj something it will run that instead inside that directory.

$HOME/.zshrc or similar

proj() {
  local project_cmd=("$@")
  local project_dir=$($HOME/.scripts/proj)
  echo "$project_dir"
  [[ $? -gt 0 ]] && return 1
  cd "$project_dir"
  if [[ ${#project_cmd[@]} -eq 0 ]]; then
    tn
  else
    "${project_cmd[@]}" "$project_dir"
  fi
}

Dependencies

  • brew install fzf findutils tmux
  • gem install tmuxinator

Scripts

$HOME/.scripts/proj

#!/usr/bin/env bash

set -e

main() {
	result="$(find_projects | gsort -r | fzf "$@")"
	project=$(echo "$result" | cut -d ' ' -f3)
	command=("$@")
	[[ $? -eq 0 ]] || exit 1
	echo "$project"
	exit 0
}

find_projects() {
	find_in "$HOME/Dev/github" 2
	find_in "$HOME/Dev/bitbucket" 2
	find_in "$HOME/Dev/Experiments" 1
	find_in "$HOME/Dev/go/src" 3
}

find_in() {
	find_all_folders "${1:?}" "${2:?}" | grep -v '.git$'
}

find_all_folders() {
	gfind "${1:?}" \
		-mindepth "${2:?}" \
		-maxdepth "${2:?}" \
		-type d \
		-printf '%TY-%Tm-%Td %TH:%TM:%.2TS %p\n'
}

with_datestamp() {
	while read project; do
		echo "$(stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$project") $project"
	done
}

main "$@"

$HOME/.bin/tn

#!/usr/bin/env bash

set -e

# Use matching tmuxinator project
for project in "$HOME"/.config/tmuxinator/*; do
	name=$(grep -E -m 1 "^name:" "$project" | cut -d ' ' -f2)
	root=$(grep -E -m 1 "^root:" "$project" | cut -d ' ' -f2)

	[[ "$PWD" != "$root"* ]] && continue

	tmuxinator start "$name" || break

	exit 0
done

session_name="$(basename "$PWD" | sed -e "s/\./\-/g")"

tmuxinator start default -n "$session_name" "$PWD"

$HOME/.config/tmuxinator/default.yml

root: <%= args[0] %>
windows:
  - editor:
      layout: main-vertical
      panes:
        - NVIM_LISTEN_ADDRESS="/tmp/nvimsocket-<%= args[0] %>" nvim README.md +:G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment