0

Problem:
I use zsh and added to its config file a line of launching ranger on startup.

.zshrc

# A few other settings and commands.
ZSH_TMUX_AUTOSTART="true"
plugins=(tmux ...)

source ranger

Due to it every newly opened(mostly in tmux) zsh runs ranger.

Expectation:
Lauch a ranger process only if there is no running one.

Something like looks as follow:

if [[ ! -v RANGER_RUNNIG ]]; then
   export RANGER_RUNNING="true"
   source ranger
fi
5
  • this doesn't seem logical on multiple levels – why do you start zsh when you actually want to start ranger? Commented Jan 12, 2022 at 17:56
  • I don't start it, it's my default shell. A point is when i run tmux its first pane contains a ranger process and if i open more panes they also run it what i would like to disable. Moreover, if i open a subshell from a ranger process it inherits such a behaivor. Commented Jan 12, 2022 at 18:08
  • would you not rather want your tmux to open a ranger in the first pane, and for any other pane, start a zsh? Commented Jan 12, 2022 at 18:35
  • your export approach cannot work, because environment variables are only passed to children (i.e., processes started by a process), not along "siblings" (i.e., processes that were started by the same process, in your case tmux) Commented Jan 12, 2022 at 18:36
  • Well, as i use tmux zsh plugin i don't see a way to extend it by running tmux on startup only once with a custom command, it's ranger in my case. But the idea sounds good. Commented Jan 12, 2022 at 18:50

1 Answer 1

1

Basically, the solution through modifying what zsh does on every new shell sounds a bit complicated. Instead, simply start your tmux with the program that you want to show in the first pane:

tmux new-session ranger

When you manually add further panes, they will just start the default shell.

You can also prepare a whole bunch of panes. For example, to start with a layout of

┌────────────┬───────────┐
│            │    zsh    │
│            │           │
│  ranger    ├───────────┤
│            │    zsh    │
│            │           │
└────────────┴───────────┘

you could just

#!/bin/sh
# e.g. /usr/local/bin/my_tmux
# or just ~/bin/mt, to make your typing easier 
# (assuming /home/r45i/bin is part of $PATH)
#

# check we're not running within tmux
if [ -z "$TMUX" ]
then
  # start a detached session
  tmux new-session -d ranger
  # split the window horizontally
  tmux split-window -h
  # split the freshly created vertically
  tmux split-window -v
  # attach to the session
  tmux attach-session -d
fi
4
  • That's a good suggestion. But if you take a look at tmux plugin code you probably will get to an issue that it's incompatible to use the plugin env ZSH_TMUX_AUTOSTART and launch tmux with the tmux new-session -d ranger command. What do you think about it? Code of the plugin is here github.com/ohmyzsh/ohmyzsh/blob/master/plugins/tmux/… Commented Jan 13, 2022 at 10:27
  • sorry, must admit I don't have the time to go through that. Feels like a programming problem, someone at stackoverflow might be able to help rather rapidly! Commented Jan 13, 2022 at 12:20
  • 1
    Got it. Anyway, i'm going to work it out and after, if find a solution, get back to here to extend your answer. Thx. Commented Jan 13, 2022 at 13:18
  • I decided to drop tmux plugin off, it brings too much pain =) This snippet unix.stackexchange.com/a/355206/467276 fits more than others to my case. Now i added a line tmux new ranger inside the condition from the snippet to the end of .zshrc. Commented Jan 14, 2022 at 18:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.