The i3 FAQ has migrated to All content here is read-only.
Ask Your Question

How to launch a terminal "from here"?

asked 2012-06-21 07:44:55 +0000

napcode gravatar image

I often need another terminal in the same working directory (debugging, compiling... there are many use cases). How can I launch a terminal (rxvt) via i3 and set it to the working directory of the currently selected window?

edit retag flag offensive close merge delete

8 answers

Sort by ยป oldest newest most voted

answered 2012-06-21 09:29:58 +0000

Michael gravatar image

There is a script called cwd-spawn for urvxt which does this:

edit flag offensive delete link more


The link is dead and I can't find any other copies of or references to cwd-spawn. Any chance you could mirror the script?

bricksoup gravatar imagebricksoup ( 2012-09-15 19:30:12 +0000 )edit
Michael gravatar imageMichael ( 2012-09-23 19:09:44 +0000 )edit

answered 2013-04-25 13:07:48 +0000

schischi gravatar image


Being faced with the same problem, I wrote a simple program, which seems to work in most cases: xcwd on github

edit flag offensive delete link more


I tried all the other scripts mentioned in this thread, but this small c program was the only one that really worked for me. Thank you

cee gravatar imagecee ( 2013-06-20 16:04:05 +0000 )edit

Works great (Ubuntu 12.04 and 13.04), thank you.

syl20bnr gravatar imagesyl20bnr ( 2013-07-30 02:15:57 +0000 )edit

xcwd works as expected when launched from the command line but I can't make it work with a shortcut like this bindsym $mod+Return exec gnome-terminal --working-directory="`xcwd`"

syban gravatar imagesyban ( 2015-09-30 14:11:21 +0000 )edit

answered 2012-06-21 10:33:40 +0000

victor gravatar image

I wrote a simple script which seems to work:

ID=$(xdpyinfo | grep focus | cut -f4 -d " ")
PID=$(($(xprop -id $ID | grep -m 1 PID | cut -d " " -f 3) + 2))
if [ -e "/proc/$PID/cwd" ]
    urxvt -cd $(readlink /proc/$PID/cwd) &

With bindsym $mod+b exec ~/.i3/ in your config file.

edit flag offensive delete link more


Thanks victor! IGood idea!

napcode gravatar imagenapcode ( 2012-06-21 12:31:35 +0000 )edit

Great idea and quite simple solution! For me your script works only for terminals like urxvt or xterm. It does not work for gvim for example. Why you add 2 to pid ? Anyway this is version of your script that works for me:

Tahtisilma gravatar imageTahtisilma ( 2012-06-21 15:04:41 +0000 )edit

+2 is (certainly) to get the pid of the *child* processs of the terminal, ie the shell (after a fork). However, this trick doesn't work with urxvtd/c, as only the daemon opens all the shells ...

kevin gravatar imagekevin ( 2013-03-25 15:17:46 +0000 )edit

I've modified the script to more reliably find the child process. It uses the pstree command.

viking gravatar imageviking ( 2013-06-24 15:47:56 +0000 )edit

answered 2013-07-01 18:02:18 +0000

ack006 gravatar image

updated 2013-07-03 14:09:09 +0000

Here's my simple snippet for use with Bash, to be added to ${HOME}/.bashrc:

# Commands to be executed before the prompt is displayed
# Save current working dir
PROMPT_COMMAND='pwd > "${HOME}/.cwd"'

# Change to saved working dir
[[ -f "${HOME}/.cwd" ]] && cd "$(< ${HOME}/.cwd)"

No need to find the pid of the most recent shell, the only requirement is a shell which supports running a command either when the prompt is displayed or each time a command is entered at the prompt.

The saved directory even persists across logout/login, if you don't want that, simply delete ${HOME}/.cwd on logout.

Note: If you have multiple terminals open at different directories, switch to the terminal whose directory you want to 'clone', type <enter> or run some other command at the prompt to save that directory, then open a new terminal.

edit flag offensive delete link more


Awesome! This works great for me! First solution that actually works with gnome-terminal... and taking the simplicity of this, I am sure it will work anywhere with bash. Cheers for sharing!

bruno.braga gravatar imagebruno.braga ( 2013-07-01 22:19:22 +0000 )edit

But that will force HDD to write data on every [Enter] press!

ShadowPrince gravatar imageShadowPrince ( 2013-07-12 21:09:00 +0000 )edit

That's what "history" command does anyway, right?

bruno.braga gravatar imagebruno.braga ( 2013-07-14 11:14:02 +0000 )edit

For those who worry about too many disk writes, on a relatively modern Linux installation you can change $HOME to $XDG_RUNTIME_DIR which should expand to a per-user directory for run-time volatile storage. This should normally be mounted as a tmpfs, so no worries wearing out your precious SSDs :)

ack006 gravatar imageack006 ( 2015-08-10 19:48:45 +0000 )edit

answered 2015-04-10 12:21:42 +0000

machielo gravatar image

updated 2015-04-29 15:19:25 +0000

For those of you using urxvtd/c, none of the above answers work. This, however, works:

I use it like this in my .Xresources:

URxvt.perl-ext-common: others,fork-here
URxvt.keysym.Mod4-Shift-Return: perl:fork-here:fork-at-cwd

Then with Mod4+Enter I open a normal urxvtc terminal, but with Mod4+Shift+Enter I have the new urxvtc terminal "here", as the OP wants.

Remember to reread resources with xrdb ~/.Xresources, relaunch urxvtd to load the new perl extension, then relaunch a new urxvtc for it to take effect (or just reboot).

Hope this helps!

edit flag offensive delete link more


This does indeed work with pure urxvtd/c. However, it doesn't work in another situation which I find important. If I run mc (Midnight Commander), it switches to the folder from which mc was started, not to the current folder of mc. Some previous solutions work in this case with regular urxvt.

i3convert gravatar imagei3convert ( 2015-05-01 05:39:23 +0000 )edit

Hmmm I see, different use-cases :S I really like urxvtd/c to save some memory, but processes forked (launched) from urxvtc don't usually modify the parent terminal cwd so this doesn't work for those :(

machielo gravatar imagemachielo ( 2015-05-05 08:52:15 +0000 )edit

Perhaps a hybrid approach can be done inside the perl script, with xprop and if the process returned is URxvt then follow with this fork-here approach, but ideally we would use the xprop approach for everything, to launch a terminal in the same folder as, say, a pdf viewer, as explained by others :S

machielo gravatar imagemachielo ( 2015-05-05 08:53:58 +0000 )edit

@machielo: My current solution is I modified fork-here to get the pid of the shell for urxvtd/c and then I use another solution to extract the folder of the deepest child. This works at least in the Midnight Commander case I mentioned above. Thanks for the fork-here extension!

i3convert gravatar imagei3convert ( 2015-05-05 16:00:33 +0000 )edit

Awesome! But credit to where it's due: I didn't made the original fork-here extension, I just documented it here because it solved my needs :)

machielo gravatar imagemachielo ( 2015-05-07 14:37:58 +0000 )edit

answered 2013-07-01 18:31:59 +0000

V0id gravatar image

For zsh, I have the keybinding CTRL+N to open a terminal in the cwd of the current one.

# Open a new window in this term's cwd
alias nw="urxvt256c-mlc"
nwZle() { zle push-line; BUFFER="nw"; zle accept-line; }
zle -N nwZle
# CTRL+n
bindkey '^n' nwZle

Note the keybinding is only a shortcut to a command, so it works only when the prompt can accept a command. That is, either the line is blank or a command has been typed (in this case it will be restored upon launch), but not if you're in a pager, vim, a command is blocking, etc.

edit flag offensive delete link more


Excellent idea! I did away with the alias, and altered the function so it becomes like this: ``nwZle() { zle push-line; BUFFER="setsid urxvt"; zle accept-line; }``

joepd gravatar imagejoepd ( 2014-03-24 08:43:32 +0000 )edit

answered 2012-09-23 18:51:05 +0000

joepd gravatar image

Wanted to comment, but there was not enough space...

I worked with Tahtisilma's script. I needed to add 1 to the PID, in stead of 2. This is because the working directory of the terminal is irrelevant, we need to have the one of the shell contained in the terminal. I used pstree -p to find the heuristics of PID-enumeration, and made a keybind to $mod+Shift+Return.

Beware: urxvt{d,c} does not work, as only the PID of the daemon is returned with xprop.

Would be cool to have a cwd-parser for more than just terminals, so you can open a term in the directory where you have a pdf loaded...

edit flag offensive delete link more


Based on your idea of pstree I modified the script to walk to the deepest child with proper working directory. Here it is:

Tahtisilma gravatar imageTahtisilma ( 2012-11-09 14:30:00 +0000 )edit

answered 2015-06-30 18:43:52 +0000

For users of xfce4-terminal, like me, I put together a little shell script.

Here's a gist

In ~/.i3/config:

bindsym $mod+T exec "~/.i3/"
edit flag offensive delete link more

Question Tools



Asked: 2012-06-21 07:44:55 +0000

Seen: 6,409 times

Last updated: Jun 30