Ticket #3088 (new enhancement)

Opened 4 years ago

Last modified 2 years ago

Tell the current directory to gnome-terminal

Reported by: egmont Owned by:
Priority: minor Milestone: Future Releases
Component: mc-core Version: 4.8.10
Keywords: Cc:
Blocked By: Blocking:
Branch state: no branch Votes for changeset:


Gnome-terminal has had a cool feature for ages: when you open a new tab, it opens in the same directory as the one that was previously active. This gives a huge productivity boost. G-t used to achieve this by looking at the tab's immediately child (typically a shell), and checking its cwd under /proc. This way it was impossible for it to know MC's current directory.

G-t 3.7.0 (vte 0.34.0) changed the implementation: g-t no longer digs into /proc to figure it out automatically, it's the shells responsibility to tell it to g-t.

To test: if you have g-t >= 3.7.0 then execute this

echo -ne $'\e]7;file:///usr/bin\a'

and then open a new tab, it will open in /usr/bin.

The directory has to be URI-encoded, that is, prefixed by file:// and special characters encoded with %.

It would be really cool to have an option so that MC sets this whenever the directory is changed, pretty similar to the way it can update the window title. This way whenever you opened a new tab in g-t, it would open at the directory where you are in MC.



mc-4.8.10-terminal-cwd.patch (6.3 KB) - added by egmont 4 years ago.
first version
mc-4.8.13-terminal-cwd.patch (6.3 KB) - added by egmont 3 years ago.
updated to newest mc

Change History

comment:1 Changed 4 years ago by egmont

The feature seems to originate from Mac terminal, here's a good description:

Apparently the hostname should also be included in the URI so that the terminal knows if the shell is running on a remote host.

There's also a way to set the current filename. It might make sense to set to the name of the file being viewed/edited, or maybe even to the file where the cursor is. I'm not sure how this is being used, as I don't have access to Mac.

comment:2 Changed 4 years ago by egmont

Note: as opposed to the xterm window title, we should take care not to set it when on VFS.

Changed 4 years ago by egmont

first version

comment:3 Changed 4 years ago by egmont

I've added a version that works nice, but maybe still needs to be cleaned up a bit.

The feature has to be enabled in Options -> Configuration -> Auto cd new terminal.

It's disabled by default because the escape sequence that it prints messes up the display of old gnome-terminal (vte < 0.34). Other graphical emulators I've tried (xterm, konsole, urxvt, putty) ignore the sequence. The Linux console also becomes messed up, hence I added a check for xterm-like terminal.


Find a good name for this option, and find a shortcut key in the config dialog.

Figure out where the code should go in the source tree. Right now it's in filemanager/layout.c because the work was heavily based on the "xterm window title" feature which is there. But this new feature has nothing to do with the "layout". For the same reason I decided not to put the config option next to the xterm one.

Figure out when to call the method. This might depend on ticket #3090. Right now I'm calling this whenever the xterm window title is also updated. But it might be a better place to call it when the command prompt is updated, or when mc actually performs a chdir(). Although mc seems to call chdir() back and forth for both panels which would impose a short race condition here.

Please test the patch with Mac Terminal.app (>= Lion) or Gnome-terminal (>= 0.34). Expected behavior: when opening a new terminal tab, it should open in mc's current directory (unless it's a remote directory or an archive file or such). It should work even if the path contains all kinds of weird special characters.

A note for Konsole: when opening a new tab, it seems to look not only at the immediate child of the current tab, but somehow digs deeper and finds the actual "active" (?) process and takes its cwd. So in konsole you see this behavior even without this patch.

comment:4 Changed 4 years ago by egmont

Sorry, I meant: test with Gnome-terminal >= 3.7 (which depends on vte >= 0.34).

comment:5 Changed 4 years ago by egmont

I've been happily using this patch for 2 months, it makes opening a new terminal tab much more convenient. I just press Ctrl+Shift+T in gnome-terminal, and the new tab opens in the directory where I navigated to in MC. A convenience feature I could hardly live without, once I got used to it.

Dear developers, could you please comment on this, would you accept this feature to MC? If so, could you please help finalize the implementation (as detailed in comment 3)?

comment:6 Changed 3 years ago by egmont

Friendly ping - dear developers, any comments on this?

Note to myself: Should use g_filename_to_uri() rather than manually crafted code.

comment:7 Changed 3 years ago by mgedmin

/etc/profile.d/vte.sh, shipped with the libvte package on Ubuntu, does an explicit check

# Not running under vte?
[ "${VTE_VERSION:-0}" -ge 3405 ] || return 0

Midnight Commander could look at this environment variable and determine whether it needs to emit the OSC 7 escape code whenever the current working directory changes.

This would help Mac terminals, but would autodetect gnome-terminal versions that support and need this escape.

Last edited 3 years ago by mgedmin (previous) (diff)

Changed 3 years ago by egmont

updated to newest mc

comment:8 Changed 2 years ago by zaytsev

Aha, so that's why this feature doesn't work in latest Ubuntu :-/

Maybe mooffie is right though and this kind of stuff should be all offloaded to the scripting engine? Of course, the engine should first get merged, and then a consistent effort has to happen to strip the auxiliary niceties, where auxilariness of features might become a subject of sharp debates, but maybe mc-4.6 could be a good baseline...

Note: See TracTickets for help on using tickets.