Command to preview line in buffer just like the numb.nvim plugin in neovim

Before making the switch from neovim, I had a very sparse configuration, with only a few plugins, trying to delete as many plugins as possible.
Though, GitHub - nacro90/numb.nvim: Peek lines just when you intend was never discarded because it brought me so much value for as simple as it is.
I tried to recreate it in kakoune :blush:

declare-option -hidden -docstring "line to go back too in case the jump-to-line prompt is aborted" \
    int beforelinejump

define-command -hidden -docstring "start a prompt to preview and jump around a file by line number" \
jump-to-line %{
    set-option buffer beforelinejump %val{cursor_line}
	evaluate-commands %{
        prompt ":" nop \
            -on-abort %{
                edit %val{bufname} %opt{beforelinejump}
            } \
            -on-change %{
                try %{ edit %val{bufname} %val{text} }
            }
	}
}

map -docstring "interactively jump to line" global goto ':' '<esc>: jump-to-line<ret>'

rec.1548.3UxH

Let me know if you can think of a more efficient way to jump.
This is my first “plugin” for kakoune, still trying to figure it out.

Edit: added the -on-abort switch to reset your position in the file if you cancel the prompt.



Please note that Orcan Tiryakioğlu (nacro90) does not endorse (AFAIK) this snippet.

3 Likes

Goodness, that is handy. I may have to add that to my kakrc.

There does exist another way to do this, though: in normal mode, you can type the line you wish to go to, followed by ‘g’ (without the quotes) to jump to a line.

the line you wish to go to, followed by ‘g’

Yes, as in vim, but it’s not very practical, and you don’t have a preview of where you’re jumping to. I wanted to gain back this numb.nvim ability :blush:

I thought of using execute-keys "%val{text}g" in the on-change callback, but it didn’t work, and just typed the values in the prompt message (or I am just too dumb and couldn’t figure it out).

A bit different approach which doesn’t require declare-option:

define-command -override -hidden -docstring 'start a prompt to preview and jump around a file by line number' \
jump-to-line %{
        prompt ':' nop \
            -on-abort %{
                evaluate-commands %sh{
                    if [ -n "$kak_text" ]; then
                    	printf 'execute-keys 2<c-o>vv\n'
                    fi
                }
            } \
            -on-change %{
                try %{ edit %val{bufname} %val{text} }
            }
}

It also goes back to line and column position in case of abort.

Update: check for edge case: jump back only if text was integer.

define-command -override -hidden jump-to-line %{
    prompt ':' nop \
        -on-abort %{
            evaluate-commands %sh{
                input=$(printf '%d' "$kak_text")
                if [ $input -gt 0 -o "$kak_text" = '0' ]; then
                	printf 'execute-keys 2<c-o>vv\n'
                fi
            }
        } \
        -on-change %{
            try %{ edit %val{bufname} %val{text} }
        }
}
1 Like

Nice ! Although you jump back using , only two times, so if someone jump more than 1 time this won’t bring you back to the beginning, no ?

Yes, good catch. Your solution is more robust, however to restore column position we may utilize int-list:

declare-option -hidden int-list jump_back_line
define-command -override -hidden jump-to-line %{
    set-option global jump_back_line %val{cursor_line} %val{cursor_char_column}
    prompt ':' nop \
        -on-abort %{
            edit %val{bufname} %opt{jump_back_line}
        } \
        -on-change %{
            try %{ edit %val{bufname} %val{text} }
        }
}
2 Likes

Oooooh, I didn’t know about int-list ! That’s good and far prettier than my solution involving shell substring

declare-option -hidden -docstring "line to go back too in case the jump-to-line prompt is aborted" \
   str beforelinejump

define-command -hidden -docstring "start a prompt to preview and jump around a file by line number" \
jump-to-line %{
   set-option buffer beforelinejump "%val{cursor_line}:%val{cursor_column}"
   evaluate-commands %{
       prompt ":" nop \
           -on-abort %{
               evaluate-commands %sh{
                   line="${kak_opt_beforelinejump%%:*}"
                   col="${kak_opt_beforelinejump##*:}"
                   printf "edit %s %d %d" "${kak_bufname}" "${line}" "${col}"
               }
           } \
           -on-change %{
               try %{ edit %val{bufname} %val{text} }
           }
   }
}

Will definitely implement your solution, thanks a lot @vbauerster

I’ve put this snippet in a git repository. I’ve removed the shell script call thanks to @alexherbo2
https://git.sr.ht/~nasmevka/small.kak