3

I have a script called lorem.sh which takes in the number of paragraphs(first argument) and number of lines in each paragraph(second argument) and prints random text of that format to standard out. I turned this into a vim key binding so I could enter this text into my file with :

nmap <leader>l :read ! ~/lorem.sh 1 100<CR>

This works but I was wondering if it would be possible to add a count to this command in the same way you can specify a count to other commands like Ndd to delete N lines below the current one. If I could have something like N<leader>l working, I could enter N paragraphs of text at once.

Thanks !

3

Based on :help v:count:

                    *v:count* *count-variable*
v:count     The count given for the last Normal mode command.  Can be used
        to get the count before a mapping.  Read-only.  Example: >
    :map _x :<C-U>echo "the count is " . v:count<CR>
<       Note: The <C-U> is required to remove the line range that you
        get when typing ':' after a count.
        When there are two counts, as in "3d2w", they are multiplied,
        just like what happens in the command, "d6w" for the example.
        Also used for evaluating the 'formatexpr' option.
        "count" also works, for backwards compatibility, unless
        |scriptversion| is 3 or higher.

                    *v:count1* *count1-variable*
v:count1    Just like "v:count", but defaults to one when no count is
        used.

You want

nnoremap <leader>l :execute 'read !~/lorem.sh' v:count1 '100'<CR>

(Notice I am using nnoremap; this is a good habit.)

2
  • 1
    your specific response did not work but i did some more digging in the part of the help file that you linked and I found exactly what I was looking for : the concatenation . operator and the exe command. This one worked for me in the end : noremap <leader>l :exe "read ! ~/lorem.sh " . "v:count1" . " 100<CR>" – First User Oct 7 '20 at 19:48
  • 2
    @FirstUser doh! I even wrote a QA about exec and variables – D. Ben Knoble Oct 7 '20 at 19:50

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.