You are not logged in.

#1 2016-11-17 18:49:29

LithoUser
Member
Registered: 2016-11-17
Posts: 139

[SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Hello,

Hard to find a good title for this one wink I've installed Arch a few weeks ago, and this is the first problem I encounter that I can't solve by myself using the wiki.

I currently have this command in my Openbox menu.xml file :

urxvt -e vim ~/.config/openbox/menu.xml

It allows me to have a shortcut in my openbox menu to easily edit the menu.xml file.

My issue is that opening urxvt like this doesn't automatically start tmux (and so some parts of my vimrc don't work anymore).

I've tried all the solutions proposed in the "Start tmux on every shell login" Tmux wiki page one after the other, but it never works : vim is opened inside of urxvt (and I've checked that my .vimrc and my .bashrc are correctly sourced), but tmux is not.

So my question is : is there a way to automatically start tmux when I open a new terminal with vim with my openbox menu ?

Thanx in advance for your help !

Last edited by LithoUser (2016-11-30 03:27:34)

Offline

#2 2016-11-18 21:33:05

geno.nullfree
Member
From: en_US
Registered: 2013-05-29
Posts: 18

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

can you do something like

urxvt -e tmux new-session vim ~/.config/openbox/menu.xml

Offline

#3 2016-11-19 09:34:24

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Yes I can, thank you very much !

There is an exception though : opening a protected file with "gksudo" doesn't seem to source tmux.conf when tmux is launched.
For example :

gksudo urxvt -e tmux new-session vim /bin/myfile

Maybe it has something to do with the "TMUX" variable, since when I want to use "sudo" using the command line to open a file in vim using tmux, I have to do :

sudo TMUX="${TMUX}" vim /bin/myfile

Or am I missing something ?

But unfortunately, mixing both commands in the following code doesn't work (the window is closed instantaneously) as a command in my openbox menu.xml file :

gksudo TMUX="${TMUX}" 'urxvt -e tmux new-session vim /bin/myfile'

So, is there a way to correctly source my .tmux.conf file even when I use sudo with vim ?

Offline

#4 2016-11-22 20:00:06

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

After some more investigations, it seems that I was wrong.

The "TMUX" variable has nothing to do with this. It's just that my .tmux.conf file is not sourced with sudo.

So : is there a way to source it ? Maybe I should create an alias for the "tmux" command to source my .tmux.conf file ? Is there a way to do this so that it's still compatible with the above solution (urxvt -e tmux new-session vim) ?

Offline

#5 2016-11-23 04:16:32

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

according to the man page, the command to source a config file for tmux is

tmux -f /location/of/config/file

Offline

#6 2016-11-24 06:30:03

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Sure, but this doesn't solve my problem, since the following doesn't work :

gksudo 'urxvt -e tmux -f ~/.tmux.conf new-session vim /bin/myfile'

Offline

#7 2016-11-24 06:47:51

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

that's probably because you're using a substitution for the path, which defaults to /root since you're doing it after gksudo (unless I'm mistaken). try using the absolute path

Offline

#8 2016-11-24 14:17:37

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

You're right. The following seems to work :

gksudo 'urxvt -e tmux -f /home/user/.tmux.conf new-session vim /bin/myfile'

Except that now my .bashrc is not sourced by urxvt. This really seems like a nightmare !

Is my configuration wrong somewhere ? Isn't there a way to do

gksudo urxvt vim /bin/myfile

and have the rest done automatically ? In fact, shouldn't it be like this by default ? What am I missing ?

For example, if I want to manually open a new terminal window and edit some protected file with vim, it's hard to remember the entire command and to type it each time...

So I guess I still have two issues :

  • my .bashrc is not sourced ;

  • I should find a way to have all those files sourced by default.

Anyway, a great thank you for your help HilmTye !

Last edited by LithoUser (2016-11-24 14:18:55)

Offline

#9 2016-11-24 14:22:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

LithoUser wrote:

I've tried all the solutions proposed in the "Start tmux on every shell login"

That doesn't work as you aren't starting a shell.

As for all the gksu stuff, that is a horrible idea.  You want to run urxvt, tmux, and vim as root just so you can edit a file owned by root?  No, you should run urxvt as your regular user, tmux as your regular user, and vim as root.  Or even better run everything as a regular user, then just do `:w !sudo tee %` to save the file as root.  This will allow your user configurations to be used at every stage (for tmux, and vim).  And it will not open several processes as root that really don't need to be run as root.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2016-11-24 15:28:20

2ManyDogs
Forum Moderator
Registered: 2012-01-15
Posts: 4,645

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

LithoUser wrote:

Except that now my .bashrc is not sourced by urxvt. This really seems like a nightmare !

Why would you think your user's tmux config and .bashrc would be sourced when you are running things as root?

Either do as Trilby suggests and run things as your user (where your tmux conf and bashrc will be sourced), copy your user's config files to /root, or move whatever you need sourced to the global config files so they are available for all users.

Last edited by 2ManyDogs (2016-11-24 15:36:35)


How to post. A sincere effort to use modest and proper language and grammar is a sign of respect toward the community.

Offline

#11 2016-11-25 13:08:10

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Why would you think your user's tmux config and .bashrc would be sourced when you are running things as root?

True. My misunderstanding.

So I'm going to follow Trillby's advice, and use `:w !sudo tee %\` when I have to save a file as root. I *think* things are a bit clearer for me now ; many thanks for that.

Nevertheless, the following shortcut in my openbox menu still does not source my .bashrc :

urxvt -e tmux new-session vim ~/myfile.txt

My (obviously poor) understanding was that while doing this, I open a new terminal process, and I thought that the .bashrc file is supposed to be sourced in such a case. So what am I missing ?

Thank you for your help. I'm really conscious that I still do not understand many things well, and that's after having spent hours reading tutorials and - of course - the Arch wiki. My conversion to Linux is recent, and it's a whole new world to explore.

Offline

#12 2016-11-25 13:26:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

LithoUser wrote:

My (obviously poor) understanding was that while doing this, I open a new terminal process, and I thought that the .bashrc file is supposed to be sourced in such a case. So what am I missing?

Bashrc is not sourced when opening a terminal.  It is sourced when running bash (hence the name!).

With that command, your bashrc should be read for each new tmux window you open (assuming that you run bash in them) but as is, bash is never started in that command, so bashrc is not sourced.  If you want to run bash, run bash:

urxvt -e bash -c 'tmux new-session vim ~/myfile.txt'

You might need to add the '-l' flag to bash, I'm not sure.

You might also want to consider starting a detached tmux session on login, then just (re)attaching to that as needed.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#13 2016-11-27 18:49:12

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

I have tried the following :

urxvt -e bash -c 'tmux new-session vim ~/myfile.txt'
urxvt -e bash -c -l 'tmux new-session vim ~/myfile.txt'
urxvt -e bash -cl 'tmux new-session vim ~/myfile.txt'

None of these works (urxvt is immediately closed).

You might also want to consider starting a detached tmux session on login, then just (re)attaching to that as needed.

Good idea, I'll look into it.

Thanx for the help !

Offline

#14 2016-11-27 20:47:20

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Do you mean urxvt is immediately closed if/when you close vim (without opening another tmux tab/windw)?  If so, that is to be expected with those commands.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#15 2016-11-28 05:49:19

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

No, I mean that urxvt is opened for half a second and then immediately closes itself. That's why I've said that it doesn't work.

Offline

#16 2016-11-28 06:11:21

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

LithoUser wrote:
urxvt -e bash -c 'tmux new-session vim ~/myfile.txt'
urxvt -e bash -c -l 'tmux new-session vim ~/myfile.txt'
urxvt -e bash -cl 'tmux new-session vim ~/myfile.txt'

none of these tells tmux what to do with "vim ~/myfile.txt"
also, you will run into your earlier problem with using ~ instead of absolute paths

if you're not sure what the problem is, the best way to troubleshoot it is to stick it into a terminal and see what it spits out.

Last edited by HiImTye (2016-11-28 06:12:02)

Offline

#17 2016-11-28 06:40:53

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

also, tmux is itself a shell, so starting bash and then starting tmux is a bit silly, as tmux is going to use whatever is the default shell, which is probably also bash, but it's an extra pointless step.

Offline

#18 2016-11-28 07:25:25

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

I don't understand what you say here : bash is my default shell, yes. But launching tmux is not enough to have my .bashrc sourced. I assume that's why Trillby suggested to launch bash explicitely.

also, you will run into your earlier problem with using ~ instead of absolute paths

No, I've tried with full paths, and still no luck.

urxvt -e bash -cl 'tmux new-session vim /home/me/myfile.txt'

With this code urxvt is opened, tmux is launched, and vim opens the correct file. BUT my bashrc file is still not surced. And I'm out of resources...

Last edited by LithoUser (2016-11-28 07:26:06)

Offline

#19 2016-11-28 09:25:47

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

there's an easy way to find out

sudo tmux
echo $SHELL

as 2manydogs suggested, you need to be mindful of which user's configuration files are sourced when you run things as other users

Last edited by HiImTye (2016-11-28 09:26:16)

Offline

#20 2016-11-28 12:03:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

HiImTye wrote:

none of these tells tmux what to do with "vim ~/myfile.txt"

Yes, they do.  Tmux accepts a command to run in a new winodw.

HiImTye wrote:

also, you will run into your earlier problem with using ~ instead of absolute paths

No he wont.  He's no longer running this as root.  ~ is fine.

HiImTye wrote:

also, tmux is itself a shell...

No, it most certainly isn't.  It is a terminal multiplexer.  Just as urxvt will not run bash (and thus will not source bashrc) without being told to do so, niether will tmux.  Neither urxvt nor tmux are themselves shells.  They both - if and only if not given another command to run in it's place - will run the default shell (often bash) when they start up.  So if you give urxvt or tmux a command to run in place of your default shell, no shellrc file will be sourced.

Those commands you provided work for me.  Running them from a shell to see if there is an error message is a good idea.

As for bash, do you need your bashrc sourced before tmux runs?  Why do you need your bashrc sourced at all to run tmux and vim?  It's still very unclear to me what your end goal is.  You want to open a file in vim, and for some reason you want that vim session to be within a tmux session - WHY?  You also need bashrc to be sourced for vim and/or tmux to run the way you want - WHY?  These are very odd goals, so I'm wondering if there isn't an XY problem here.  How would you know if your bashrc has been sourced?  What effect do you expect it to have that you are not seeing?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#21 2016-11-28 13:30:35

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

I sure should have been more explicit about my intentions.

So here it is : I want to run tmux and my bashrc to have the same experience when I edit my config files in vim from my openbox menu that the experience I have when I manually launch vim to edit them. More precisely, my vim is configured to display some text (for example my markdown titles) in italics and to enable some special keys that I use frequently, thanx to these options in my .tmux.conf :

set -g default-terminal "tmux"
set-window-option -g xterm-keys on

And I have some transparency in my terminal windows, so that I can easily see through those windows which is sometimes useful (and nice to see wink
I like to be able to toggle this transparency (with a keybind defined in my openbox config), thanx to the transset-df tool and the following line in my .bashrc :

transset-df -a 0.85 > /dev/null 2>&1

So when I launch vim directly from my openbox menu, I can't benefit from those "enhancements" provided by my tmux.conf and bashrc configurations. But maybe (probably) it's not the right way to do this ?

I hope all of this is understandable. It costs me quite some time to write in english, but I love that language and am trying to make some efforts wink

Last edited by LithoUser (2016-11-28 13:31:41)

Offline

#22 2016-11-28 16:55:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Thanks that all makes sense.  You can definitely avoid needing tmux for these purposes as that configuration is all in vimrc anyways (you just test against the TERM variable I assume - just change the conditional so it doesn't require TERM=tmux).

You can likely also get rid of the need for bashrc being sourced for transparency if you configure transparency in your Xresources or Xdefaults.

Getting rid of the need for tmux here would vastly simplify the openbox menu entry and make troubleshooting much easier.  Also removing the need for bash would make it very easy, but may be less important.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#23 2016-11-28 18:34:07

woodape
Member
Registered: 2015-03-25
Posts: 159

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Shameless plug, checkout my git repo quickkeep. I ran into all of these problems trying to sort out something very similar to what you're doing and just made a script out of it. With the "quickkeep" script from the repo, you'd do

quickkeep Editor vim ~/.mytextfile.txt

and Bob's your uncle. It opens a urxvt terminal with a tmux session named "Editor" or whatever you want to call it, with vim opened to your text file. Both your bashrc and tmux conf will be sourced. When you close vim it'll kill the tmux-window vim was in, and if this is the only window in that tmux session, it'll kill the tmux session followed by the terminal itself. If you only close the terminal, then the tmux session persists and running "quickkeep Editor" will open a new terminal and pull it back up. If the tmux session is attached in a terminal but not in focus, "quickkeep Editor" will pull it to the current workspace and put it in focus.

I use this script for everything I do in the terminal. There's even a dmenu with it.

If you *really* wanted to run vim as root (and you should take Trilby's advice and not do this) you'd do the following:

quickkeep Editor gksudo vim ~/.mytextfile.txt

Last edited by woodape (2016-11-28 18:45:53)

Offline

#24 2016-11-29 08:15:59

LithoUser
Member
Registered: 2016-11-17
Posts: 139

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

Thanx for the info. I've tried your quickkeep script, double checking that xdotool and wmctrl are installed, since they're used by the script.

Unfortunately, my .bashrc is not even sourced with it. All the rest is fine (urxvt opens correctly, and so does tmux). So I assume I have a problem with my .bashrc config. I have a .bash_profile with just the following line :

[[ -f ~/.bashrc ]] && . ~/.bashrc

What am I missing ?

Last edited by LithoUser (2016-11-29 08:45:34)

Offline

#25 2016-11-29 11:59:53

cirrus
Member
From: Glasgow Scotland
Registered: 2012-08-24
Posts: 340
Website

Re: [SOLVED] Automatically open vim inside tmux inside urxvt in Openbox

OP consider launching your tmux session using a script.
The wiki has more indepth explanation

Offline

Board footer

Powered by FluxBB