1

I have created a helper function that will insert a number of blank lines and a keyboard shortcut of T to execute it. This way I can do something like 5T to insert five blank lines with the cursor unchanged (it's a bit different than 5o, though mainly to practice writing a function that I could use in files).

It looks something like this:

function InsertBlankLine() abort

    " a: args will give us the following by default: -- see `:h: a:var`
    "   '0': 1, '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 \<C-o>S"

    " 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

    " Check if there's any text on the first line, and if there is, go to the first blank line below it.
    let first_line_text = trim(getline(first_line))
    call LogOutput('First Line text: ' . first_line_text ,"DEBUG", {'line': expand('<sflnum>'), 'func': expand('<sfile>')[9:]})
    if first_line_text != ''
        normal! j
    endif

endfunction

nnoremap T :call InsertBlankLine()<CR>

Where should I put this function? Here are a few things to note:

  • It imports another function LogOutput.
  • It's a bit large, so maybe I take it out of the vimrc (which is where it currently is)?
  • My vim directory is at ~/.vim. In there I have both an autoload and after and plugin folder.

What is the suggested practice for a function like this? (Note the function is just for me, I don't care about sharing it or making a plugin or anything -- it's quite trivial in the scope of things and more for me to practice with vimscript).

3

Normally, the function goes to "autoload", while "nnoremap" into either "vimrc", or "after/ftplugin".

| improve this answer | |
  • If the mapping/command if meant to be distributed, I'd say in {rtp}/plugin/{pluginfilename}.vim – Luc Hermitte Jun 15 at 9:55
  • @LucHermitte Yes, but the OP says it's not a plugin. – Matt Jun 15 at 9:57
  • Indeed. My mistake, I've missed the note at the end ^^' – Luc Hermitte Jun 15 at 10:10

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.