iTerm2: quick download over SSH using CMD+click
Asked Answered
M

3

5

iTerm2 allows you to click on a link (CMD+click) and open it quickly. However, when working over SSH, this doesn't work. Is it possible to enable this functionality, so that I can CMD+click a file, and it will automatically download into a folder on my local machine?

Thanks!

Metronome answered 17/4, 2017 at 14:26 Comment(0)
T
16

I've not had much success with ⌘+Clicking to download via SCP in iTerm2 because I have a complex set of rules involving jump hosts in ~/.ssh/config.

But I have found an elegant work around: a shell function which writes to STDOUT to trigger iTerm2 into capturing the output and saving it as a file!

I keep the following snippet (Toolbelt → Snippets) which I execute to define a command download:

alias download="bash <(base64 -d <<<'IyEvYmluL2Jhc2gKaWYgWyAkIyAtbHQgMSBdOyB0aGVuCiAgZWNobyAiVXNhZ2U6ICQwIGZpbGUg
Li4uIgogIGV4aXQgMQpmaQpmb3IgZmlsZW5hbWUgaW4gIiRAIgpkbwogIGlmIFsgISAtciAiJGZp
bGVuYW1lIiBdIDsgdGhlbgogICAgZWNobyBGaWxlICRmaWxlbmFtZSBkb2VzIG5vdCBleGlzdCBv
ciBpcyBub3QgcmVhZGFibGUuCiAgICBjb250aW51ZQogIGZpCgogIGZpbGVuYW1lNjQ9JChlY2hv
IC1uICIkZmlsZW5hbWUiIHwgYmFzZTY0KQogIGZpbGVzaXplPSggJCh3YyAtYyAiJHtmaWxlbmFt
ZX0iKSApCiAgcHJpbnRmICJcMDMzXTEzMzc7RmlsZT1uYW1lPSR7ZmlsZW5hbWU2NH07c2l6ZT0k
e2ZpbGVzaXplWzBdfToiCiAgYmFzZTY0IDwgIiRmaWxlbmFtZSIKICBwcmludGYgJ1xhJwpkb25l
Cg==')"

The base64-encoded string decodes to:

#!/bin/bash
if [ $# -lt 1 ]; then
  echo "Usage: $0 file ..."
  exit 1
fi
for filename in "$@"
do
  if [ ! -r "$filename" ] ; then
    echo File $filename does not exist or is not readable.
    continue
  fi

  filename64=$(echo -n "$filename" | base64)
  filesize=( $(wc -c "${filename}") )
  printf "\033]1337;File=name=${filename64};size=${filesize[0]}:"
  base64 < "$filename"
  printf '\a'
done

Which relies on iTerm2's download protocol

Sample session showing the notifications from iTerm2:

iTerm2 session on remote host OSX notification iTerm2 menu bar opens iTerm2 menu bar sub-menu

Tabaret answered 5/11, 2021 at 13:45 Comment(2)
The documentation for the download protocol is now here: iterm2.com/documentation-images.htmlElytron
relly nice workaround. did really help me and it's nifty and neat. Thank you very muchPaint
M
10

This is actually possible with Shell Integration installed. Note that Shell Integration will need to be installed on any server that you are ssh'ing into, not just on your local machine. From this link:

iTerm has recently introduced a feature called Shell Integration. Using this feature, we can upload and download files conveniently directly from iTerm 2. Drag a file into the window when pressing Option Key uploads the file to the remote ssh connection. Right-click on a file using ls command will bring up a context list containing downloading the file.

  1. Click “iTerm2->Install Shell Integration” when sshing into the remote server.
  2. Ensure the server has a correct FQDN as hostname and can be connected through this hostname. (You can use hostname -f to check it)
  3. If you’re using private key authentication, then you should have id_rsa in your .ssh directory. However, you should also put id_rsa.pub in your .ssh directory to use this feature.

Sorry for the late answer, but I was just trying to do the same thing and came across your question. Thought I would post my findings once I found a solution.

Myrlemyrlene answered 10/11, 2017 at 15:17 Comment(0)
M
0

CMD+click won't work over SSH, but if calling context menu it will show "Download with scp from [host]" option. I believe it does require installing shell integration.

Minh answered 14/10, 2024 at 15:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.