Skip to main content
replaced http://vi.stackexchange.com/ with https://vi.stackexchange.com/
Source Link

I asked a similar question a few weeks ago (how to complete a tag name containing a colon) and here was the answeranswer.

I asked a similar question a few weeks ago (how to complete a tag name containing a colon) and here was the answer.

I asked a similar question a few weeks ago (how to complete a tag name containing a colon) and here was the answer.

added 334 characters in body
Source Link
saginaw
  • 6.9k
  • 2
  • 27
  • 47

If you want to preserve the original behavior of <C-X><C-N>, and use another {lhs} to trigger the custom completion, for example <C-X><C-A>, you could use this mapping:

inoremap <C-X><C-A> <C-O>:call <SID>CustomKeyword(join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)'), ''))<CR><C-X><C-N>

You just have to change the first <C-X><C-N> in the original {lhs}, but not the second one in the {rhs}.


If you want to preserve the original behavior of <C-X><C-N>, and use another {lhs} to trigger the custom completion, for example <C-X><C-A>, you could use this mapping:

inoremap <C-X><C-A> <C-O>:call <SID>CustomKeyword(join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)'), ''))<CR><C-X><C-N>

You just have to change the first <C-X><C-N> in the original {lhs}, but not the second one in the {rhs}.

deleted 10 characters in body
Source Link
saginaw
  • 6.9k
  • 2
  • 27
  • 47

I asked a similar question a few weeks ago (how to complete a tag name containing a colon) and here was the answer.


Your question is slightly different as you don't want to temporarily add a colon to the value of 'iskeyword' but all characters which can appear inside a WORD.

Here's a different version of the original answer which redefines the behavior of <C-X><C-N> to autocomplete WORDS instead of words:

inoremap <C-X><C-N> <C-O>:call <SID>CustomCompletion<SID>CustomKeyword(join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)'), ''))<CR><C-X><C-N>

function! s:CustomCompletionCustomKeyword(chars) abort
    for char in split(a:chars, '\zs')
        execute 'setlocal iskeyword+=' . char2nr(char)
    endfor
    augroup custom_keyword
        autocmd!
        autocmd CompleteDone * setlocal iskeyword< | autocmd! custom_keyword
    augroup END
endfunction

The characters inside the string that you pass as an argument to the function CustomCompletionCustomKeyword() will be added to the buffer-local value of 'isk', and the autocmd will reset the option once the completion is done by copying the global value into the buffer-local one (setlocal iskeyword<), as well as remove itself (| autocmd! custom_keyword).

Besides, you want to complete WORDS whose characters should be any printable character except a whitespace. According to this chart, it seems the first printable character is the exclamation mark ! and the last is the tilde ~. Thus, the string of printable characters could be obtained with the following code:

join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)'), '')

Which is the string passed as an argument to CustomCompletionCustomKeyword() inside the {rhs} of the previous mapping whose {lhs} is <C-X><C-N>.


Note that you could reuse the CustomCompletionCustomKeyword() function to change 'isk' in any way.
For example, if you wanted to autocomplete tag names containing a colon, you could install the following mapping:

inoremap <C-X><C-]> <C-O>:call <SID>CustomCompletion<SID>CustomKeyword(':')<CR><C-X><C-]>

The general syntax would be:

inoremap {lhs} <C-O>:call <SID>CustomCompletion<SID>CustomKeyword('characters_to_add_to_isk')<CR>{lhs}

Where {lhs} is the keys you hit to trigger the completion (<C-X><C-N>, <C-X><C-]>, ...).

I asked a similar question a few weeks ago (how to complete a tag name containing a colon) and here was the answer.


Your question is slightly different as you don't want to temporarily add a colon to the value of 'iskeyword' but all characters which can appear inside a WORD.

Here's a different version of the original answer which redefines the behavior of <C-X><C-N> to autocomplete WORDS instead of words:

inoremap <C-X><C-N> <C-O>:call <SID>CustomCompletion(join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)')))<CR><C-X><C-N>

function! s:CustomCompletion(chars) abort
    for char in split(a:chars, '\zs')
        execute 'setlocal iskeyword+=' . char2nr(char)
    endfor
    augroup custom_keyword
        autocmd!
        autocmd CompleteDone * setlocal iskeyword< | autocmd! custom_keyword
    augroup END
endfunction

The characters inside the string that you pass as an argument to the function CustomCompletion() will be added to the buffer-local value of 'isk', and the autocmd will reset the option once the completion is done by copying the global value into the buffer-local one (setlocal iskeyword<), as well as remove itself (| autocmd! custom_keyword).

Besides, you want to complete WORDS whose characters should be any printable character except a whitespace. According to this chart, it seems the first printable character is the exclamation mark ! and the last is the tilde ~. Thus, the string of printable characters could be obtained with the following code:

join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)'))

Which is the string passed as an argument to CustomCompletion() inside the {rhs} of the previous mapping whose {lhs} is <C-X><C-N>.


Note that you could reuse the CustomCompletion() function to change 'isk' in any way.
For example, if you wanted to autocomplete tag names containing a colon, you could install the following mapping:

inoremap <C-X><C-]> <C-O>:call <SID>CustomCompletion(':')<CR><C-X><C-]>

The general syntax would be:

inoremap {lhs} <C-O>:call <SID>CustomCompletion('characters_to_add_to_isk')<CR>{lhs}

Where {lhs} is the keys you hit to trigger the completion (<C-X><C-N>, <C-X><C-]>, ...).

I asked a similar question a few weeks ago (how to complete a tag name containing a colon) and here was the answer.


Your question is slightly different as you don't want to temporarily add a colon to the value of 'iskeyword' but all characters which can appear inside a WORD.

Here's a different version of the original answer which redefines the behavior of <C-X><C-N> to autocomplete WORDS instead of words:

inoremap <C-X><C-N> <C-O>:call <SID>CustomKeyword(join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)'), ''))<CR><C-X><C-N>

function! s:CustomKeyword(chars) abort
    for char in split(a:chars, '\zs')
        execute 'setlocal iskeyword+=' . char2nr(char)
    endfor
    augroup custom_keyword
        autocmd!
        autocmd CompleteDone * setlocal iskeyword< | autocmd! custom_keyword
    augroup END
endfunction

The characters inside the string that you pass as an argument to the function CustomKeyword() will be added to the buffer-local value of 'isk', and the autocmd will reset the option once the completion is done by copying the global value into the buffer-local one (setlocal iskeyword<), as well as remove itself (| autocmd! custom_keyword).

Besides, you want to complete WORDS whose characters should be any printable character except a whitespace. According to this chart, it seems the first printable character is the exclamation mark ! and the last is the tilde ~. Thus, the string of printable characters could be obtained with the following code:

join(map(range(char2nr('!'),char2nr('~')), 'nr2char(v:val)'), '')

Which is the string passed as an argument to CustomKeyword() inside the {rhs} of the previous mapping whose {lhs} is <C-X><C-N>.


Note that you could reuse the CustomKeyword() function to change 'isk' in any way.
For example, if you wanted to autocomplete tag names containing a colon, you could install the following mapping:

inoremap <C-X><C-]> <C-O>:call <SID>CustomKeyword(':')<CR><C-X><C-]>

The general syntax would be:

inoremap {lhs} <C-O>:call <SID>CustomKeyword('characters_to_add_to_isk')<CR>{lhs}

Where {lhs} is the keys you hit to trigger the completion (<C-X><C-N>, <C-X><C-]>, ...).

Source Link
saginaw
  • 6.9k
  • 2
  • 27
  • 47
Loading