Vim: Navigation

Snipcademy 2020

Disclaimer: Snipcademy is supported by your purchases of our recommended products through Amazon Services LLC and the ads posted on our website. All products listed are thoroughly researched, curated and reviewed so that viewers are given the best recommendations. Thank you and happy browsing!

01. Navigating controls (h, j, k, l, ESC, [0-9]) Hello! Click this region to activate me and start learning about Vim! Let's jump right to it! Vim is a "modal editor", meaning it has different modes. When you open vim, it opens in COMMAND mode. If you ever find Vim in a different mode (or if your controls are acting wonky), simply press ESC to re-activate COMMAND mode. Great! We'll start with basic navigation. To move around in Vim, press: ^ k < h l > j v l - move right h - move left j - move down k - move up Time to practice! Try going right (l) and left (h). Now try moving down (j)! Let's move back up (k). Stray away from the [[arrow keys]]! It may seem easier to use the arrow keys, but those two seconds you take to move over to use them can actually add up to a lot on the course of your programming career! Let's try navigating through this haiku: Yay, we are learning how to navigate with Vim This is so much fun! We can make moving around more efficient using a numerical modifier. Simply precede the command with an value [0-9]. For example, pressing '4h' moves 4 characters to the left. Review Questions: 1) Why are we learning Vim? (scroll down for answer) Solutions: 2) It's fast, lightweight, and available on almost every computer. 02. Navigating words (web, WEB) To navigate through words in a line: w - navigate to the beginning of the next word W - same as w, beginning of the next WORD, which ignores punctuations. What's the difference between a word and WORD? A word includes punctuations, while a WORD does not. Try navigating through the following: 1-800-555-1234 My sister's boyfriend's mother. // Now with some simple java code for (int i = 0; i < 10; i++) { System.out.println("I love vim's navigation tools."); } To navigate to the end of the word/WORD: e - to the last letter of the next word E - same as e, ignoring punctuations # now navigate through a simply Python function def square(x): return x ** 2 Not to go back a word: b - navigate back to the beginning of a word B - same as b, ignoring punctuations <!-- HTML for web developers ;) --> <p>Hello I am a block paragraph. <b>Don't mess with me</b>.</p> Review Questions: 1) How do you activate COMMAND mode? 2) How many words is 192.168.1.1? How many WORDs is it? 3) How would you move to the fourth word forward? How about the third WORD back? Solutions 1) ESC for command mode. 2) 7 words, 1 WORD. 3) 4w, 3B. 03. Navigating lines ($, 0, ^, g_, -, +) We can navigate much more efficiently by navigating through lines. Use the following keys to move to: $ - end of line 0 - beginning of line Notice that some lines are really long, and thus wrap around. To see which lines are wrapped around, simply look at the line number to the left. Try navigating to the end of the line in this sentence. In the case where the line begins with with white space, use: ^ - first non-white space at the beginning of line g_ - first non-white space at end of line (this doesn't work in this interface, but it should when using external Vim). # Try using the above commands in a simple python code, which loves to use white space. def fib(n): if n < 2: return n return fib(n-2) + fib(n-1) You may also navigate through lines with indentations with the - and + keys: - - moves up to the first non-whitespace character + - moves down to the first non-whitespace character Notice that for "+" you have to press shift, then the "=" sign. In Vim terms, that's just too much work. What we can use instead is the [return] key. Try navigating through the first letters of this tricky paragraph! Challenge! Review Questions: 1) NavigatE Through This Line HittinG Only The CapitalizeD letteR. 2) How would you ignore punctuations while navigating through words? Solutions 1) (starting from the 1) Wewwwwewwwee 2) Use the upper-case counterparts - WEB. 04. Navigating sentences and paragraphs ({, } (braces), (, ) (parantheses)) Now let's see how to navigate through sentences or big blocks of code! Use these keys to wade through sentences in a paragraph: ( - start of sentence ) - end of sentence (Again, apologies, but these don't work in-browser. They will work in external vim, however.) This is a sample sentence. This is another sample sentence. Does a question mark complete the a sentence? How about an exclamation point? Yes! Yes they all do! Isn't this so exciting? Yes. Yes it is. To find the end of a sentence, Vim looks for "?", ".", or "!". To skip entire paragraphs, use: { - start of paragraph } - end of paragraph Vim considers any block of text surrounded with blank lines as a paragraph. /** * A class that demonstrates the usefulness of vim * keyboard shortcuts. Simply navigate this class * file using the { and } keys. * * @author CodeSnippetsAcademy * @version 1.0, December 11, 2014 * */ public class Car { public String color; // color of our car public int speed; // How fast our car is moving (mph) /** Parameterized constructor * @param aColor - the color of a car * @param aSpeed - the speed of the car */ public Car(String aColor, int aSpeed) { color = aColor; speed = aSpeed; } /** Default constructor */ public Car() { color = "blue"; speed = "50"; /** set the color of the car */ public void setColor(String newColor) { color = newColor; } /** set the speed of the car */ public void setSpeed(int newSpeed) { speed = newSpeed; } /** apply brakes */ public void applyBrake(int decrement) { speed -= decrement; } /** speed up */ public void speedUp(int increment) { speed += increment; } } Review Questions 1) How would you navigate through lines of code, where there are indentations? 2) What's the difference between using "0" and "^"? Solution: 1) You can use "-" and "+" or "-" and "[return]". Using the [return] is more efficient since you don't have to hold the [shift] key. 2) "0" is to the beginning of the line, while "^" is to the first non-white space of a line. 05. Navigating the screen (H, L, M, ctrl+e, ctrl+y) Great progress! Your navigational skills are getting great! :-) Now let's look at how to navigating through your screen. To navigate around the screen: H - to the top of the screen 4H moves 4 lines below the top L - to the bottom of the screen 3L moves 3 lines above the last line M - to midscreen Let's just place some filler text here to extend this file length so you can have more space. Now how about scrolling along the screen? Use: ctrl+e - move the screen one line up ctrl+y - move the screen one line down Review Questions 1) Navigate through the following uppercase letters: Hello my name is Bobby. I don't know how to use white space properly. So sometimes I use four spaces, sometimes two, It really just depends on my mood. 2) Navigate from the beginning to the end of each sentence above. Answers: 1) Use "-" and the [return] key 2) Use "^" and "$" 06. Navigating large files (gg, G, ctrl+b, ctrl+f, ctrl+d, ctrl+u, z) Let's now learn how to navigate large files. For quick movement in large files, use: gg - move to the beginning of file G - move to the end of file 5G moves to the 5th line For scrolling, we use the "ctrl" key: ctrl+f - page down ctrl+b - page up ctrl+d - half a page down ctrl+u - half a page up We can also reposition our screen with the "z" key: z[return] - Move current line to the top of screen z. - move current line to center of screen z- - move current line to bottom of screen *** 1) Mouse cursor HERE and hit "z[return]"! *** Lorem ipsum dolor sit amet, consectetur adipiscing elit. Teneo, inquit, finem illi videri nihil dolere. Mihi enim satis est, ipsis non satis. Non ego tecum iam ita iocabor, ut isdem his de rebus, cum L. Ego vero isti, inquam, permitto. Duo Reges: constructio interrete. Faceres tu quidem, Torquate, haec omnia; Et quidem saepe quaerimus verbum Latinum par Graeco et quod idem valeat; Invidiosum nomen est, infame, suspectum. Aliter enim nosmet ipsos nosse non possumus. Quae hic rei publicae vulnera inponebat, eadem ille sanabat. Nihil enim hoc differt. Illa videamus, quae a te de amicitia dicta sunt. Quis non odit sordidos, vanos, leves, futtiles? *** 2) Now cursor over HERE and try "z."! *** *** 3) Now mouse HERE and press "G"! *** Pauca mutat vel plura sane; Legimus tamen Diogenem, Antipatrum, Mnesarchum, Panaetium, multos alios in primisque familiarem nostrum Posidonium. Uterque enim summo bono fruitur, id est voluptate. Istam voluptatem perpetuam quis potest praestare sapienti? Quacumque enim ingredimur, in aliqua historia vestigium ponimus. Quis est tam dissimile homini. Nonne videmus quanta perturbatio rerum omnium consequatur, quanta confusio? Ergo et avarus erit, sed finite, et adulter, verum habebit modum, et luxuriosus eodem modo. Nihilne est in his rebus, quod dignum libero aut indignum esse ducamus? Ita cum ea volunt retinere, quae superiori sententiae conveniunt, in Aristonem incidunt; Pauca mutat vel plura sane; Legimus tamen Diogenem, Antipatrum, Mnesarchum, Panaetium, multos alios in primisque familiarem nostrum Posidonium. Uterque enim summo bono fruitur, id est voluptate. Istam voluptatem perpetuam quis potest praestare sapienti? Quacumque enim ingredimur, in aliqua historia vestigium ponimus. Quis est tam dissimile homini. Nonne videmus quanta perturbatio rerum omnium consequatur, quanta confusio? Ergo et avarus erit, sed finite, et adulter, verum habebit modum, et luxuriosus eodem modo. Nihilne est in his rebus, quod dignum libero aut indignum esse ducamus? Ita cum ea volunt retinere, quae superiori sententiae conveniunt, in Aristonem incidunt; Nos cum te, M. Sin laboramus, quis est, qui alienae modum statuat industriae? Hanc ergo intuens debet institutum illud quasi signum absolvere. Occultum facinus esse potuerit, gaudebit; Sed fac ista esse non inportuna; Sapiens autem semper beatus est et est aliquando in dolore; Satisne vobis videor pro meo iure in vestris auribus commentatus? Hoc Hieronymus summum bonum esse dixit. Non minor, inquit, voluptas percipitur ex vilissimis rebus quam ex pretiosissimis. Ergo, inquit, tibi Q. Quid enim me prohiberet Epicureum esse, si probarem, quae ille diceret? Eodem modo is enim tibi nemo dabit, quod, expetendum sit, id esse laudabile. Egone non intellego, quid sit don Graece, Latine voluptas? Nescio quo modo praetervolavit oratio. Ab hoc autem quaedam non melius quam veteres, quaedam omnino relicta. Sed quid attinet de rebus tam apertis plura requirere? Nos cum te, M. Sin laboramus, quis est, qui alienae modum statuat industriae? Hanc ergo intuens debet institutum illud quasi signum absolvere. Occultum facinus esse potuerit, gaudebit; Sed fac ista esse non inportuna; Sapiens autem semper beatus est et est aliquando in dolore; Satisne vobis videor pro meo iure in vestris auribus commentatus? Hoc Hieronymus summum bonum esse dixit. Non minor, inquit, voluptas percipitur ex vilissimis rebus quam ex pretiosissimis. Ergo, inquit, tibi Q. Quid enim me prohiberet Epicureum esse, si probarem, quae ille diceret? Eodem modo is enim tibi nemo dabit, quod, expetendum sit, id esse laudabile. Egone non intellego, quid sit don Graece, Latine voluptas? Nescio quo modo praetervolavit oratio. Ab hoc autem quaedam non melius quam veteres, quaedam omnino relicta. Sed quid attinet de rebus tam apertis plura requirere? *** 4) Mouse over HERE and try "z-"! *** Congratulations! You have finished learning about navigation. Let's move onto how we can edit within the Vim editor! Review Question: 1) Place your cursor to the middle of this screen. 2) Scroll up/down one line at a time. Solutions: 1) "M" 2) ctrl+y, ctrl+e