0

I have the following function that I'm calling to insert blank lines under the cursor:

function InsertBlankLine()

    call LogOutput('*** START ***', "DEBUG", {'line': expand('<sflnum>'), 'func': expand('<sfile>')[9:]})
    " a: args will give us the following by default: -- see `:h: a:var`
    "   '0': '000': 'firstline': 109, 'lastline': 108 
    "   '0' is the number of extra arguments, '000' is the list of those extra args
    let data = a:
    call LogOutput("Data: " . string(data), "DEBUG", {'line': expand('<sflnum>'), 'func': expand('<sfile>')[9:]})

    " Access a dict value get(dict, 'value')
    let first_line = get(a:, 'firstline')

    " Insert a new line
    call LogOutput('Executing normal o', "DEBUG", {'line': expand('<sflnum>'), 'func': expand('<sfile>')[9:]})
    execute "normal! o"

    " Go back to the first line
    let cmd = printf("normal! %sG", first_line)
    call LogOutput('Executing cmd: ' . cmd, "DEBUG", {'line': expand('<sflnum>'), 'func': expand('<sfile>')[9:]})
    execute cmd

endfunction

And I can call it like this with a shortcut:

nnoremap T :call InsertBlankLine()<CR>

So that I can type in 10T and it will insert ten blank lines. This all works. However, it spits out a lot of input which I want to suppress using silent. However, I'm not sure how to pass :silent to the function as it will usually start with a number. For example, if I type in 10T, this is the command it runs:

:.,.+9call InsertBlankLine()

So how would I get that to execute silently (without removing the LogOutput's ? enter image description here

4
  • You're missing a : in :call...
    – filbranden
    Jun 15 '20 at 3:54
  • @filbranden sorry, that was entered in on the cmd line. It works normally, for example :.,.+9call InsertBlankLine(). However, when I try and add a silent to the cmd it's erring.
    – David542
    Jun 15 '20 at 4:51
  • There's a difference between :.,.+9silent call ... (shouldn't work) and :silent .,.+9call ... (I'm guessing this one works?) In any case, please edit your question to match what you actually tried to do.
    – filbranden
    Jun 15 '20 at 4:55
  • @filbranden updated.
    – David542
    Jun 15 '20 at 5:11
5

Use append(), it's silent and it doesn't move the cursor -- As I said in your review question, I found using :normal & co convoluted to alter buffer content. Vim functions have less side effects.

:nnoremap <silent> T :<c-u>call append('.', '')<cr>

It can even be used to insert multiple lines: https://vi.stackexchange.com/a/9720/626

:nnoremap <silent> T :<c-u>call append('.', repeat([''], v:count1))<cr>

Among the other things you were possibly searching, there is

1
  • 1
    THANK YOU for the :h :map-<silent>. You're expert🎉 Finally I got an answer although I asked Google an XY problem.
    – Weekend
    Sep 8 '20 at 7:53

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.