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

David542
  • 2,445
  • 14
  • 50
  • You're missing a : in :call... – filbranden Jun 15 '20 at 03: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 04: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 04:55
  • @filbranden updated. – David542 Jun 15 '20 at 05:11

1 Answers1

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

Luc Hermitte
  • 17,351
  • 1
  • 33
  • 49
  • 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 08 '20 at 07:53