DEV Community

Cover image for If Tmux
Waylon Walker
Waylon Walker

Posted on

If Tmux

I do much of my work from tmux, I love it so much that I want to setup some functionality that puts me in tmux even if I didnt ask for it.

Bash Function

Bash function to check if the shell is in a tmux session.

in_tmux () {
  if [ -n "$TMUX" ]; then
    return 0
  else
    return 1
  fi
  }
Enter fullscreen mode Exit fullscreen mode

Using the bash function

I often open up vim to do some quick edits, but before I know it I have several splits open and I need access to another shell utility, but I forgot to start in tmux. This function makes sure that I start in tmux every time.

Using if_tmux to ensure vim is opened in tmux.

vim () { 
  in_tmux \
    && nvim \
    || bash -c "\
    tmux new-session -d;\
    tmux send-keys nvim Space +GFiles C-m;\
    tmux -2 attach-session -d;
    "
  }
Enter fullscreen mode Exit fullscreen mode

I am not quite sure if this is proper use of the && and ||, let me know if you have a better way to do one thing if in_tmux returns true and another if it returns faslse.

Top comments (4)

Collapse
 
thefluxapex profile image
Ian Pride • Edited

The thing about && and || in Bash (or normal Shell) is that it doesn't always work as expected depending on other factors (when errors are thrown etc...) and (imo) should be used only in the simplest of situations. Having said that; lots of situations are simple and not error prone.

Instead of ever using that method I now always resort to if/else. It's ugly, but it's safe in all situations and I don't have to ever think about it. Having said that; I don't see any issue with your use here and it should be fine.

Your 1st function is a bit redundant:

in_tmux () {
  if [ -n "$TMUX" ]; then
    return 0
  else
    return 1
  fi
  }
Enter fullscreen mode Exit fullscreen mode

Could be:

in_tmux () {
    if [ -z "$TMUX" ]; then
        return 1
    fi
}

# or even
in_tmux () { [ -z "$TMUX" ] && return 1; }
Enter fullscreen mode Exit fullscreen mode

as a return of 0 is the default return and the -z switch tests if empty.

Collapse
 
waylonwalker profile image
Waylon Walker

Oooh nice I like that, you are definitely helping me step up my bash skills here.

Collapse
 
thefluxapex profile image
Ian Pride

Glad to help. Been coding in Linux 15+ years and Windows 20+; about as long as I have been on a machine I have made it do what I wanted it to do and so if there's something I know or am familiar with it's a command line/shell environment and it feels good to pass on what I've learned; pay it forward. Lots of trial and error, happily enduring countless degradation from pretentious CS elitists, and finally some great groups of people in forums (most of which no longer exist) who finally helped me get to where I am today (knowledge-wise lol) and so anything I can help somebody with I try to.

Again, glad I could help.

Thread Thread
 
waylonwalker profile image
Waylon Walker

Lots of trial and error, happily enduring countless degradation from pretentious CS elitists, and finally some great groups of people in forums

Thank you so much, its people like you who have got me where I am now. My very first "Mentor" was that person, and would hold everything for himself. He inspired me to be nice to others and share as much as possible as I could see what being an ass did for him.