[SOLVED] xterm key binding to open new window in current directory
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
xterm key binding to open new window in current directory
Up until now, I've used "fancy" terminal emulators like konsole, but now I want to use plain old xterm. I've been able to find out how to do set it up pretty much how I need, except for one thing. With other terminal emulators, I'm used to being able to press "Ctrl-Shift-N" to open a new terminal in the current directory, but I can't figure out how to do this with xterm.
The closest things I've found are the "exec-formatted" and "exec-selectable" keybindings, which use data selected from elsewhere on the screen to form part of the command. I don't want to do that though; I want to bind a key to essentially just do "xterm &". I'm sure someone else must have figured this out already, but I haven't had any luck searching. Can anyone help?
A simple approach would be to bind a key to open a new xterm in general, say with xbindkeys (or through your DE).
I've got that already through my window manager. I'm talking about a shortcut to open a new terminal in the same directory as the current one. If I can't figure it out, I suppose "xterm &" isn't such a bad key binding after all.
Last edited by montagdude; 05-06-2017 at 10:28 PM.
Reason: typo
This is kind of convoluted, but it works (most of it taken from here). It needs 3 tools though (xbindkeys, xdotool, and xvkdb; all are on SBo).
First of all xbindkeys picks up ctrl+shift+n (I don't know if these codes will be different on different machines, I got them with xbindkeys -mk). So something like this in ~/.xbindkeysrc;
Code:
"~/custom.sh"
m:0x5 + c:57
Control+Shift + n
Then the script that actually runs (in this case, ~/custom.sh):
Code:
#!/bin/sh
W=`xdotool getactivewindow`
S1=`xprop -id ${W} |awk '/WM_CLASS/{print $4}'`
S2='"XTerm"'
if [ "$S1" == "$S2" ]; then
xterm
else
xvkbd -xsendevent -text "\CN"
fi
This is kind of convoluted, but it works (most of it taken from here). It needs 3 tools though (xbindkeys, xdotool, and xvkdb; all are on SBo).
First of all xbindkeys picks up ctrl+shift+n (I don't know if these codes will be different on different machines, I got them with xbindkeys -mk). So something like this in ~/.xbindkeysrc;
Code:
"~/custom.sh"
m:0x5 + c:57
Control+Shift + n
Then the script that actually runs (in this case, ~/custom.sh):
Code:
#!/bin/sh
W=`xdotool getactivewindow`
S1=`xprop -id ${W} |awk '/WM_CLASS/{print $4}'`
S2='"XTerm"'
if [ "$S1" == "$S2" ]; then
xterm
else
xvkbd -xsendevent -text "\CN"
fi
I'm sure there's a better way though
EDIT: I thought it was working, but it doesn't open in the right directory. It seems to open xterm in whichever directory the xbindkeys daemon was run from (normally $HOME if it's run as a startup application). So the tricky part still seems to be starting a new xterm in the same directory as the active one.
Last edited by montagdude; 05-06-2017 at 11:09 PM.
* When there are multiple child PIDs for the xterm process, take the first one, which is the bash shell. That was necessary because my method for launching it creates a subprocess, and the following command that sets DIR won't work unless SH_PID lists only the PID for bash. In addition to letting cut work right, echoing the result of the ps command also gets rid of any spaces preceding the PID, which the pid command would insert if the number was low enough (less than 10000).
* This modification makes it work when the CWD has spaces.
* Is the xvkbd command necessary? Doesn't seem to be, and then I don't need to install xvkbd.
Finally, I was able to avoid using xbindkeys by putting this in ~/.Xresources:
Thanks for that. I didn't know about that method either. I prefer dgribbon's method, just because the new xterm is not a background process in the original like it is when you do `xterm &`.
Last edited by montagdude; 05-07-2017 at 09:34 PM.
I thought you would want to use the keyboard shortcut in other programs, so the xvkdb part is supposed to pass the ctrl-shift-n command through if it's enacted on a program which is not an xterm. I tried it using a different method, and it ended up in an infinite loop of triggering xbindkey repeatedly (although the xvkdb method seems to avoid that). If you only ever want to use ctrl-shift-n in xterms, then the whole thing is probably overkill
*edit* Just saw the binding in ~/.Xresources, much cleaner solution.
Good point; I would probably do `setsid xterm' in that case, rather than `xterm &'
Thanks! This place is really a wealth of information. I'm using this version at work where xdotool is going to be annoying to build. Do you know if the keyboard shortcut can be made "Ctrl-Shift-N" instead of just "Ctrl-N"?
I tried both of those variations, but here they both seem to correspond to Control-n. Nothing happens with Control-Shift-n even if I enter the binding as \C-N.
Edit: strangely enough, if I set it as \C-f or \C-p or their upper-case variations, it works with or without Shift. Don't know why N is different.
Edit 2: Nevermind, I figured it out. I still had the Ctrl-shift-n binding in ~/.Xresources, even though it wasn't being used. After removing that, the bash binding works properly again. It works with or without Shift and no matter whether the binding is "\C-N" or "\C-n", but I don't mind that.
Thanks again to both of you for coming up with 2 solutions. I also learned a few things in the process.
Last edited by montagdude; 05-08-2017 at 02:30 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.