LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 05-06-2017, 08:27 PM   #1
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
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?
 
Old 05-06-2017, 09:52 PM   #2
drgibbon
Senior Member
 
Registered: Nov 2014
Distribution: Slackware64 15.0
Posts: 1,220

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
A simple approach would be to bind a key to open a new xterm in general, say with xbindkeys (or through your DE).
 
Old 05-06-2017, 10:18 PM   #3
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
Quote:
Originally Posted by drgibbon View Post
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
 
Old 05-06-2017, 10:40 PM   #4
drgibbon
Senior Member
 
Registered: Nov 2014
Distribution: Slackware64 15.0
Posts: 1,220

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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

Last edited by drgibbon; 05-06-2017 at 10:41 PM.
 
2 members found this post helpful.
Old 05-06-2017, 11:01 PM   #5
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
Quote:
Originally Posted by drgibbon View Post
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.
 
Old 05-06-2017, 11:49 PM   #6
drgibbon
Senior Member
 
Registered: Nov 2014
Distribution: Slackware64 15.0
Posts: 1,220

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
You can open in the CWD with;

Code:
#!/bin/sh

W=`xdotool getactivewindow`
WPID=`xdotool getactivewindow getwindowpid`
S1=`xprop -id ${W} | awk '/WM_CLASS/{print $4}'`
S2='"XTerm"'

if [ "$S1" == "$S2" ]; then
    SH_PID=`ps --ppid ${WPID} -o pid=`
    DIR=`readlink -e /proc/${SH_PID}/cwd/`
    xterm -e "cd ${DIR} && /bin/bash"
else
    xvkbd -xsendevent -text "\CN"
fi
 
3 members found this post helpful.
Old 05-07-2017, 06:48 AM   #7
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
Thanks dgribbon. Those are some tricks I didn't know about. I will update this thread with I get a chance to test it.
 
Old 05-07-2017, 02:10 PM   #8
Loomx
Member
 
Registered: Sep 2012
Distribution: Slackware
Posts: 184

Rep: Reputation: Disabled
You could do this via your ~/.bashrc file:

Code:
bind '"\C-N":"xterm &\n"'
 
1 members found this post helpful.
Old 05-07-2017, 09:06 PM   #9
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
Quote:
Originally Posted by drgibbon View Post
You can open in the CWD with;

Code:
#!/bin/sh

W=`xdotool getactivewindow`
WPID=`xdotool getactivewindow getwindowpid`
S1=`xprop -id ${W} | awk '/WM_CLASS/{print $4}'`
S2='"XTerm"'

if [ "$S1" == "$S2" ]; then
    SH_PID=`ps --ppid ${WPID} -o pid=`
    DIR=`readlink -e /proc/${SH_PID}/cwd/`
    xterm -e "cd ${DIR} && /bin/bash"
else
    xvkbd -xsendevent -text "\CN"
fi
Your method works. Well, actually, it's 95% of the way there. I had to change it slightly:

Code:
#!/bin/bash

W=$(xdotool getactivewindow)
WPID=$(xdotool getactivewindow getwindowpid)
S1=$(xprop -id ${W} | awk '/WM_CLASS/{print $4}')

if [ "$S1" == '"XTerm"' ]; then
    SH_PID=$(echo $(ps --ppid ${WPID} -o pid=) | cut -d' ' -f1)
    DIR=$(readlink -e /proc/${SH_PID}/cwd/ | sed 's/ /\\ /g')
    xterm -e "cd ${DIR} && /bin/bash"
fi
* 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:
Code:
*VT100.Translations: #override \
                    Shift Ctrl <Key>N:          exec-selectable("~/xterm-new.sh '%s'", word)
Where ~/xterm-new.sh is the script above.

I'm pretty happy with this method. It works well and turns out to only need xdotool, not xvkbd and xbindkeys. I am marking this as solved.

Last edited by montagdude; 05-07-2017 at 09:14 PM.
 
1 members found this post helpful.
Old 05-07-2017, 09:08 PM   #10
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
Quote:
Originally Posted by Loomx View Post
You could do this via your ~/.bashrc file:

Code:
bind '"\C-N":"xterm &\n"'
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.
 
Old 05-07-2017, 10:07 PM   #11
Loomx
Member
 
Registered: Sep 2012
Distribution: Slackware
Posts: 184

Rep: Reputation: Disabled
Good point; I would probably do `setsid xterm' in that case, rather than `xterm &'
 
Old 05-07-2017, 10:31 PM   #12
drgibbon
Senior Member
 
Registered: Nov 2014
Distribution: Slackware64 15.0
Posts: 1,220

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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.

Last edited by drgibbon; 05-07-2017 at 10:33 PM.
 
Old 05-08-2017, 09:24 AM   #13
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
Quote:
Originally Posted by Loomx View Post
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"?
 
Old 05-08-2017, 02:02 PM   #14
Loomx
Member
 
Registered: Sep 2012
Distribution: Slackware
Posts: 184

Rep: Reputation: Disabled
It already is :-)

Code:
bind '"\C-N":"setsid xterm\n"'  # Control-Shift-n
bind '"\C-n":"setsid xterm\n"'  # Control-n
 
Old 05-08-2017, 02:12 PM   #15
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619
Quote:
Originally Posted by Loomx View Post
It already is :-)

Code:
bind '"\C-N":"setsid xterm\n"'  # Control-Shift-n
bind '"\C-n":"setsid xterm\n"'  # Control-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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
xterm - howto lauch an xterm into a specific directory jobano Linux - Software 11 01-30-2023 04:45 AM
[SOLVED] st (simple terminal) - set window title to current directory dluco Linux - General 2 07-01-2014 12:11 PM
Apps open under current window rjs1943 Linux - Newbie 3 01-30-2014 08:44 AM
[SOLVED] perl script tp open an xterm window and run a command. casperdaghost Linux - Newbie 2 07-25-2010 02:49 AM
Window Maker key binding question. jhigz Linux - Newbie 3 12-12-2005 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 07:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration