The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
3

How to launch a terminal "from here"?

asked Jun 21 '12

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?

8 answers

Sort by » oldest newest most voted
0

answered Jun 21 '12

Michael gravatar image

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

http://labs.qu.cx/urxvt-perl/tree/cwd...

Comments

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 (Sep 15 '12)edit
5

answered Apr 25 '13

schischi gravatar image

Hi,

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

Comments

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 (Jun 20 '13)edit

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

syl20bnr gravatar imagesyl20bnr (Jul 30 '13)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 (Sep 30 '15)edit
4

answered Jun 21 '12

victor gravatar image

I wrote a simple script which seems to work:

#!/bin/bash
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" ]
then
    urxvt -cd $(readlink /proc/$PID/cwd) &
else
    urxvt
fi

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

Comments

Thanks victor! IGood idea!

napcode gravatar imagenapcode (Jun 21 '12)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: http://pastebin.com/eKy1smm1

Tahtisilma gravatar imageTahtisilma (Jun 21 '12)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 (Mar 25 '13)edit
1

I've modified the script to more reliably find the child process. It uses the pstree command. https://gist.github.com/viking/5851049

viking gravatar imageviking (Jun 24 '13)edit
2

answered Jul 1 '13

ack006 gravatar image

updated Jul 3 '13

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.

Comments

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 (Jul 1 '13)edit

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

ShadowPrince gravatar imageShadowPrince (Jul 12 '13)edit

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

bruno.braga gravatar imagebruno.braga (Jul 14 '13)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 (Aug 10 '15)edit
1

answered Apr 10 '15

machielo gravatar image

updated Apr 29 '15

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

github.com/myeisha/urxvt-fork-here

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!

Comments

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 (May 1 '15)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 (May 5 '15)edit
1

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 (May 5 '15)edit
1

@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 (May 5 '15)edit
1

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 (May 7 '15)edit
1

answered Jul 1 '13

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.

Comments

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 (Mar 24 '14)edit
0

answered Sep 23 '12

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...

Comments

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

Tahtisilma gravatar imageTahtisilma (Nov 9 '12)edit
0

answered Jun 30 '15

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/xfce4-terminal-pwd.sh"

Question Tools

7 followers

Stats

Asked: Jun 21 '12

Seen: 6,409 times

Last updated: Jun 30