Drag and Drop from Terminal

(blog.meain.io)

118 points | by xk3 510 days ago

13 comments

  • aendruk 510 days ago
    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.

    • ddlsmurf 509 days ago
      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)
      • codesnik 509 days ago
        Wow, thank you. I didn't know about dragging, I always resorted to "open -R" to drag a file to anything GUI.
    • codetrotter 510 days ago
      Neat! Here’s a blog post with more details

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

    • runlevel1 509 days ago
      Explanation of how `ls --hyperlink` works and how to create links in the terminal: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3c...
    • puppybang 509 days ago
      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

      • sime2009 509 days ago
        What other actions do you have in mind? "copy path" and "open in default app" are the most obvious ones.
    • chrismorgan 509 days ago
      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”.
      • morsch 509 days ago
        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.

        • chrismorgan 509 days ago
          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.)
          • morsch 509 days ago
            Like I said, xdg-open file://hostname/path works for me. Dunno what the terminal uses, but I tried it directly.
            • chrismorgan 508 days ago
              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.

      • pmontra 509 days ago
        What if you control click on the hyperlink?
        • chrismorgan 509 days ago
          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.
          • pmontra 509 days ago
            Sorry to hear that. xdg-open and those URLs work in my Ubuntu 20.04 and Control click works in my Gnome Terminal.
    • pmontra 509 days ago
      Thanks! I almost don't need a graphical file manager anymore.
    • RockRobotRock 510 days ago
      TempleOS has had this for years
    • kwhitefoot 509 days ago
      Thank you!
  • _chendo_ 510 days ago
    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.

    • codesnik 509 days ago
      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.

    • dunham 509 days ago
      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.)
  • douglee650 509 days ago
    On Mac, I think you can `open .` and it will open a finder window in the pwd
    • CGamesPlay 509 days ago
      You can also `open -R $filename` and it will open the directory in finder with the relevant file selected.
    • sime2009 509 days ago
      `open .` will work on most Linux systems too.
      • chrismorgan 509 days ago
        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 .`.
    • zxcvgm 509 days ago
      I usually use “start .” on Windows to achieve the same.
  • hprotagonist 510 days ago
    https://news.ycombinator.com/item?id=33137057

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

  • angry_octet 510 days ago
    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.

    • dixie_land 510 days ago
      FWIW xdg-open works similar to open

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

      • leejoramo 510 days ago
        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

        • skissane 509 days ago
          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`.

    • synthc 510 days ago
      Linux has xdg-open to open files from the terminal
  • niedzielski 509 days ago
    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

    • xk3 507 days ago
      woah~! this is really simple but also a game changer. I like the way you think
  • kretaceous 509 days ago
    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

  • readingnews 510 days ago
    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.

    • doubleunplussed 509 days ago
      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.

      • readingnews 509 days ago
        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).
        • glandium 509 days ago
          Debian has been defaulting to wayland for a while, with e.g. Firefox still using X11 via xwayland.
      • chrismorgan 509 days ago
        Clipboard integration works smoothly in XWayland.
    • t-3 509 days ago
      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.
    • treve 509 days ago
      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.
  • flobosg 510 days ago
    Cool idea. I get some Plan 9/plumbing vibes from this.
  • Wevah 509 days ago
    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

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

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

  • jurip 509 days ago
    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.