- Lab
- A Cloud Guru
Navigating in Vim
Being able to quickly and accurately move to where you need to be in Vim is an important skill, and one that only takes a little practice before you'll be zooming to right where you need to be! In this hands-on lab, we will start with the basics of moving around in a buffer or file's contents, getting to top/bottom, directly to a given line, or move around in large movements such as by screen or buffer. We'll then move around by paragraph, on a line, and even down to moving around word by word, both forward and backward for all movements.
Path Info
Table of Contents
-
Challenge
Move Around in Vim, Change the Cursor Line Positioning, and Navigate to the Buffer Boundaries
-
Open the Vim editor:
vim
-
Create a working file:
:help quotes
:w ~/quotesdemo.txt
-
When the file is written to disk, quit Help:
:q
-
Open the
~/quotesdemo.txt
file in a buffer::e ~/quotesdemo.txt
-
Navigate to the top of the file:
gg
-
Navigate to the end of the file to see how many lines long it is:
G
Note: Pressing Ctrl+G will also display the number of lines in a buffer/file.
-
Navigate to line 100 of the file:
100G
-
Noting the percentage shown on the far lower right of the screen, navigate to 50% of the way down the file:
50%
-
To see if the top line of the screen is the start of a paragraph, press
H
and thenk
to move to the top line and move the display up one line.Note: If the cursor won't go to the top of the screen, use the following to turn OFF the
scrolloff
option::set scrolloff=0
-
Reposition your cursor to the middle line of the screen by pressing
M
, then to the bottom line of the screen withL
. -
Navigate to line 17:
17G
-
Press
O
(upper-case "o") to open a new line above the one that begins:Coming with a very GUI mindset...
-
While in Insert Mode, try the various navigation commands you have tried so far, then use the dedicated PgUp, PgDn, Home, End and cursor keys, which is all Insert Mode really allows for navigation.
-
Quit the demonstration file without saving changes:
ESC
:q!
-
-
Challenge
Affect Line Display, Show the Control Characters in Your Files, Navigate on Lines and by Words and Characters
-
Edit the demonstration file you created in Task 1:
vim ~/quotesdemo.txt
-
Navigate to the top of the file:
gg
-
Turn on the line numbers option:
:set number
-
Navigate to the first line of the fourth paragraph:
}}}}j
-
Your cursor should be on the first character of the line that begins:
Vim is so much better than vi...
-
Now move your cursor to the end of the line by pressing the
$
character.Note: Normally the end of a line and the end of a paragraph are the same thing, as a paragraph usually is a sentence or more ending in a Line Feed.
-
Note that you are not going to the closing parantheses following the word
Australia
, as you would expect to. Look at the line numbers for a clue. -
Set the displaying of un-printable characters on the screen:
:set list
-
Note that the
$
orLF
(Line Feed) character is at the end of each of the lines, indicating the use of a manualENTER
to set the length of the line. This and line numbers tell the whole story. -
Place your cursor on the top line of the paragraph that begins with:
Coming with a very GUI mindset....
-
Turn the multiple physical lines in the paragraph into a proper paragraph by pressing the
J
character until there are no remaining$
characters in betweenComing with
and(Jose Fonseca)
. -
Now use the
$
character to go to the end of the "line" you have just created out of five individual lines, then press the^
character to go to the beginning of the new paragraph/line. -
Use the
w
character to move forward through the paragraph for 10 words, noticing how it treats punctuation as a word. Then use5w
to move forward five words at a time. -
Navigate backwards to the beginning of the paragraph using
B
, noticing how it treats the punctuation. -
Look up why this happens:
:help B
-
Make sure to read the definitions of what a
word
and aWORD
are and the difference. Also note similar information aboutb
,w
, andW
. -
Exit Help:
:q
-
Position your cursor at the beginning of the paragraph that starts with
Coming with a very GUI....
and then usee
to move to the end of the word, then several more, until you reach punctuation, noting where it stops, then use^
to then repeat those steps withE
, noting the (subtle) difference that makes. -
Return to the top of the file:
[[
-
Quit and don't save the demonstration file:
:q!
-
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.