LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-20-2016, 09:26 AM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
Question Bash completion was wrong => commented => what was the problem?


Sometime ago, I found out the file /etc/bash_complete and /etc/bash_complete.d/ directory. Changing them allowed me to fix a few things I did not like about some "smart" choices for completion. Most of the times, what I did was to comment sections of the first file.

Now I have a problem with scp. In my history I have an scp command which I want to reuse: I want all of it to be the same, except the first file argument.

Suppose the command is:

Code:
scp /var/tmp/file.txt me@somewhere.around.me:/some/remote/folder/ # 123456
So what I do is:

- ^R 123456 => find the line to reuse

- HOME to go to the start of if

- a few DELETEs to erase the file argument

- now I am on the first argument, and want to use the completion to file or folder names. But it does not work!

As an "unwanted but necessary" fix, I type "a" in the command start, go back to editing the file argument (that now works, there is no "ascp" command with "smart" completion) and after getting the file name I wanted I erase the "a" and (finally!) execute it.

My current situation is:

1. there is no ocurrency of "scp" in /etc/bash_completion. Only two "scp" exist, and they are inside comments.

2. there is no "scp" file in the folder /etc/bash_completion.d

3. in the file /etc/bash_completion.d/ssh there were some lines about scp, but now I commented them.

Can you fix the problem it had? What is it?

The function for scp (uncommented again), is:

Code:
 scp(1) completion

_scp()
{
	local configfile cur userhost path prefix

	COMPREPLY=()
	cur=`_get_cword ":"`

	_expand || return 0

	if [[ "$cur" == *:* ]]; then
		local IFS=$'\t\n'
		# remove backslash escape from :
		cur=${cur/\\:/:}
		userhost=${cur%%?(\\):*}
		path=${cur#*:}
		# unescape spaces
		path=${path//\\\\\\\\ / }
		if [ -z "$path" ]; then
			# default to home dir of specified user on remote host
			path=$(ssh -o 'Batchmode yes' $userhost pwd 2>/dev/null)
		fi
		# escape spaces; remove executables, aliases, pipes and sockets;
		# add space at end of file names
		COMPREPLY=( $( ssh -o 'Batchmode yes' $userhost \
            command ls -aF1d "$path*" 2>/dev/null | \
            sed -e "s/[][(){}<>\",:;^&\!$=?\`|\\ ']/\\\\\\\\\\\\&/g" \
            -e 's/[*@|=]$//g' -e 's/[^\/]$/& /g' ) )
		return 0
	fi

	if [[ "$cur" = -F* ]]; then
		cur=${cur#-F}
		prefix=-F
	else
		# Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
		set -- "${COMP_WORDS[@]}"
		while [ $# -gt 0 ]; do
			if [ "${1:0:2}" = -F ]; then
				if [ ${#1} -gt 2 ]; then
					configfile="$(dequote "${1:2}")"
				else
					shift
					[ "$1" ] && configfile="$(dequote "$1")"
				fi
				break
			fi
			shift
		done

		[[ "$cur" == */* ]] || _known_hosts_real -c -a -F "$configfile" "$cur"
	fi
	# This approach is used instead of _filedir to get a space appended
	# after local file/dir completions, and $nospace retained for others.
	local IFS=$'\t\n'
	COMPREPLY=( "${COMPREPLY[@]}" $( command ls -aF1d $cur* 2>/dev/null | sed \
        -e "s/[][(){}<>\",:;^&\!$=?\`|\\ ']/\\\\&/g" \
        -e 's/[*@|=]$//g' -e 's/[^\/]$/& /g' -e "s/^/$prefix/") )

	return 0
}
complete -F _scp $nospace scp sshfs
Which is wrong - it should allow me to type that file/dir name where I wanted. How to fix it to add come back with its smartness?

Last edited by dedec0; 09-20-2016 at 09:28 AM.
 
Old 09-21-2016, 01:57 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Hm.
I do not think it is related to scp at all.
Bash will try to auto-complete the argument as a file/dir if possible. You can try it also with some fake command, like:
my_cmd_str /var/log/<auto-complete any file>
 
Old 09-21-2016, 09:11 AM   #3
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by pan64 View Post
Hm.
I do not think it is related to scp at all.
Bash will try to auto-complete the argument as a file/dir if possible. You can try it also with some fake command, like:
my_cmd_str /var/log/<auto-complete any file>
I do not understand you. I used "ascp" as the fake command to get the auto-complete for filenames in my wanted scp command position. But I had to change the command name from "scp" to "ascp", like I tried to describe above.

A command with the syntax "scp <filename> <user@server:path>" is valid, but the function above does not give me the completion for local filenames where they can be (and I chose to have them). Due the function existence, Bash will not use the normal completion, as other commands may have. So I commented it (for the whole system, there is no other way I know).

Now I am wondering if, for scp, a fix instead of commenting is better, but I cannot do it myself. That function is mostly hard for me to understand.
 
Old 10-05-2016, 10:43 PM   #4
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Still no answers... :(

When I said above: "Now I have a problem with scp", I knew the described problem is not with the scp command itself, but with Bash (or something connected to it), as I tried to show in the thread's title and other things I wrote.
 
Old 10-06-2016, 09:00 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
My understanding is that bash_completion.d is not supposed to help you complete the syntax of a particular command, but instead to help you find alternative commands following the same structure. For example, the following list:
Code:
debconf               debconf-communicate               debconf-escape               debconf-show
debconf-apt-progress  debconf-copydb                    debconf-set-selections
 
Old 10-06-2016, 01:46 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by dedec0 View Post
Sometime ago, I found out the file /etc/bash_complete and /etc/bash_complete.d/ directory. Changing them allowed me to fix a few things I did not like about some "smart" choices for completion. Most of the times, what I did was to comment sections of the first file.
maybe the answer lies here?
why don't you just rename the backups you made and see if it solves the problem?
oh, you didn't backup important system files before fiddling with them? reinstall bash/completion, then?
 
Old 10-07-2016, 06:31 AM   #7
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by ondoho View Post
maybe the answer lies here?
why don't you just rename the backups you made and see if it solves the problem?
oh, you didn't backup important system files before fiddling with them? reinstall bash/completion, then?
If the answer is there, I did not understand it. I have detailed my actions here, and (for me) they seem good and reasonable steps.

I have fiddled with some system files, but my changes are made to be reverted, if needed. For example, when I find a function that decides about arguments for a specific command, I do not delete its lines, I comment all of them and leave it there. A function of that sort may be changed in some detail, but an observation of what was changed is also added.

Eventually I make copies of these system files, so I can "move" the changes to other places or machines when I feel their "can-be-solved" problems.

--
@rtmistler

bash_completion.d (and the other files and directories I pointed) deal with the completion of commands name and also with their arguments. This includes the completion done for specific commands (like scp) that will have in its own "completion buffer"¹. For example, the scp completion function allowed only hostnames in the first argument - although we can also have filenames there. This problem is the reason why I commented the whole scp function, so it gets just the default completion for arguments for all commands: filenames. I do not understand the scp function, so I have not even tried to make changes to it - and this thread started when I imagined that something better could exist.

¹ I know it is not a buffer, just a metaphore here

--

Something almost offtopic here that I plan to also make elsewhere: a few weeks ago I started to always use the "thread solved" feature. While the threads I started are not "SOLVED", I expect another answer (not yet true for my old threads). When I am satisfied with the achievements made through the answers, I will mark it as solved. Then no more answers are necessary (new comments and ideas could be very welcome, although uncommon).

Last edited by dedec0; 10-07-2016 at 06:46 AM.
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why does auto-complete in bash think a pdf file is a directory??? taylorkh Linux - Newbie 4 11-29-2015 07:42 PM
Bash auto-complete of environment variables armandino101 Linux - General 11 12-20-2012 05:22 AM
How to get tab auto complete in bash to scroll through all possible choices joeldick Linux - Software 5 04-06-2011 04:27 PM
auto-completion - how does it work & can my script args auto-complete? BrianK Programming 1 06-11-2004 04:51 PM
bash auto-complete behaviour changes acid_kewpie Linux - Software 5 10-26-2002 01:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration