Skip to content

Increment and Decrement

Screwtapello edited this page Aug 15, 2018 · 2 revisions

Vim will increment or decrement the number under the cursor when you hit CtrlA or CtrlX (respectively), but Kakoune does not implement that behaviour by default. Here's a script that will set up something similar:

define-command \
    -params 1 \
    -hidden \
    -docstring "Modify each selected number by the given amount." \
    modify-numbers \
%{
    try %{
        execute-keys -draft \
            ":set-register a %arg{1}" <ret> \
            s [+-]?0[Xx][0-9a-f]+ <ret> \
            | %{/bin/sh -c 'read value; printf 0x%x\\n $(( $kak_main_reg_a + $value ))'} <ret>
    } catch %{
        execute-keys -draft \
            ":set-register a %arg{1}" <ret> \
            s [+-]?0[Xx][0-9A-Fa-f]+ <ret> \
            | %{/bin/sh -c 'read value; printf 0x%X\\n $(( $kak_main_reg_a + $value ))'} <ret>
    } catch %{
        execute-keys -draft \
            ":set-register a %arg{1}" <ret> \
            s [+-]?0[0-7]+ <ret> \
            | %{/bin/sh -c 'read value; printf 0%o\\n $(( $kak_main_reg_a + $value ))'} <ret>
    } catch %{
        execute-keys -draft \
            ":set-register a %arg{1}" <ret> \
            s ([+-]?)0[Oo]?([0-7]+) <ret> \
            | %{/bin/sh -c 'printf 0o%o\\n $(( $kak_main_reg_a + } <c-r>1 0 <c-r>2 %{ ))'} <ret>
    } catch %{
        execute-keys -draft \
            ":set-register a %arg{1}" <ret> \
            s [+-]?[0-9]+ <ret> \
            | %{/bin/sh -c 'read value; printf %d\\n $(( $kak_main_reg_a + $value ))'} <ret>
    }
}

map \
    -docstring "Increment numbers" \
    global normal <c-a> \
    ":modify-numbers 1<ret>"

map \
    -docstring "Decrement numbers" \
    global normal <c-x> \
    ":modify-numbers -1<ret>"
Clone this wiki locally