In Normal Mode these are the keys you will use to navigate. Notice I always mention Normal Mode, so it's always best to be in Normal Mode as often as you can.
h | left |
j | down |
k | up |
l | right |
This might be difficult to adjust to at first. Within a few weeks of usage, I started to love it. I no longer have to think about going up, down, left, or right. It just happens. Muscle memory!
Tip: The best way to learn Vim navigation is to use it. Chrome's Vimium plugin is the best way to get used to this navigation.
Vim allows you to concatenate multiple commands together. Let's say you want to jump to 10 lines down, you use 10j. To jump 10 lines up, use 10k.
You can also go into Command Mode and enter a line number. Your cursor will jump to that line number. For example, to go to line 1, enter :1
gg navigates you to top of the document.
G navigates to the bottom of the document.
The scroll bar bites the dust with these 2 commands!
Shift + } : jump down one paragraph.
Shift + { : jump up one paragraph.
This is easy and convenient for jumping between methods or paragraphs of text.
0 to jump to beginning of a line
$ to go the end of line
w to navigate to next word. For example, 2w will navigate two (2) words forward.
b to navigate backward. For example, 2b will navigate two (2) words back.
These navigations are super useful when navigating through code.
I never thought this was "a thing" with other editors, and I never thought of using it. My repertoire was go to the end of line hit 'Enter,' and start typing or hit 'Home' and 'Enter' to create a line above.
Well in Vim, you just press o to create a new line below no matter where you are in the line. It puts you in Insert Mode so you can type. Use capital O if you want to create a line above.
I bet you're exhausted! Take a break, have some coffee, and come back for some Vim goodness. If Vim were karate, you'd be a yellow belt once you mastered navigation.
Click above load demo |
Managing real estate, aka screen space, is really important. If you are working from home, you may not have the luxury of additional monitors like you do in an office. Fear not! Vim may look like a terminal, but it has powerful features packed in it.
Lets do a split!
In command mode enter vsp for a vertical split
In command mode enter sp to do a horizontal split.
Jumping between Splits
Ctrl + w and then h,j,k,l, depending on where you want to go. By now you know what keys to use to go up, down, left, and right. If not, it's okay. We all fumble once in a while.
Are you a tabs person and don't enjoy splitting screens?
Go to Command Mode and type tabedit to create a new tab. Ta da. You have a new tab!
How do I cycle through tabs?
Type gt.
How do I go back a tab?
Type gT (Notice the capitalization)
Folds is a modern IDE feature used to collapse a piece of code.
Go to the method you want to fold. Type zf10j to fold 10 lines down, and you have a fold.
Type zo to open a fold.
Folds works in visual mode too! If you are visual person, select a region and type zf.
Splits demo. Click above |
Tabs demo. Click above |
Folds demo. Click above |
Editing is what developers do most of the time. We love to delete code more than refactor. We like to make minor nits here and there to make the code better. Sometime the lucky ones get to write a lot of green field code. Vim helps you with all of this.
Undo is really important. For example, you might be in Insert Mode and type a command that is used in Normal Mode. Suddenly you're cringing. What did I just do? Don't worry.
Go back to Normal Mode and press u. Keep pressing u to undo as far back as you want to go.
Redo is Ctrl + r in Normal Mode.
You will be surprised by the speed of undo and redo and the lengths to which Vim remembers your edits.
In Normal Mode you can press yy to copy a line and then use p to paste it some place else.
Now let's use some navigation tools we learned earlier. Let's copy two (2) lines. 2yy. Then press p. Wasn't that easy?
If you prefer Visual Mode, or if you want to see what you are copying, then enter Visual Mode by pressing v in Normal Mode. Select the text to copy and press y. Navigate to your favorite spot and hit p to paste.
Fantastic Copying
Copying within braces.
There is no such thing as fantastic copying in Vim, but I named it so because I feel it is awesome. You have method body which you need to copy between braces. void foo{ // some bar voodoo }
Navigate to the any position inside the braces and type following in normal mode: yi{
This can be read in plain English as "yank in {"
Navigate to the destination where you need to paste and press p
Isn't that amazing?
Copying within round brackets
Some times you want to copy all the parameters inside a round bracket. Try yi(
Copying a sentence is yis. Copying a paragraph is yip.
I was a happy man the day I learned this.
Copying undo redo with vim. Click above for demo |
Conclusion:
If you made all the way down here, give a pat on your back. As I said before, if Vim were karate you'd now be an Orange belt with more skills in your repertoire. You are really determined to learn something new, so I would like to congratulate you and hear from you. I hope you learned something interesting about Vim. I hope to see you again when I publish Part 2 where we will dive into more interesting topics. Please leave your comments, suggestions, questions and hit that "Like" button if you liked this post.
Comments
Post a Comment