0

I'm trying to use the feature g<C-A> in a vimscript function to renumber a visual selection-ed list, but it never does the renumbering.

function! ReNumber() 
     execute "normal! g$\<C-A>"
endfunction 

I've tried incorporating the notion of range as suggested by this and this.

function! ReNumber() range
     execute a:firstline .. ',' .. a:lastline .. "normal! g$\<C-A>"
endfunction   

I have tried using a remapping for g<C-A> and it does work, but unsure if I could use the remapped key in my vimscript function

vnoremap <C-H> g<C-A>

How do I use g<C-A> in my vimscript function, when I call the function on a visually selected chunk of text?

5
  • 1
    Could you tell us why your are adding $? An example of transformation before and after would help to understand what you want to achieve :-) Apr 2 at 18:52
  • You ask how to use g<C-a> but you are trying to use g$<C-a>.
    – romainl
    Apr 2 at 19:19
  • I thought that was how to write out <C-a> in a norm command based on this.
    – wxz
    Apr 2 at 20:50
  • 1
    The reddit post gives the $ as an example for operating on the end of the line. When you’re not sure, try :help.
    – D. Ben Knoble
    Apr 2 at 21:18
  • Oh, you're right. I saw it late last night and in my desperation to get this to work, thought it was necessary for referencing a control key. Thanks for pointing that out!
    – wxz
    Apr 2 at 21:31

1 Answer 1

1

To renumber till the end of the buffer I would do:

:normal! VGg^A

Where ^A is inserted using Ctrl va Or

:execute "normal! VGg\<C-a>"

If you want to trigger it from an existing selection you could do:

vnoremap <F6> g<C-a>

Or using the normal command:

vnoremap <F6> :<C-u>normal! gvg<C-v><C-a><cr>
  • the <C-u> get rid of the '<,'>
  • the gv restore the selection
  • the <C-v><C-a> insert the ^A character in the command line.

g<C-A> is only working in visual mode.

It increments the first number of each line in the selection by a number (1 for the first line, 2 for the second line, ...).

If a count is provided the first number of the first line is incremented by count, the first number of the second line by 2 count, ...

More information with :help v_g_CTRL-A.

5
  • These worked for me for renumbering an entire file (hence the VG). But when I tried them on a range that I had highlighted, after removing VG, the top command you suggested did nothing and the bottom command said "no range allowed". How would I make this work on a specific range that I've highlighted? Thank you!
    – wxz
    Apr 2 at 20:56
  • The range should be a range of lines. If you already selected the line `execute "normal! gvg\<C-a>" should do the trick. Apr 2 at 21:16
  • 1
    Since OP is confused about key notation, it’s worth explaining why the execute string uses double quotes and escapes for the key, and maybe even why that’s not needed with map commands because they interpret key notation first.
    – D. Ben Knoble
    Apr 2 at 21:20
  • @VivianDeSmedt running execute "normal! gvg\<C-a>" in my vimscript function worked!! Would you modify your answer to reflect this and I'll accept your answer? Thank you!
    – wxz
    Apr 2 at 21:32
  • 1
    For anyone running into this problem, I'll also add that when a chunk of text is visually highlighted and I wanted to use g<C-a> in ex mode, I needed to run :execute "normal! gvg\<C-a>" making sure to remove the range brackets '<,'> from the command, otherwise I got the "range not allowed" error.
    – wxz
    Apr 2 at 21:38

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.