Shell – Bash script to start tmux and issue commands

gnome-terminalnautilusshell-scripttmux

I want to use a nautilus script to open a (gnome-) terminal with a tmux session (or start one) at a specific location and then execute some commands in this terminal (e.g. nvim $file).

I've encountered 2 problems however:
1: I have "Run a custom command instead of my shell" at "tmux", such that every terminal starts in a tmux session. This seems to negate the ability to open the terminal at a given location. What I tried is putting an executable test.sh file in ~/.local/share/nautilus/scripts/ with content:

#!/bin/bash
gnome-terminal --working-directory=$NAUTILUS_SCRIPT_CURRENT_URI 

this works on a blank profile. With "tmux" as startup command however I just get a blank terminal at ~

2: If I try to use any command after that, nothing happens.

nvim some_file_there

does nothing, just as echo "hi" and exec echo 'hi'

Could someone explain the behaviour to me?

Meanwhile I've deactivated the "Run a custom command" setting in terminal. However, still I can only change the working directory (open terminal here), but cannot issue any further commands.

My newest test script containing only:

#!/bin/bash
zenity --info --text="$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
gnome-terminal -e "ls"

Does somehow change the working directory to the one, that the nautilus script is started from! Also it shows the results of the ls command, but in the terminal a dialog band is dropped down in blue saying: "The child process exited normally with status 0." And a Relaunch button to the right.
– I guess this means, that a new session or terminal or so is started (the child), but it doesn't continue, such that I could eventually use it!?

Can someone maybe clarify what happens here?

Best Answer

I've found solution relying heavily on tmux. Since tmux is working independently of the shell and prevails even after closing the windows, one can prepare a tmux session and then attach to it. The thing won't instantly exit, since the attachment command does not return unless you exit it.

This and the fact that you can name and search a session yields the following Nautilus-Script:

#!/bin/bash
# nautilus script to start files in nvim under a tmux session
# place this script into ~/.local/share/nautilus/scripts/nvimOpen.sh
# presented without warranty by mike aka curvi

# nvim running in another session? -
# TODO tmux rename-session -t $whaever nvim

# Tmux session 'nvim' is running neovim always!
if tmux has-session -t nvim ; then
  # test if it is open and split it
  for selected_file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS ; do
    tmux send-keys -t nvim Escape # change to normal mode
    tmux send-keys -t nvim ";vsp $selected_file" Enter # open file in vsplit
  done
else
  # or start it up freshly!
  tmux new-session -d -s nvim ;
  tmux send-keys -t nvim "nvim -O $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" Enter
  tmux send-keys -t nvim Escape # change to normal mode
fi
# after the tmux session is prepared - attach to it in gnome-terminal!
gnome-terminal -e "tmux attach-session -d -t nvim"

Bonus: since I send the keys, instead of issueing the commands directly they appear in the terminals history, like expected!

Attention: my nvim/init.vim contains a remapping of ; to :, which means, that in order to run one has to check the sent keys for "regular" vim/neovim settings!

Related Question