Navigate a line in Vim Normal mode
Unfortunately, I won't be writing on my robotics pet project for a while, because I'll be heading over to the University of Wisconsin-Madison for my Master's in Computer Science, and in preparation for the move I've dismantled TOHNKATSU. Ah well.
Nevertheless, I'll be sharing some small tips about Vim today, which to be honest I feel is one of the few things I'm qualified to teach.
Today's topic is on navigating a line in Vim's Normal mode. That's the mode that you enter when you hit <Esc>. Many of you probably know that /my_search_term jumps forward to a search term, ?my_search_term is the equivalent in the backwards direction, 14G brings you to line 14, etc, but really, we spend a disproportionate amount of time just navigating a single line in Vim. That's why it's important and that's why we're talking about it today!
Jump to a character
A bad brown fox jumps over my steely dan!
^ ^ ^ ^ ^
2Fb Fb cursor fe 2fe
Nevertheless, I'll be sharing some small tips about Vim today, which to be honest I feel is one of the few things I'm qualified to teach.
Today's topic is on navigating a line in Vim's Normal mode. That's the mode that you enter when you hit <Esc>. Many of you probably know that /my_search_term jumps forward to a search term, ?my_search_term is the equivalent in the backwards direction, 14G brings you to line 14, etc, but really, we spend a disproportionate amount of time just navigating a single line in Vim. That's why it's important and that's why we're talking about it today!
Jump to a character
A bad brown fox jumps over my steely dan!
^ ^ ^ ^ ^
2Fb Fb cursor fe 2fe
The command is f<char> to jump rightwards and F<char> to jump leftwards. As with many Vim commands, prepend a number n to execute the command n times, e.g. 2f<char> is equivalent to executing f<char> twice. Super useful, especially for small edits in a word or a short phrase.
Jump before a character
She's really fun to hang out with (if you know what I mean).
^ ^ ^
T' cursor t)
T' cursor t)
This is pretty much the same as above, except the cursor jumps to the character just before your search character. The command is t<char> to jump rightwards and T<char> to jump leftwards. Maybe not as universally useful as f/F, but it's really handy if you're a programmer and there are all these mathematical operators (+-*/) that you want to jump next to - not directly to, but next to.
Wrap up
Just like most commands in Vim, you'll almost certainly need to consciously incorporate these into your editing/programming habits, but it'll become second-nature soon enough. Remember to combine these with the standard commands like d for delete and y for yank, e.g. dfe deletes all characters up to and including the next e, and yt) copies all characters up to but excluding the next closing bracket.
I'm sure these will be useful to you. Enjoy!