Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Drag and Drop from Terminal (meain.io)
118 points by xk3 on Dec 2, 2022 | hide | past | favorite | 50 comments


See also:

  ls --hyperlink
With a compatible terminal you can then just click on results. Another nicety is making your shell prompt a link to the working directory.

Haven’t seen a terminal that lets you drag the links but that sounds like a logical feature.


iTerm2 has great build in support for it. You just cmd-click and hold a file and it's as if it was sourced from any other ui app. You can even get it to work with remote files both ways, including dropping a file on an iTerm2 window during an ssh session and it can scp it over for you. (yes needs some shell collaboration, but it's trivial to setup)


Wow, thank you. I didn't know about dragging, I always resorted to "open -R" to drag a file to anything GUI.


Definitely have an in depth read of iTerm2's documentation (<https://iterm2.com/documentation.html>), at the very least one of the feature page (<https://iterm2.com/features.html>) - I know it sounds weird, but it's so far ahead of other terminals that it's hard to imagine, and expectations are blown. The trigger feature for example is fantastic too.


Neat! Here’s a blog post with more details

https://purpleidea.com/blog/2018/06/29/hyperlinks-in-gnome-t...


Explanation of how `ls --hyperlink` works and how to create links in the terminal: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3c...


Sadly not all terminals have a great support for ls --hyperlink.

Here is a quick comparison if anyone needs: https://github.com/asdf8dfafjk/Terminal-Features/blob/main/F...

Wish some terminals would allow more "actions" on hyperlinks, maybe using right click, and even drag-drop


What other actions do you have in mind? "copy path" and "open in default app" are the most obvious ones.


ls --hyperlink is completely broken for me, because it’s producing links of the form file://hostname/path rather than file:///path, and xdg-open says “xdg-open: file 'file://hostname/path' does not exist”.


I also get file://hostname/path urls, but opening them works fine. xdg-open 1.1.3

Ctrl-clicking opens the link, but is anything supposed to happen when I click on the link (in Gnome Terminal). I thought it might copy the link, but nope.


Perhaps Gnome Terminal is using gio open instead of xdg-open. Running `gio open file://something-else/home` opens /home, apparently just completely ignoring the authority part. Theoretically including the host name is good because you can handle remote machines, but in practice I’m not sold on it because here we have one major tool ignoring it (actively bad!) and another major tool not supporting it (less bad, but less convenient), and there’s no standard accepted way of interpreting/opening a file: URL with an authority anyway. (Windows treats it as SMB and can do something provided it actually is that, but it won’t be, so it can’t. I don’t know of any other OS having any convention at all. Just tried it in a couple of browsers under Linux, Firefox actively discards the authority part, Chromium keeps it and works if it’s localhost or 127.0.0.1 or such, but says ERR_INVALID_URL if it’s something else.)


Like I said, xdg-open file://hostname/path works for me. Dunno what the terminal uses, but I tried it directly.


Ah, I did indeed misinterpret you.

On further investigation, I have found one authority that works in xdg-open: localhost. Not the host name, not 127.0.0.1, not ::1 or [::1], not anything else present in /etc/hosts, just localhost. I’m baffled, but not quite invested enough to delve into the source to figure out what could be going on.


What if you control click on the hyperlink?


Irrelevant. I’m using Alacritty, which is set to use xdg-open, and xdg-open isn’t liking the URL form, as demonstrated by running it directly.


Sorry to hear that. xdg-open and those URLs work in my Ubuntu 20.04 and Control click works in my Gnome Terminal.


Thanks! I almost don't need a graphical file manager anymore.


TempleOS has had this for years


Thank you!


Glad to see I'm not the only one that wanted this workflow!

I added this the first cut of this feature to iTerm2 about a decade ago because I often work in the terminal, but sometimes need to reference files elsewhere in my workflow. The feature was named "Semantic History" although in hindsight, it's not a very good name.

To use, hold Cmd and click+drag the path to where you want to drop it. Behind the scenes, the current working directory is stored with the position of the buffer, so it works on relative paths even when you change your working directory. On Cmd+Drag, it searches the buffer around the cursor until it finds a valid path to work with.


TIL that current working directory is stored with buffer lines. Cool idea.

I've quickly tried to drag and drop file from another directory earlier from the history to the terminal itself, but it was converted to "cd directory" command somehow.


On iPad "secure shellfish" can do this over an ssh connection. You can grab a file out of an "ls" listing and drag it to another app. (You do have to highlight the name first, so not quite as ergonomic as iTerm.)


On Mac, I think you can `open .` and it will open a finder window in the pwd


You can also `open -R $filename` and it will open the directory in finder with the relevant file selected.


`open .` will work on most Linux systems too.


Don’t think I’ve come a binary named open on a Linux machine. (And under Arch Linux, `pacman -F /usr/bin/open` produces nothing, so no package in the standard repositories contains it.) I’m guessing you’re thinking of `xdg-open .`.


There's also "see ."


Never heard of it, nor can I find any references to it, or Arch Linux packages containing it.


See this. I suspect it's installed by default on Ubuntu.

https://manpages.ubuntu.com/manpages/xenial/man1/run-mailcap...


I usually use “start .” on Windows to achieve the same.


https://news.ycombinator.com/item?id=33137057

dragon is great. goes very nicely with emacs/dired!


Nice. Linux has been missing out on this.

On macos you have the 'open <path>' command, which is equivalent to double clicking that <path> object, whether it be a folder, file or application; you can also drag a folder/file to the terminal to paste it's path. No context menu <open terminal here> by default, but I think there are add one which do that.


FWIW xdg-open works similar to open

https://linux.die.net/man/1/xdg-open


Just yesterday i accidentally type `open .` in my Ubuntu/KDE/fish shell to open the current directory in the GUI.

I quickly thought “wait, remember that only works on macOS” and the was shocked to see KDE’s Dolphin open the directory. I dig into it and found there was an alias for ‘open’ to ‘xdg-open’

Nice


On Linux, `open` was already taken - by a command to run a program on a virtual console (aka virtual terminal; also VC or VT for short). Probably doesn't see much use anymore. I remember in the 1990s, running Slackware, I used virtual consoles all the time. Nowadays, I have an Ubuntu box sitting on my desk, and I use it a lot, but always over SSH or HTTPS from my Mac. It doesn't even have a monitor attached to it any more; back when it did, the monitor would spend months at a time turned off, and on the rare occasions I did use it, I don't remember ever leaving X11 (or maybe it was Wayland, I can't remember). PTYs still get used all the time, but virtual consoles/terminals not that much.

Hence the command had to be named `xdg-open`. Until, more recently, someone decided it was a stupid situation, so `open` was renamed `openvt`, and `open` is now an alias for `xdg-open`.


Linux has xdg-open to open files from the terminal


Wow, this looks awesome! I love CLI / GUI integration tools.

In case you're interested, I wrote some examples on how the system clipboard is also a great integrator for migrating data to and from the CLI: https://github.com/niedzielski/cb#examples


woah~! this is really simple but also a game changer. I like the way you think


I use nnn[0] and it has a simple plugin[1] based on dragon. I can press `;d` and the dragon window pops up.

It is an OK workflow IMO. I'm not sure if I can directly drag and drop from the terminal list somehow. Or better yet, a full keyboard workflow.

0: https://github.com/jarun/nnn/

1: https://github.com/jarun/nnn/tree/master/plugins


Is it just me, or has copy/paste from terminal to terminal, webbrowser to terminal, anything to terminal gone awry? I seem to recall being able to just copy/paste from dang near anything to my terminal in Linux. Now, it surely seems like its heads or tails, hit or miss, "works 50% of the time, all of the time" kind of deal.

Is it just me? I swear I see this on three different distros. Highlight in terminal/program X, paste to <pick any terminal>, and half the time it just does not work/gibberish/crash and burn.


I wonder if it's something about you running on Wayland, and some terminals or programs are running under XWayland? Not sure if the two actually share the primary selection clipboard or whatever properly.

I'm still under Xorg (running GNOME) for now, I'll move to Wayland when the benefits outweigh the costs, which I don't think is the case for me yet.


I do not run wayland on my distros. I am on Gentoo, Debian and Slack. AFAIK, none of them now use wayland (my Gentoo install is xorg and icewm, for example).


Debian has been defaulting to wayland for a while, with e.g. Firefox still using X11 via xwayland.


Clipboard integration works smoothly in XWayland.


It's the browsers using a different clipboard and being weird. There are some clipboard daemons you can use to unify everything and get nice menus from, but otherwise you have to find out when to middle click, when to shift-insert and when to (puke) ctrl-v.


Reading this is a little wild, because I have been experiencing this for a while, but until this comment I thought I mussed have pressed the wrong keyboard buttons.


Cool idea. I get some Plan 9/plumbing vibes from this.


I wrote something similar for fun for macOS a while back[1], though it doesn’t have the niceness of double-clicking the filename. (And of course now I’ve noticed more things to fix or change.)

[1] https://github.com/Wevah/dragterm


I use Yoink [1] on macos for a similar purpose to 'dragon' mentioned in the post.

[1] https://eternalstorms.at/yoink/mac/tips/


Another related tip for Mac: if you are using Yoink — and you totally should be — you can add files to to it from shell with `open -a Yoink filename`. I have a shell function called `yo` for it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: