Search code examples
vimneovimabbreviation

How to insert abbreviate hashtag-starting lines in Neovim


I have the following line in my ftplugin/cpp.vim folder:

inorea <buffer> #def #define

And it keeps on popping errors whenever I enter Neovim. The entire code goes like this:

if (&ft != 'cpp')
    finish
endif

inorea <buffer> #inc<lt> #include <lt>><left>
inorea <buffer> #inc #include ""<left>
inorea <buffer> #def #define
nnoremap <buffer> #inc<lt> i#include <lt>><left>
nnoremap <buffer> #inc" i#include ""<left>
nnoremap <buffer> #def<Space> i#define 
inoremap <buffer> /* /**/<left><left>

" For Single-File Codes : Save, Compile, and Run
nnoremap <buffer> <F5> :w<CR>:!g++ % -o %<.exe<CR><CR>:!%<.exe<CR>
nnoremap <buffer> <C-F5> :w<CR>:!g++ % -o %<.exe<CR><CR>:tabe<CR>:terminal<CR>3j$a

Funnily enough, line 5 doesn't pop an error, and they do work in .cpp files. Any help would be appreciated.


Solution

  • How abbreviations work depends on the 'iskeyword' option. See :h Abbreviations.

    The left-hand side of the abbreviation can either be only keyword characters (full-id), non-keyword characters ending with a keyword character (end-id), or any characters ending with a non-keyword-characters (non-id).

    If the # character is not part of 'iskeyword', then #def is none of the three types and thus :ab #def ... is invalid. Because that's keyword characters starting with a non-keyword character.

    The format of 'iskeyword' is rather cryptic (see :h 'isfname'), but by default it's set to @,48-57,_,192-255. It doesn't include #.

    Quick test:

    :set isk=a-z
    :ab #def foo
    E474: Invalid argument
    :set isk=a-z,#
    :ab #def foo
    

    So, you could put set iskeyword+=# before your abbreviations, but that might lead to other issues as well, since the characters from 'iskeyword' are used for a lot of things.