meain/blog

Feb 23, 2022 . 2 min

Drag and drop from terminal

I have built up a pretty good workflow in the terminal. The main thing that I was missing until now was an easy way to drag and drop the files that I am looking at in a terminal to other apps.

So far, whenever I wanted to share a file from the terminal I would open up a GUI file browser, navigate to that directory, find the file and then drag and drop it. Not anymore. I recently was able to cobble together a pretty good(IMO) for dragging and dropping files to GUI applications and thought I would share.

Here is what is happening in the video. I am in a project repo in which I have listed the files in the current directory using ls. Now I just double-click on the file that I want to share, it bring up a thing (dragon) from which I can just drag and drop into slack.

Now let us see how to get this workflow. The main tool that is helping with this is dragon. Here is how you use it:

dragon <filename>

Using the above will open a window from which you can drag and drop. You can pass -x to close it automatically after you share one item.

It is already great on its own, but let's wire it up so that we can bring this up when we double-click on a filename. This is done using tmux.

bind -n DoubleClick1Pane run-shell "dragon -x '#{pane_current_path}/#{mouse_word}'"

What is happening here is that when you double-click on something, tmux end up calling dragon with pane_current_path(pwd of current shell) + / + mouse_word(whitespace separated piece of text under cursor) which ends up as path to the file. You can probably have it be more intelligent by separately passing it onto a script that only adds pane_current_path if and only if mouse_word is not a file or even search backwards/forwards for that file, but I just went with a simpler implementation as I will be mostly selecting after typing ls.

Another possibility is that you can just wire it up with ranger or lf and open dragon up like that.

I do end up taking and sharing a lot of screenshots, and so I have a special case for that. Every time I take a screenshot, the path is automatically copied to my clipboard. Now, in my wm (i3), I have a keybinding that will open up the path in clipboard with dragon. Here is the code for that:

bindsym $mod+Mod1+Control+Shift+l exec "dragon -x $(pbpaste)"

With this, after I take a screenshot I can press $mod+Mod1+Control+Shift+l and have that window to share it. About the keybinding, it actually ends up as tab+shift+l for me after remappings for those wondering how on earth I am remembering/pressing them

BONUS: The above explained setup is Linux only, but if you are on macOS, I was using something really hacky with hammerspoon to do something similar: https://github.com/meain/dotfiles/blob/master/hammerspoon/.config/hammerspoon/slackdrop.lua . This essentially just end up emulating the keybindings necessary to upload a file (from path in clipboard) from the slack UI.

And that is it. Enjoy sticking to just a terminal.

← Home