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:

  • 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 May 17 '19 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 May 17 '19 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 May 17 '19 at 14:06

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>")
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).

  • What is the line('.') part? – Moberg May 20 '20 at 8:36
  • 1
    it returns the line number where your cursor is. Cf :help line – padawin May 25 '20 at 6: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.