1

tmux behave differently between the command lines and equivalent config file (.tmux.conf)

command line:

bash$ tmux new \; split-window -h \;

config file (.tmux.conf), (and then, in the command line, we just type 'tmux')

new
split-window -h

The command line version nicely shows me a split window at the startup, and it creates only one session. However, the config file version behave differently:

  • It creates 2 sessions, with session name "0" and "1" rather than just one seesion.
  • Session "0" has the split windows, identical to what I saw in the command line version.
  • Session "1" has a single full window.
  • It stops at session "1".

So, in the config file version, I finally reach the session "1" on the screen. If I do an 'exit', and then attach session 0, then, it goes to session "0" with the same results as the command line version.

If, alternatively, the config file contains only the split line:

split-window -h

then when 'tmux' is typed, it shows an error message at the startup:

.tmux.conf:1: no current target 

And no split windows (since the split window command fails: As we can see, it complains the 1st line of the configuration to have no current target.)

Apparently, command line version is what I want/need.

What's wrong with the config file version?

1
  • The reason why it creates two sessions with tmux was discussed here stackoverflow.com/questions/40464015/… (need to see source or logs to confirm this). As for split-window -h failing, it needs a target to split.
    – davidt930
    Oct 16, 2023 at 11:11

1 Answer 1

3

The difference is in the tmux command you manually type after editing .tmux.conf.

Many issues with tmux become more understandable when you realize and remember the tmux server and each tmux client are separate processes. In some circumstances a client starts a server. .tmux.conf is for the server, not for clients.

tmux without arguments behaves like tmux new-session aka tmux new. It starts a tmux server if needed. When a tmux server starts, it parses .tmux.conf before handling requests from tmux clients.

So if there is no server already running, your sole tmux creates a tmux server and it executes:

new-session      # because of .tmux.conf
split-window -h  # because of .tmux.conf
new-session      # because this is the request from your client

Your client ends up attached to the session created with the last new-session, the one it implicitly requested.

If you want your client to attach to an already created session, use tmux attach-session aka tmux attach or tmux a. Note in case there is no tmux server yet, attach-session will try to create it and the session your .tmux.conf will create will count as "already created" when the request from the client is finally handled. This is mentioned in the documentation:

If no server is started, attach-session will attempt to start it; this will fail unless sessions are created in the configuration file.

With .tmux.conf in question, you need tmux a (not just tmux) to attach to the session created from within the file. Note if the tmux server already exists, it won't parse .tmux.conf again, your tmux a will try to attach to some existing session (if any).

Some users (including me) prefer to go back to an existing session, so they use tmux new-session -A or tmux a || tmux in their shells. These commands work fine with your .tmux.conf.

If you want split-window -h to run for any newly created session then a hook like this is the way:

set-hook -g session-created 'split-window -h'

(it's a tmux command, from a shell it will be tmux set-hook …).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .