[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Opening a new window,and cd to the same directory as the currentwind
From: |
samuel.lethiec |
Subject: |
Re: Opening a new window,and cd to the same directory as the currentwindow,no matter what I'm doing |
Date: |
Tue, 20 Nov 2007 13:05:03 +0100 |
User-agent: |
RoundCube Webmail/0.1-rc1 |
Eventually, I've found a way to do this. I thought some of you might be
interested, so...
I'm quoting myself here:
> 3) How is the current window number stored in screen? I mean, is there a
command, a register or something,
> that would tell me the current window number? Then I could store it in a
variable before creating the new
> window...
>
> and... since on every window, the variable $WINDOW is defined (at window
creation, I know, but I don't
> change the window number), in my ~/.bashrc, I could find back the process
with $WINDOW = current window,
> and then find its CWD
Few answers latter I was already dreaming:
>
> I'd need something like the 'exec' command but which would take the
environment from the shell of the
> current window, not the parent shell
>
But actually, combine these two ideas, and you've got something not so
crasy! ;)
let me explain why:
==
exec [[fdpat] newcommand [args ...]]
Run a unix subprocess (...)in the current window
==
The WINDOW environment variable is also defined for the subprocess!
(As I already said, I never move window araund, so $WINDOW is always the
current windown umber, but it
might not always be the case for you...)
All we have to do now is to find back the current window process pid, and
then read its /proc/${pid}/cwd
Since this process and the subprocess have the same PPID, this makes things
even easier :)
Here is the 'interesting' part of my screen-utils.bash:
=====
#!/bin/bash
# Author: samlt
# This script is intended to be run within a screen session
# Find the PWD of the window process, from which this script is called
function getSameDir () { #{{{
local WINNB pid
# find the current window process pid
while read pid; do
[[ $$ == $pid ]] && continue
WINNB=$(tr '\000' '\n' </proc/${pid}/environ | sed -n
'/^WINDOW=/s///p')
[[ -z "${WINNB}" ]] && continue
[[ ${WINDOW} -eq ${WINNB} ]] && break
done < <(ps --ppid ${PPID} --noheader -o %p | sed 's/^[[:space:]]*//')
# check if we really found something, or if we've just reached the end
of the loop
[[ ${WINDOW} -eq ${WINNB} ]] || exit 2
echo $(readlink /proc/${pid}/cwd)
}
#}}}
# Open(in the background?) a new window in the same dir as the (local)
process on the current window
function newWindow () { # {{{
local DIR="$(getSameDir)"
# screen -X eval screen "stuff 'cd \"$DIR\"^M^L'" other
screen -X eval screen "stuff 'cd \"$DIR\"^M^L'"
}
# }}}
# (sniiiip...)
###############
# "main"
########
# if WINDOW is not set, no need to go further, we're not in a screen
session
[[ -z ${WINDOW} ]] && exit 1
case "${1}" in
nw)
newWindow
;;
# (sniiiip...)
esac
# vim: set et sts=3 sw=3 foldmethod=marker :
====
Now to open a new window (and cd to the same directory as the current
window) while you're working on
a remote box, or simply if you don't have your (local) prompt available
just issue
:exec /path/to/screen-utils.baeh nw
:)
Now make a shortcut, and enjoy :)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: Opening a new window,and cd to the same directory as the currentwindow,no matter what I'm doing,
samuel.lethiec <=