4

I have a set of 10 line numbers, n1, n2 ,..., n10 in a code of 1000+ lines. I need to jump from one line to another. My normal practice is to do n1G, n2G... till the set is complete. It takes about 5 seconds to see if the specific line is error free.

I would like to automate this by jumping into the lines one after the other after 5 seconds stay on each line.

Is it possible for vim to jump to a series of predefined line numbers after x seconds?

5

There is :sleep command (:h sleep or :h gs). Using that command it's possible to loop over given array of line numbers:

let g:line_list = [ 5, 10, 15 ]
let g:line_list_sleep = 5 " in seconds, add m suffix to go miliseconds

function LinePlaylist()
  for x in g:line_list
    execute "normal " . x . "G"
    redraw!
    echom getline('.')
    execute "sleep " . g:line_list_sleep
  endfor
endfunc

This will go through all lines defined in g:line_list array. You can also set the interval using g:line_list_sleep. Line should be visible both by cursor position and it will be echo'ed (you can remove echo if you don't like/need it). You can always break the loop with Ctrlc. Adding above excerpt to your .vimrc makes it possible to use :call LinePlaylist() to run it.

  • Works just fine :) ... – Sathyam Jan 30 '17 at 12:11
4

If you have vim8 you might want to play with the new timers feature. The difference with @grodzik solution is that timers are asynchronous meaning that you can still use Vim will the cursor is moving.

The idea is to add the following to your .vimrc:

function! GoToLines()
    " Indexes of the lines to read
    let lines = [1, 2, 3, 4, 5, 10]
    " Time between to changes of lines in miliseconds
    let delay = 5000
    " Will be used to delay each line change
    let repeat = 0

    " Go to each lines
    for line in lines
        call timer_start(repeat * delay, function('GoToLineTimer', [line]))
        let repeat += 1
    endfor
endfunction

First a function which contains the list of the lines to visit (you can modify that so it take the list as an argument). For each line contained in the list the function will create a timer with a delay. This timer will call the following function which only changes the line of the cursor:

function! GoToLineTimer(line, timer)
    execute 'normal! ' .a:line . 'G'
endfunction

You can then simply use :call GoToLines() or create a mapping which will call the function for you.

If you want to stop the timers and keep your cursor on the current line you can use :call timer_stopall() to stop all of the timers.

Of course this solution will only work with vim8 and the +timers feature

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.