0

I am making my life easier by mapping shortcuts to long DocBook XML structures in insert mode, for example

map! <unique> <localleader>ct <callout arearefs="">>

is there a way to prepend count to the mapping in insert mode? Assuming , is my <localleader>, if I want to insert the whole <table> structure with ,tb, i could easily insert let's say 20 rows (,tr) by typing something like:

20,tr
3
  • I am not sure about how to do it in insert mode, but I can show you how to do it in normal mode.
    – padawin
    Commented May 17, 2019 at 12:46
  • I don't think I fully understand what you are trying to do, but it seems like something that could be easily accomplished with a macro. qq to start recording your macro, q to stop recording, then 20@q to replay the macro 20 times. :help recording
    – Tumbler41
    Commented May 17, 2019 at 14:04
  • From what I understood, he wants to have a command which, when called with a count number, does something count times, but not the whole thing (in this case create a single HTML table with count rows in it.
    – padawin
    Commented May 17, 2019 at 14:06

1 Answer 1

1

If I understood correctly (cf comments under the question), the following might help you:

function! CreateTable(count)
    " Note, the append creates the table from the end to beginning
    call append(line('.'), "</table>")
    call append(line('.'), repeat(["<tr></tr>"], a:count))
    call append(line('.'), "<table>")
endfunc
command -range -nargs=0 CreateTable call CreateTable()

And with a mapping (in normal mode) for it:

nnoremap <localleader>tr :<C-U>call CreateTable(v:count1)<CR>

<C-U> cancels the count, to call the function once only (I think this is the good help page: :h c_CTRL-U).

v:count1 in the last count typed (:help v:count1).

2
  • What is the line('.') part?
    – Moberg
    Commented May 20, 2020 at 8:36
  • 1
    it returns the line number where your cursor is. Cf :help line
    – padawin
    Commented May 25, 2020 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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