Skip to main content

Make scp autocomplete server names and file/folders names on Ubuntu 16.04

I was struggling to make scp auto-complete servers names but also complete local files names under Ubuntu bash.

For some strange reason, out of the box solutions just didn't work, I could make the server completion work or the local files completion work but not both.
And I'm not even talking about remote file names over SSH, just local file names.

Installing auto-complete on Ubuntu if not already there:

https://unix.stackexchange.com/questions/136351/autocomplete-server-names-for-ssh-and-scp/335143

Replace /etc/bash_completion.d/ssh with the following content:


         ssh()
         {
         local cur prev opts
         COMPREPLY=()
         cur="${COMP_WORDS[COMP_CWORD]}"
         prev="${COMP_WORDS[COMP_CWORD-1]}"
         opts=$(grep '^Host' ~/.ssh/config | grep -v '[?*]' | cut -d ' ' -f 2-)
        
         _longopt

         RESULT="$COMPREPLY $opts"
         COMPREPLY=( $(compgen -W "$RESULT" -- ${cur}) )
        
         if [[ "$cur" == -* ]]; then
         COMPREPLY=( $compgen -W "$( $1 --help 2>&1 | \
         sed -ne 's/.*\(--[-A-Za-z0-9]\{1,\}=\{0,1\}\).*/\1/p' | sort -u)" \
         -- "$cur" ) )
         [[ $COMPREPLY == *= ]] && compopt -o nospace
         elif [[ "$1" == @(mk|rm)dir ]]; then
         _filedir -d
         else
         _filedir
         fi
        
         return 0
         }
         complete -F _ssh ssh
         complete -F _ssh scp


Enjoy !

Comments

Popular posts from this blog

Making pageant load keys and passwords from command line.

I have tried looking all over the place for a solution to load pageant.exe that will load my private keys using their passwords in an automated way and could not find one. Pageant by default knows how to read keys passed as parameters via command line but does not know how to receive the associated passwords belong to them. Therefore, I had created a patched version that knows how to handle the above. Usage: pageant.exe -p file1_password file1.ppk -p file2__password file2.ppk Link to download the patched pageant: pageant.exe Hope that helps.