1

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.

4
  • 1
    Try adding snippets, youtu.be/XA2WjJbmmoM
    – Mani
    May 7, 2020 at 23:59
  • 1
    Welcome to Vi and Vim! Glad you were able to solve your problem!
    – D. Ben Knoble
    May 8, 2020 at 0:18
  • @Mani +1 to the recommendation to use snippets! It's definitely the right tool for the job!
    – filbranden
    May 8, 2020 at 1:08
  • 3
    Simply put =defBlock
    – Matt
    May 8, 2020 at 3:48

1 Answer 1

2

Have you considered modifying the function to move the cursor after the list insertion? This worked in my Vim (v8.1.7777)

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)
    norm! 4jo
endfunction
8
  • 1
    Apologies, I initially misread the imap for an nmap. I have since modified my response with a working function. May 7, 2020 at 23:49
  • 1
    Thank you for the warm welcome, @D. Ben Knoble♦! Wouldn't it be +4 instead of -4 for the ex command since we're trying to move the cursor down 4 lines? May 8, 2020 at 0:48
  • 1
    @filbranden /facepalm yep.
    – D. Ben Knoble
    May 8, 2020 at 1:08
  • 1
    Welcome @ThatsWhatSheCoded!
    – filbranden
    May 8, 2020 at 1:09
  • 1
    Thanks. That seems to work nicely. I hadn't considered using that command in this context. I use extensively in with :g commands, but as I mentioned, before yesterday, I haven't done anything with Vim Script (other than tweak my rc file). May 8, 2020 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.