You are not logged in.

#1 2014-07-25 11:18:12

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

[SOLVED] Bash filename completion with vim command

In bash, tab completion of paths has recently stopped working when issuing vim commands, where the path is deeper than two directories (it continues to work as expected with other commands, such as ls and cd).

For example, pressing tab after

ls .config/btsync/bt

expands to

ls .config/btsync/btsync.conf

and pressing tab after

vim .config/bt

expands to

vim .config/btsync/

However, when pressing tab after

vim .config/btsync/bt

nothing happens.

I would expect it to expand to

vim .config/btsync/btsync.conf

as in the ls example, above.

I have bash-completion installed, there is no 'vim' file in /usr/share/bash-completion/completions/ or /etc/bash_completion.d/

Any suggestions as to the potential cause of this behaviour would be gratefully received!


Update:
I've narrowed this down to vim command in bash will only tab-complete directories, not files.

Last edited by roguewolf (2014-07-26 10:37:50)

Offline

#2 2014-07-25 11:29:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] Bash filename completion with vim command

The aliases and functions commands sourced by your bashrc might also help.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2014-07-25 11:30:50

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Bash filename completion with vim command

Have you tried tapping 'tab' twice?

Offline

#4 2014-07-25 11:41:43

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

Re: [SOLVED] Bash filename completion with vim command

Thanks for the quick replies.

@Trilby: Good point, I've edited my original post to include links.

@Karol: Tapping tab twice also has no effect.

Offline

#5 2014-07-25 11:45:18

Awebb
Member
Registered: 2010-05-06
Posts: 6,275

Re: [SOLVED] Bash filename completion with vim command

Have you, by chance, installed bash-completion and managed to misconfigure it?

Offline

#6 2014-07-25 11:48:21

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

Re: [SOLVED] Bash filename completion with vim command

@Awebb: I've had bash-completion installed for quite a long time but have never made any (intentional) changes to its configuration.

For information, there is no 'vim' file in /usr/share/bash-completion/completions/ or /etc/bash_completion.d/, I'm not sure if there should be.

Offline

#7 2014-07-25 12:21:02

Awebb
Member
Registered: 2010-05-06
Posts: 6,275

Re: [SOLVED] Bash filename completion with vim command

You could find that out, by going to the bash-completion page on the package site and list all files. There is no vim file by default. However, try uninstalling bash-completion, I remember having such an effect due to it.

Offline

#8 2014-07-25 12:31:21

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

Re: [SOLVED] Bash filename completion with vim command

I ran

pacman -Qi bash-completion

to get a list of all files installed by that package to find out if there were any vim-related files but, as you mentioned, there are none.

I uninstalled bash-completion but vim path completion still didn't work.  I also reinstalled, but still no joy.

I've also just confirmed that the same behaviour occurs when running as su, so that seems to suggest that it's not related to my user configuration?

Offline

#9 2014-07-25 13:49:59

Awebb
Member
Registered: 2010-05-06
Posts: 6,275

Re: [SOLVED] Bash filename completion with vim command

Yes, this is a safe assumption. I also reproduced the folder name and file to make sure it's not specific to the string. It works for me. It's a wild guess, but could you check your folder permissions?

Offline

#10 2014-07-25 14:26:12

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

Re: [SOLVED] Bash filename completion with vim command

Thank you for sticking with me on this.  I've just checked the folder permissions and everything is as I would expect.  I can also reproduce the behaviour on a number of folders throughout the filesystem (e.g. with '/usr/share/bash-completion', ironically).

I've also just noticed that it does not seem to be limited to subfolders;  if I type

vim .bash

and press TAB, it does not autocomplete to

vim .bashrc

even though the file is present.

Last edited by roguewolf (2014-07-25 14:28:28)

Offline

#11 2014-07-25 14:33:48

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

Re: [SOLVED] Bash filename completion with vim command

Also, if I just type 'vim ' (with the trailing space) and press tab twice, only directories are displayed.

If I type 'nano ' and press tab twice, both files and directories are displayed.

So, I think I can narrow this issue down to: vim command in bash will only tab-complete directories, not files.

Offline

#12 2014-07-26 10:36:12

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

Re: [SOLVED] Bash filename completion with vim command

It turned out to be due to the completion specifications associated with vim.

complete -p vim

returned

complete -F _filedir_xspec vim

Running

complete -r vim

removed those entries and allowed vim to complete filenames again.

Now to do some research on what the original associated specifications meant and how they ended up there.

Thanks to everyone for their help.  Marking as solved.

Offline

#13 2014-07-28 08:27:14

Awebb
Member
Registered: 2010-05-06
Posts: 6,275

Re: [SOLVED] Bash filename completion with vim command

Wow, I'm glad I stuck around. I was not aware of the complete command. Nice!

Offline

#14 2014-07-28 12:36:17

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: [SOLVED] Bash filename completion with vim command

Awebb wrote:

Wow, I'm glad I stuck around. I was not aware of the complete command. Nice!

bash-completion is built around the complete command wink

Offline

#15 2014-07-28 13:45:17

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVED] Bash filename completion with vim command

I have the same output for complete -p vim but don't have the same issue with autocompletion:

$ complete -p vim
complete -F _filedir_xspec vim
$ ls -F test/
a_file     somefile     some_otherfile.conf     this_dir/
$ vim test/<tab><tab>
a_file     somefile     some_otherfile.conf     this_dir/
$ vim test/some<tab><tab>
somefile     some_otherfile.conf
$ vim test/some

If I type the '_' and press tab, some_otherfile.conf is autocompleted.

I wonder if the actual problem could be the 'xspec' part. Firefox has the same output for complete -p, but only certain files will autocomplete:

$ complete -p firefox
complete -F _filedir_xspec firefox
$ ls
this_dir  a_file  arch.htm  somefile
$ firefox <tab><tab>
arch.htm  this_dir/ 
$ firefox 

So maybe something is limiting which files are autocomplete possibilities for vim.

Last edited by alphaniner (2014-07-28 14:25:15)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#16 2014-07-29 09:01:29

roguewolf
Member
From: UK
Registered: 2012-05-28
Posts: 32

Re: [SOLVED] Bash filename completion with vim command

I've done some more digging.

complete | grep _filedir_xspec

Showed that a large number of commands were using the _filedir_xspec function.  All of these commands (those that I tested, at least) exhibited the same 'directory-only' completion behaviour.

I then found the _filedir_xspec function in /usr/share/bash-completion/bash_completion:

_filedir_xspec()
{
    local cur prev words cword
    _init_completion || return
    _tilde "$cur" || return 0
    local IFS=$'\n' xspec=${_xspecs[${1##*/}]} tmp
    local -a toks
    toks=( $(
        compgen -d -- "$(quote_readline "$cur")" | {
        while read -r tmp; do
            printf '%s\n' $tmp
        done
        }
        ))
    # Munge xspec to contain uppercase version too
    # http://thread.gmane.org/gmane.comp.shells.bash.bugs/15294/focus=1    eval xspec="${xspec}"
    local matchop=!
    if [[ $xspec == !* ]]; then
        xspec=${xspec#!}
        matchop=@
    fi
    xspec="$matchop($xspec|${xspec^^})"
    toks+=( $(
        eval compgen -f -X "!$xspec" -- "\$(quote_readline "\$cur")" | {
        while read -r tmp; do
            [[ -n $tmp ]] && printf '%s\n' $tmp
        done
        }
        ))
    if [[ ${#toks[@]} -ne 0 ]]; then
        compopt -o filenames
        COMPREPLY=( "${toks[@]}" )
    fi
}

Although I can't claim to understand everything the function is doing, one thing I do know is that it restricts completion to directories only.

So, I've uninstalled bash-completion and vim is now working as expected (not sure why this didn't work when I tried it previously, although I can't remember if I rebooted on that occasion).

@alphaniner: That is curious, it seems odd that both vim and firefox would share the same function but behave differently.  If you run

ls /usr/share/bash-completion/completions/

is there a file for either vim or firefox?

Also, does the _filedir_xspec function in your /usr/share/bash-completion/bash_completion file match the one I posted above?

Offline

#17 2014-07-30 13:25:55

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVED] Bash filename completion with vim command

I don't have any completions for vim or firefox in /usr/share/bash-completion/completions/ and my _filedir_xspec function matches yours. Regarding the different behaviour I see for vim and firefox, further down in the bash_completions file there's a bunch of calls to _install_xspec, specifically

_install_xspec '*.@([ao]|so|so.!(conf|*/*)|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class)' vi vim gvim rvim ...
_install_xspec '!*.@(?([xX]|[sS])[hH][tT][mM]?([lL]))' netscape mozilla lynx galeon dillo elinks amaya firefox ...

I thought a borked _xspec for vim could have caused the problem you were having (presumably they can be modified elsewhere), but if everything using _filedir_xspec is misbehaving it's probably something else. FWIW I just discovered you can view the 'active' _xspecs with

set |grep "^_xspecs"

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#18 2016-05-18 07:13:52

galen_liu
Member
Registered: 2016-05-18
Posts: 1

Re: [SOLVED] Bash filename completion with vim command

I have same issue with bash version 4.3.42, but bash version 4.3.11 don't have this issue!!!


bash --version
GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.





./bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Offline

#19 2016-05-18 07:39:14

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,788
Website

Re: [SOLVED] Bash filename completion with vim command

Please open a new topic about your problem, this one is old and marked as solved.

https://wiki.archlinux.org/index.php/Fo … bumping.22
https://wiki.archlinux.org/index.php/Fo … ow_to_post

Closing.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

Board footer

Powered by FluxBB