vim + COPY + mac over SSH

后端 未结 12 1200
悲哀的现实
悲哀的现实 2021-01-30 01:12

I access a sever over ssh on which I run vim for editing files. When I try to yank text from vim into an editor locally on my mac (lion) either with y OR \"+y it does not work.

相关标签:
12条回答
  • 2021-01-30 01:28

    Here's an update on the solution #2 from romainl. It creates and alias of the ssh command and launches the remotecopyserver if it's not running and installs the remotecopy(rclip) in the remote server. In short, you don't have to do anything except paste the code snippet below into your bash_profile.

    ######################## For SSH Remote Copy #########################
    export LC_SETUP_RC='command -v rclip >/dev/null 2>&1 || { echo "executing"; mkdir -p /usr/local/bin; if [ ! -f /usr/local/bin/rclip ];then wget https://raw.githubusercontent.com/justone/remotecopy/master/remotecopy -P /usr/local/bin/; ln -s /usr/local/bin/remotecopy /usr/local/bin/rclip; chmod +x /usr/local/bin/remotecopy; fi; if [[ \":\$PATH:\" == *\"/usr/local/bin:\"* ]]; then export PATH=/usr/local/bin:$PATH; fi } > /var/log/rclip.log 2>&1 || echo "Some error occured in setting up rclip. check /var/log/rclip.log"'
    
    ssh_function() {
    count="`ps -eaf | grep remotecopyserver | grep -v grep | wc -l`";
    if [ "$count" -eq "0" ]; then 
       mkdir -p $HOME/bin;
       if [ ! -f $HOME/bin/remotecopyserver ]; then 
          wget https://raw.githubusercontent.com/justone/remotecopy/master/remotecopyserver -P $HOME/bin;
          chmod +x $HOME/bin/remotecopyserver;
       fi;
       nohup $HOME/bin/remotecopyserver & 
    fi;
    ssh_cmd=`which ssh`
    PARAMS=""
    for PARAM in "$@"
    do
      PARAMS="${PARAMS} \"${PARAM}\""
    done
    bash -c "ssh ${PARAMS} -R 12345:localhost:12345 -t 'echo \$LC_SETUP_RC | sudo bash; bash -l'"
    }
    alias ssho=`which ssh`
    alias ssh=ssh_function
    alias ssh2=ssh_function
    
    vssh_function() {
    ssh_config=`vagrant ssh-config`;
    if [ "$?" -eq "1" ]; then
    echo "Problem with Vagrant config. run 'vagrant ssh-config' to debug"
    return 1
    fi
    PORT=`echo "$ssh_config" | grep Port | grep -o "[0-9]\+"`; 
    ID_FILE=`echo "$ssh_config" | grep IdentityFile | awk '{print $2}'`
    ssh2 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -i $ID_FILE vagrant@localhost -p $PORT "$@"
    }
    alias vssh=vssh_function
    

    Once the alias is activated, you can normally use ssh and whenever you need to copy to local clipboard from vim, use

    :w !rclip 
    

    to copy the whole file to clipboard

    :'<,'> !rclip
    

    to copy selected lines from visual mode. You have to press "Cmd+V" or "Ctrl+V" whenever it asks for the secret key.

    Bonus

    For those who work on Vagrant, there's a wrapper vssh which will execute vagrant ssh but also launches and install the necessary components.

    Reference

    Latest code snippet here - https://gist.github.com/ningsuhen/7933b206b92fc57364b2

    http://endot.org/2011/12/04/remotecopy-copy-from-remote-terminals-into-your-local-clipboard/

    https://github.com/justone/remotecopy

    Caveats

    The alias wraps the ssh command and there might be some unexpected issues. ssho is available if you need to execute ssh without the whole remotecopyserver thing. Alternatively, you can use the alias ssh2 and keep the ssh command as it is.

    0 讨论(0)
  • 2021-01-30 01:30

    My first answer on stackoverflow, but I feel it's a cool (albeit tiny) trick and it's worth posting. So here's what I do :

    cat <filename>
    

    When the text is printed onto the terminal, I select all the text with my mouse (the mouse scroll works since we're on the terminal window). Then copy that text with Cmd+C and paste into my local text editor.

    The only flaw with this trick is that it's impractical to use if your files are tens of thousands of lines long since selecting all the lines with your mouse would be a task in itself. But for a file of ~2k lines it works well.

    0 讨论(0)
  • 2021-01-30 01:32

    On MacOS, when SSH from machine A to machine B and using vim in machine B, I add this to my .vimrc in machine B:

    nmap yr :call system("ssh $machineA_IP pbcopy", @*)<CR>
    

    That way, in normal mode, if you copy something to * register, then type yr, the content of * register in vim@machine_B is copied to machine A's local clipboard, assuming you have setup Vim correctly with +clipboard and * register

    0 讨论(0)
  • 2021-01-30 01:35

    My go-to solution is to edit the file with vim from your local machine via scp.

    :e scp://remoteuser@server.tld//path/to/document
    

    This keeps your buffer local and makes it easy to copy to your local clipboard.

    The other advantage is that you get to use your local vim setup (.vimrc settings, plugins, etc.)

    0 讨论(0)
  • 2021-01-30 01:35

    I was hoping to improve on my solution here, but instead will share it as it seems on par with some of the others.

    When using iTerm2 on a Mac, sshing into machines, running tmux, and then editing with Vim, I have a vertical split iTerm2 window on the Mac that I pull all the way off to the side to make it as skinny as possible.

    Then when I want to copy text from Vim, I will click into the tiny slice of iTerm2 window, and go back over and highlight and then copy the text from Vim. This works the best for single lines of text.

    If there is a tmux vertical split, highlighting multiple lines in the Vim buffer won't wrap properly, and will copy text from the other tmux window, but otherwise this is great for copying 90% of what I need, without having to exit Vim, cat a file, or do something else.

    I also have ample horizontal window space, making the small iTerm2 window not a space hog.

    0 讨论(0)
  • 2021-01-30 01:37

    Yanking within vi in a terminal to which you ssh'd into copies the lines into vi's internal buffer on the remote machine, not into your Mac's clipboard.

    Use your mouse. :)

    0 讨论(0)
提交回复
热议问题