increment character under cursor

413 views
Skip to first unread message

JP Lew

unread,
Sep 21, 2012, 3:27:23 PM9/21/12
to vim...@googlegroups.com
Hi:
I was wondering if it was possible for Vim to increment the character I have selected under the cursor, rather than the whole number. For example, let's say I have a line like this:

margin-bottom: 30px;

I want it to be 40px instead. If I place my cursor over the '3' then hit CTRL-A, I get '31' and the cursor moves to the '1'. I want to increment the individual digit. (I realize in this case I could simply enter 'r4', but I'm curious about this method as it could be applicable for macros as well).

A second, and related, question, is it possible to get CTRL-A & CTRL-X to move in bigger jumps? Like rather than 1 unit a time, it'd be nice to hit CTRL-SHIFT-A to move 10 units at a time. I tried this mapping, but it doesn't seem to work:

nmap <S-C-A> 10<C-A>

Thanks. -JP

Tim Chase

unread,
Sep 21, 2012, 3:57:48 PM9/21/12
to vim...@googlegroups.com, JP Lew
On 09/21/12 14:27, JP Lew wrote:
> I was wondering if it was possible for Vim to increment the character I
> have selected under the cursor, rather than the whole number.

Not in-built. You could do something like

:nnoremap <f4> s<c-r>=<c-r>"+1<cr><esc>

to just operate on the one character under the cursor.

> A second, and related, question, is it possible to get CTRL-A & CTRL-X to
> move in bigger jumps? Like rather than 1 unit a time, it'd be nice to hit
> CTRL-SHIFT-A to move 10 units at a time.

Well, you can prefix ^A/^X with a count to change the increment, so
in your case, you can use

10^A

as you mention/map below (though in this case, the "r4" solution is
shorter)

> I tried this mapping, but it
> doesn't seem to work:
>
> nmap <S-C-A> 10<C-A>

Part of the problem may have been that vim doesn't discern
control+{char} from control+shift+{char}, but you have the right
idea. You just have to map it to something vim recognizes :-)

-tim


JP Lew

unread,
Sep 21, 2012, 5:21:15 PM9/21/12
to Tim Chase, vim...@googlegroups.com

Not in-built.  You could do something like

 :nnoremap <f4> s<c-r>=<c-r>"+1<cr><esc>

I see. You're substituting the character, entering expression mode, pasting it back then adding 1. How clever.
 
Part of the problem may have been that vim doesn't discern
control+{char} from control+shift+{char}, but you have the right
idea.  You just have to map it to something vim recognizes :-)


great, thanks Tim, I'll try something else.

Another question. How do I enter that mode where Vim outputs the key combination I just pressed? For example if I type CTRL + a on my keyboard it spits out "<C-a>". I remember it was really useful for writing mappings.

Tony Mechelynck

unread,
Sep 21, 2012, 7:32:23 PM9/21/12
to vim...@googlegroups.com, JP Lew, Tim Chase
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php

In Insert mode, Ctrl-K followed by a "special key" will insert the <>
representation of that key. If followed by a "normal" (printable) key,
OTOH, it waits for the second key in a digraph.

See :help i_CTRL-K


Best regards,
Tony.
--
"Last night, I came home and realized that everything in my apartment
had been stolen and replaced with an exact duplicate. I told this to
my friend -- he said, `Do I know you?'"
-- Steven Wright

Reply all
Reply to author
Forward
0 new messages