Terminating a Vim Line Using an External File

Can line endings Ctrl + X Ctrl + L be used to display line endings from a specific external file instead of "only" from the current buffer? Something like dictionaries, but for strings.

Update:

To check, I did the following:

  • created a tt.txt file with some test lines
  • placed the file in D: \ t1 \ (I'm on windows)
  • included file with :set path+=D:\\t1\\tt.txt
  • :set complete ? returns complete =.,w,b,u,t,i
  • :set path ? returns path=.,,,D:\t1\tt.txt
  • checkpath returns: all included files were found
  • enter the line that should be filled with the corresponding contents from tt.txt with Ctrl + X Ctrl + L returns the template was not found

What am I missing?

+4
source share
5 answers

I think the only way to achieve what you want is to execute a custom full function. See help complete-functions for (very useful!) Documentation. Here is my attempt at a solution:

First you need a separate function for the silent grep file for the line (if you just call the naked vimgrep function, you will get an ugly error if there are no matches).

 function! SilentFileGrep( leader, file ) try exe 'vimgrep /^\s*' . a:leader . '.*/j ' . a:file catch /.*/ echo "no matches" endtry endfunction 

Now, here is your completion function. Please note that the path to the file you want to find is hardcoded here, but you can change it to use the variable if you want. We call SilentFileGrep() , which uploads the results to the quickfix list. Then we extract the results from qflist (trimming the leading space) and clear the qflist before returning the results.

 function! LineCompleteFromFile(findstart,base) if a:findstart " column to begin searching from (first non-whitespace column): return match(getline("."),'\S') else " grep the file and build list of results: let path = <path_to_file> call SilentFileGrep( a:base, path ) let matches = [] for thismatch in getqflist() " trim leading whitespace call add(matches, matchstr(thismatch.text,'\S.*')) endfor call setqflist([]) return matches endif endfunction 

To use this function, you need to set the completefunc option to point to it:

 set completefunc=LineCompleteFromFile 

You can then use <CX><CU> to call completion, which you can easily map to <CX><CL> .

This seems to work very well for me, but it is not exhaustively tested.

+7
source

In Vim help, to complete the line, it was written that it only works for loaded buffers. As a workaround, you can open your dictionary in another buffer, and Vim will suggest lines from that buffer.

+2
source
  • 'path' should be a list of directories, therefore (assuming the correct syntax for Windows) :set path+=D:\\t1\\tt.txt should be :set path+=D:\\t1\\ or :set path+=D:\\t1 .

  • i in 'complete' means that candidates to complete the selection are selected from the current and included files. You must specify a file to shut down: it will not be unless you explicitly include this file.

    Say you created ~/test/testfile with this single line:

     lorem ipsum dolor sit amet 

    You add it to Vim 'path' with:

     :set path+=~/test 

    To use it as a termination source in a C ++ file, you must:

     #include <testfile> 

    and be able to:

     lore<Cx><Cf> 

    To obtain:

     lorem ipsum dolor sit amet 

As far as I know, it does not work with languages ​​that do not have an include mechanism, such as C or C ++, so you can forget about it for Markdown, JavaScript, plain text, or, if my tests indicate, even PHP that has include .

If you need a more general mechanism, just add this file to the arglist: it will be automatically used as the source of termination:

 :argadd ~/test/testfile 
+1
source

If you do

 :set dictionary=<some file> 

then you can use ctrl + x and then ctrl + k to complete from <some file >

Cm

 :help ins-completion 

for more information.

0
source

As @black_wizard points out, another file must be loaded into the buffer. With set hidden you can use the following to load another file and return to the previous buffer:

 command! -nargs=? XL silent edit <args> | silent bprevious 

To load tt.txt into another buffer and return to the previous one:

 :XL tt.txt 
0
source

Source: https://habr.com/ru/post/1496296/


All Articles