Yosh
Yosh

Reputation: 2742

set complete+=k[file] not working for whole line completion?

I have ~/.bash_history that contains lines such as sudo apt-get install, aplay foo.wav, etc. I thought it would be convenient if I could use these lines for completing whole lines while writing shell scripts.

:h compl-whole-line says:

CTRL-X CTRL-L
Search backwards for a line that starts with the same characters as those in the current line before the cursor. Indent is ignored. The matching line is inserted in front of the cursor. ** The 'complete' option is used to decide which buffers are searched for a match. Both loaded and unloaded buffers are used.**

Note the asterisks I inserted. :h 'complete' says:

 k      k{dict} scan the file {dict}.  Several "k" flags can be given,
        patterns are valid too.  For example: >
        :set cpt=k/usr/dict/*,k~/spanish

So I thought :set complete+=k~/.bash_history would do the job. It didn't.

Start vim with

$ vim -u NONE -N

then

:set complete+=k~/.bash_history
:set complete?
" complete=.,w,b,u,t,i,k~/.bash_history

, I enter to the insert mode (i), and when I type

su<C-x><C-l>

, vim says Whole line completion (^L^N^P) Pattern not found. The completion is working for words, so when I type

su<C-p>

sudo is suggested.

What am I missing? How can I use my history for whole line completion without :sp ~/.bash_history?

Upvotes: 2

Views: 198

Answers (1)

romainl
romainl

Reputation: 196809

set complete+=k[file] has nothing to do with line completion: whatever file you give to it will only be used as a source by dictionary completion (<C-x><C-k>) and keyword completion (<C-n>/<C-p>).

:help i_ctrl-x_ctrl-l is clear: line completion only works with lines found in loaded and unloaded buffers. Since ~/.bash_history is not loaded as a buffer, line completion will obviously never use it as a source.

So yes, the only practical solution is to load that file.

:sp ~/.bash_history is not really optimal, though, because a) it takes too much room, both physically and in your buffer list, and b) it requires way too many keystrokes.

The "too much room" part of the problem is easily solved with a more suited command:

:badd ~/.bash_history

The "too many keystrokes" part of the problem could be solved with an autocommand that runs the command above each time you edit a shell script:

augroup shell
    autocmd!
    autocmd BufNewFile,BufRead *.sh badd ~/.bash_history
augroup END

Upvotes: 2

Related Questions