I have, for many years, used Vim. Until today, I've not found the need for writing my own functions. I found myself faced with adding some features to existing code, and in so doing needed to maintain a pattern. To make life easier, I wrote
function! CompConst()
let constName = toupper(input("Compiler Constant: "))
let defBlock = ['#define ' . constName . '_SUFFIX_1',
\'#define ' . constName . '_SUFFIX_2',
\'#define ' . constName . '_SUFFIX_3',
\'#define ' . constName . '_SUFFIX_4']
call append(line('.'), defBlock)
endfunction
imap <C-d> <C-o>:call CompConst()<cr>
The function works marvelously except ... the cursor is left above the lines just inserted. Yes, this does make sense because I'm using append()
. I looked through the help and insert()
seems to apply to lists. I haven't yet found a function for inserting text at the current line number. Googling for insert functions and insert just reveals a whole lot of hits for the Ex commands. I even tried a bit of sleight-of-hand and used call append('.-1', defBlock)
which did the same as what's above.
If it matters, I'm using Vim 8.0 with patches 1-1453.
put =defBlock