{"id":204,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:17","modified_gmt":"2020-08-22T20:26:17","slug":"this-is-the-page-title-toplevel-38","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=204","title":{"rendered":"Moving Around in vi"},"content":{"rendered":"\n<title>Moving Around in vi<\/title>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to start inserting text at your current cursor position?\" \/>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to append text starting at your current cursor position?\" \/>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to move forward to the beginning of the next word.\" \/>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to move back to the beginning of the previous word.\" \/>\n<concept id=\"\" description=\"In 'vi' you use uppercase letters when you want move 'real' words. \" \/>\n<p>\nMost editing and movement commands are single letters and are almost always the first letter of what they do. For example, to insert text\nat your current <glossary>cursor<\/glossary>\nposition, press i. To append text, press <keyinput>a<\/keyinput>. To move forward to the beginning of the next word, press <keyinput>w<\/keyinput>. To move back to the beginning of the previous word, press <keyinput>b<\/keyinput>.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to append text starting at the end of the line?\" \/>\n<p>\nThe capital letter of each command has a similar behavior. Use <keyinput>I<\/keyinput> to insert at the beginning of a line. Use <keyinput>A<\/keyinput> to start the append from the end of the line. To move &#8220;real&#8221; words, use <keyinput>W<\/keyinput> to move forward and <keyinput>B<\/keyinput>to move back.\n<\/p>\n<concept id=\"\" description=\"In 'vi' real words are those terminated by whitespaces (space, tab, newline).\" \/>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' \" \/>\n<p>\nReal words are those terminated by whitespaces (space, tab, newline). Assume we wanted to move across the\nphrase &#8216;static-free bag&#8217;. If we start on the &#8216;s&#8217;, pressing <keyinput>w<\/keyinput>, will move me to the &#8216;-&#8216;. Pressing <keyinput>w<\/keyinput> again, we move to the <keyinput>f<\/keyinput> and then to the <keyinput>b<\/keyinput>. If we are on the &#8216;s&#8217; and press <keyinput>W<\/keyinput>, we jump immediately to the &#8216;b&#8217;. That is, to the next &#8220;real&#8221; word.\n<\/p>\n<question id=\"487\" text=\"In vi, what four letter keys are used to move single lines or single characters?\" \/>\n<concept id=\"\" description=\"If your keyboard does not let you use the arrow keys to move in 'vi' you can use the letterr 'h-j-k-l'.\" \/>\n<p>\nMovement in <command>vi<\/command> is also accomplished in other ways. Depending\non your <glossary>terminal<\/glossary>\ntype, you can use the traditional method of arrow keys to move within the file. If vi doesn&#8217;t like your terminal\ntype, you can use the keys <keyinput>h-j-k-l<\/keyinput>.\nIf we want to move to the left we press <keyinput>h<\/keyinput>.\nIf you think about it, this make sense since <keyinput>h<\/keyinput> is on the left end of these four characters. To move right, press <keyinput>l<\/keyinput>. Again, this makes sense as the <keyinput>l<\/keyinput> is on the right end.\n<\/p>\n<p>\nMovement up and down is not quite as intuitive. One of the two remaining characters (j and k) will move us up\nand the other will move us down. But which one moves in which direction? Unfortunately, I don&#8217;t have a very\nsophisticated way of remembering. If you look at the two letters physically, maybe it helps. If you imagine a line running through the middle of these characters, then you see that j hangs down below that line. Therefore, use j to move down. On the other hand, k sticks up above the middle, so we use k to move up. However, in\nmost cases, the arrow keys will work, so you won&#8217;t need to remember. But it is nice to know them, as you\ncan then leave your fingers on the keyboard.\n<\/p>\n<p>\nAs I mentioned, some keyboard types will allow you to use the arrow keys. However, you might be surprised\nby their behavior in input mode. This is especially true if you are used to a word processor where the arrow\nand other movement keys are the same all the time. The problem lies in the fact that most keyboards actually\nsend more than one character to indicate something like a left-arrow or page-up key. The first of these is\nnormally an escape (Esc). When you press one of these characters in input mode, the Esc is interpreted as\nyour wish to leave input mode.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to move to the beginning of the line?\" \/>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to move to the end of the line?\" \/>\n<p>\nIf we want to move to the first character on a line, we press <keyinput>0<\/keyinput> (zero) or <keyinput>^<\/keyinput>. To move to the last character, press <keyinput>$<\/keyinput>.\nNow, these are not intuitive. However, if you think back to our discussion on regular expressions, you&#8217;ll\nremember that ^ (caret) represents the beginning of a line and $ (dollar sign) represents the end of a line.\nAlthough, these two characters do not necessarily have an intuitive logic, they do fit in with other commands\nand programs that you find on a Linux system.\n<\/p>\n<question id=\"486\" text=\"When in vi command mode, what key combination would 'jump' four words forward?\" \/>\n<question id=\"488\" text=\"In vi, what key is used to move to the start of the line?\" \/>\n<question id=\"489\" text=\"In vi, what key combination would you use to move back 12 words?\" \/>\n<question id=\"490\" text=\"In vi, what key combination would you use to move to line 79?\" \/>\n<question id=\"491\" text=\"In vi, what key combination would you use to move down 14 lines?\" \/>\n<question id=\"492\" text=\"In vi, what key is used to move to the end of the line?\" \/>\n<concept id=\"\" description=\"In vi you can combine numbers and movement keys.\" \/>\n<p>\nWe can also take advantage of the fact that vi can count as well as combine movement with this ability to count.\nBy pressing a number before the movement command, vi will behave as if we had pressed the movement key\nthat many times. For example, <keyinput>4w<\/keyinput> will move us forward four words or <keyinput>6j<\/keyinput> will move us six lines down.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to move to a specific line?\" \/>\n<question id=\"\" type=\"mc\" text=\"In vi, what key combination would you use to move to\n line 42?\" \/>\n<question id=\"\" type=\"mc\" text=\"In vi, what key combination would you use to move head 42 lines?\" \/>\n<p>\nIf we want to move to a particular line we input the number and G. So, to move to line 43, we would press 42G, kind of\nlike 42-Go! If instead of <keyinput>G<\/keyinput> we press <keyinput>Enter<\/keyinput>, we would move ahead that many lines. For example, if we were on line 85, pressing 42 and Enter would put us on line 127. (No, you don&#8217;t have to count lines; vi can display them for you, as we&#8217;ll see later.)\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What key would you press in 'vi' to delete everything from your current line to line 83?\" \/>\n<p>\nAs you might have guessed, we can also use these commands in conjunction with the movement keys (all except\n<keyinput>Ctrl-u<\/keyinput> and <keyinput>Ctrl-d<\/keyinput>). So, to delete everything from your current location to line 83, we would input <keyinput>d83G<\/keyinput>.\n(Note that delete begins with d.) Or, to change everything from the current <glossary>cursor<\/glossary>\nposition down 12 lines, we would input <keyinput>c12+<\/keyinput> or press <keyinput>c12<\/keyinput> Enter.\n<\/p>\n<p>\n","protected":false},"excerpt":{"rendered":"<p>Moving Around in vi Most editing and movement commands are single letters and are almost always the first letter of what they do. For example, to insert text at your current cursor position, press i. To append text, press a. &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=204\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-204","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/204\/revisions"}],"predecessor-version":[{"id":634,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/204\/revisions\/634"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}