How do I change the cursor position from within a Vim function?

You can use the cursor() function to position the cursor.

        call cursor(lnum, col)

Alternatively, use the setpos() function:

        call setpos('.', [bufnum, lnum, col, off])

which set’s the cursor in the buffer bufnum to line lnum, column col and offset for ‘virtualedit’. You can use the getpos() function, to return a list with these values, that can then be fed back to the setpos() function. If you want to save and restore the viewpoint on a window, use the winsaveview() and winrestview() function calls. You can also use the following command to change the cursor position:

        exe "normal! " . lnum . "G" . col . "|"

Read free Software Algorithms and Computer Science Books by CodeAhoy Learn.

Introduction to Recursion and Backtracking

Sorting Algorithms

Introduction to C Programming Language

And More...

Speak Your Mind