alazarte

[home]

Vim navigation

There are different ways to navigate through files, or through a single file, using vim. In this note I describe a few of them, and will update it as I remember different ones. I often use the word "view" here to describe the lines being shown in any open file (which is known as a buffer in vim, and in other editors too).

Quickly scrolling through a file

What I usually do to get a sense of the file I have opened is to scroll through it, not minding (or having less control) of where the cursor will drop. For this I use ^d and ^u (that's ctrl+d and ctrl+u). This moves the cursor half a page down or upwards, and moves the view if the file is larger than the view.

In the case I know what am I looking for exactly, I think a quick search is fast enough. For example, looking for the main function in a go file with /func main. From there, to go back to where I was before there's ^o. ^o and ^i moves the cursor backwards and forwards the jump list.

The "jumplist" is a list of cursor positions, and a new position is added to that list after a cursor movement (but not any movement, like j or k won't add a new position to that list for example). I usually use this after a search.

Line jumping using marks

This is probably a bit advance, but really useful once you get the hang of it. Vim can bookmark lines in a file by using "marks". After creating a mark you can navigate in the same file to another location and simply jump back to the mark, not minding where you are or what's in the jumplist. Say I'm going back-and-forth between my main function and some other one declaration. I can bookmark the line where I declare the main function with mf, and then continue to add things to this same file in some other place, and quickly jump back to my main function with 'f. mf creates a new mark called f in the line where my cursor is, I can use any letter within this range [a-z]. Numbers are reserved, and uppercase letters are "global" marks.

The marks I explained are local, meaning are only available in the current file. I can close the file without issues, and the marks will remain there; and also I can create marks in another file using the same letter. "Global" marks exists too, these are available everywhere, but two files can't have the same letter. For example, I want to keep my vimrc file close for changing settings; what I did was going to the file and pressing mV. Now I have my vimrc available by pressing 'V from any file, I can teak something there and reload the configs by running :so% (meaning :source %, or source this file), and go back to the file I was working on with ^o.

Line jumping using relative numbers

There's another way which I used for a long time: relative numbers. I stopped using these because it was the only mean of movement that I knew in vim, but it was enough anyway. Try opening a file with more than 10 lines at least, and run :set nu rnu. Now move a few lines up and down with jk, that should give you a hint of what relative number is. If you can view the line, you can jump directly to it by pressing [number][direction]. For example, there's a function that is 12 lines up from where my cursor is, I know that is 12 lines exactly because relative numbers tells me that, and by simply pressing 12k I'm there.

Moving the cursor or the view

These are really useful: if I want change something close to the bottom of the view (no the end of the file, but the lines in the screen) I can press L in normal mode and navigate from there. L is for Last line, then there's H for the Hfirst line and M for Middle line.

Those actions go nicely with some z commands, like zz to center the cursor by moving the view, zb to move the cursor to the bottom, zt to the top.

Conclusion

I don't have a specific workflow with these movement actions that I described. I just know them, and use them when I feel like it. Don't know how else to explain it...