DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Tmux New Session

Entrée

I keep forgetting how to create a new tmux session.

Is it this one?

$ tmux new
sessions should be nested with care, unset $TMUX to force
Enter fullscreen mode Exit fullscreen mode

No.

Is it this one?

$ tmux new-session
sessions should be nested with care, unset $TMUX to force
Enter fullscreen mode Exit fullscreen mode

No.

$ tmux new-session -d
# from tmux cli, prefix/ctrl+b
# :new -d
Enter fullscreen mode Exit fullscreen mode

No error? Good. Check session with prefix + s (ctrl + b + s). Ok it seems this works.

However this gives a strange session name like 29. What is it? I though the ultimate answer to the universe was 42.

Solution

Here is the complete command to create a new tmux session:

$ tmux new-session -d -s "new" -c ~/path/to/some/dir -n "main"
Enter fullscreen mode Exit fullscreen mode

This command will create a session named new, with a window named main in ~/path/to/some/dir as working directory.

It would probably better to run it from function with some args for more customization;

# .zshrc/.bashrc


# Crate and attach a new session with args
# defaults: session named "new" in ${HOME} as working dir with window named "main"
#
# Usage:
# $ tnew
# $ tnew remote ~/path/to/dir
# $ tnew remote ~/path/to/dir scripts
tnew() {
    local session_name="${1:-new}"
    local session_dir=${2:-~/}
    local session_window_name="${3:-main}"

    tmux new-session \
        -d \
        -s ${session_name} \
        -c ${session_dir} \
        -n ${session_window_name}
}
Enter fullscreen mode Exit fullscreen mode

As described in function's doc you can use this function as below:

$ tnew
$ tnew remote ~/path/to/dir
$ tnew remote ~/path/to/dir scripts
Enter fullscreen mode Exit fullscreen mode

Now I can peacefully forget (again) creating new tmux session, all I need to
remember: tnew.

All done!

Discussion (0)