I've made a lot of videos about Vim in the past, but I wanted to make another introductory video to those that want to the learn the basic Vim keybindings and editing commands. I'm doing this because many viewers haven't watched my older videos and probably will never find them.
Your support is very much appreciated. Thanks, guys!
do you spend a lot of time in a text
editor are you a writer or a scripter or
a programmer whatever it happens to be
if you spend a lot of time at a text
editor typing you owe it to yourself to
learn vim and Vim is not that hard today
I'm going to cover the absolute Basics
as far as the Vim key bindings and the
basic Vim motions so to get started
learning Vim the very first thing you
need to do is make sure Vim is installed
on your system so I'm on an arch based
Linux distribution so I could install
themm using Pac-Man sudu Pacman capital
S Vim or if you wanted to you could
install neovim which is a more modern
version of Vim or if you happen to be a
emac user you could just use emac and
just enable evil mode inside emac and
it's a a Vim emulation that is very good
as far as like 99.9% of everything that
is in Vim is also in emac evil mode so
you could also do this in emac as well
once you have Vim or neovim installed
you can open a terminal and run the
command vimtutor and this is a little
help guide this is basically your very
first Vim lesson the Vim tutor is this
document that you could go through in
about 20 to 30 minutes and it's going to
cover all of your basic Vim motion
commands basic key bindings basic
editing commands so it's a pretty
straightforward document to go through
you can just read it line by line it's
going to tell you some commands to enter
just hit those commands but honestly I
this themm tutor document it's kind of
plain looking for one thing it's not a
programming document it's not written in
a programming language there's no syntax
highlighting so for me I think I'm going
to play in a different file I think what
I'm going to do for purposes of this
video we're just going to play with my
bash RC so I'm going to type vm. bashrc
here in my home directory and this is my
bash config now the very first thing you
need to learn when you're using Vim is
how to open a document with vim and how
to quit out of Vim well opening a
document in Vim is easy when you're in
the terminal type vim and then the path
to some document that you want to edit
or if you're creating a new document the
path to a new document you want to
create right that's easy but now that
you've got this document open how do you
quit well you type colon Q to quit so
colon is the command mode within VM
anytime you type colon it expects you to
then give it a command such as Q for
quit you could actually type the full
word quit but Q is a shorthand for quit
if I do that it quits me out of Vim if I
up Arrow here in the terminal and go
back to Vim bashrc I'm back in this
document now if you want to save changes
to a document and say I've edited a few
lines of this document well to save
those changes you want to do colon W for
right again you could do colon in the
full word right but colon W is the
shorthand for right if you want to save
and quit you could do a colon WQ for
right and then quit so if I do colon WQ
you know you know I had made no changes
but had I made some changes that would
have worked now say I make some changes
and then try to do a colon q without
saving well I'm going to get a warning
that I haven't saved the document but if
you want to quit without saving the
document say you made some changes but
then you changed your mind it's like h i
don't want those changes Sav let me quit
without saving well you can do a colon Q
exclamation point and that will quit
without saving any changes so let me get
back into my b RC let's go over some of
the basics with Vim the very first thing
you need to know is that Vim is a modal
text editor by modal I mean it has modes
by default when you open a a document in
Vim it is in normal mode and the reason
it's called normal mode is this is the
mode you're going to be in most of the
time normal mode allows you to move
around the document with your motion
Keys the basic motion keys are HJ k l so
H is lift J is down so if I start typing
J I go down K is up L is right and just
to verify I'll also do H to go back left
so h j KL again that is left down up
right now your arrow keys do also work
when you're in normal mode so you could
use the arrow keys to just you know move
up and down and left and right that
works just fine too many Vim purus will
tell you not to use your arrow keys when
you're using Vim because it kind of you
know confuses you a little bit which it
does on a standard keyboard because hjkl
are those four arrow motion keys right
but they're in a straight line across
but on your standard 110 key keyboard
your arrow keys are typically in like a
a t-shaped upside down T right so you
have the up Arrow key above the down
arrow key and then left and right out to
the side you know it's a very different
kind of uh layout on the keyboard your
arrow keys compared to hjkl and mentally
it will confuse you if you try to go
between the two luckily I don't have
this problem because I am using these uh
split keyboards from Zsa for the most
part on all of my machines and I have
these set up to where my arrow keys are
set up with left down up right all in a
straight line so it's very easy for me
to either use hjkl or I just move my
hand down two lines and the arrow keys
are also set up in the same order as
hjkl so mentally it's I don't have to
learn two different sets of Arrow
motions right they're very similar but
if you're learning Vim on a standard
keyboard try to avoid using the arrow
keys for a while because again you
really need to get used to hjkl so as I
said normal mode you're typically in
normal mode when you're in normal mode
you're not editing text now if you want
to get into insert mode which is the
mode where you can then start typing on
a keyboard and add text hit I for insert
mode and now you could start typing
something and to get back into normal
mode all you have to do is hit escape on
the keyboard and now now you're back in
normal mode so hjk andl will work you
know as motion keys right where if you
were in insert mode hjkl would actually
type the characters hjk L on the uh
point of the cursor now I didn't
actually want to add that new line to my
config because this is my actual bash RC
my bash config file so I'm going to type
youu on the keyboard to undo so anytime
you're in normal mode you to undo if I
do control r that is a redo and you will
use these constantly when you're making
changes in a document using Vim so
remember U to undo control R to redo so
let me u a couple of times to make sure
that I haven't made any changes here I'm
back at the oldest change you can see
here at the bottom Echo area already at
the oldest change so we have normal mode
which you begin in you have I for insert
mode Escape gets you back into normal
mode there's also visual mode and
there's a couple of different visual
modes you need to know you need to know
shift V for visual line mode now that
I'm in visual line mode when I go down
with a j I'm selecting the entire line
you can see I'm highlighting these
entire lines to copy or delete or
whatever it is I'm doing to these lines
manipulating the text of these lines in
some way let me escape to get out of
that so that was visual line mode
there's also control V which is visual
block mode now this instead of selecting
the whole line it just selects whatever
I select with the uh cursor point so if
I go over a little bit so if I actually
use l to go over right you can see I'm
selecting a block of text text I'm not
selecting the whole lines I'm just
selecting that highlighted area so those
are the four basic modes you need to
know normal mode insert mode and then
you have the visual line mode visual
block mode and then of course colon is
its own mode as well that's command mode
where you enter commands such as colon Q
to quit colon W to write colon WQ for
write and quit while hjkl are nice for
moving around by character most of the
time you're wanting to move around in
much bigger chunks than single
characters well there's a couple of
things you can do to quickly move around
a document the first thing you can do is
you can always do GG to get to the top
of the document right you could also do
capital G shift G to get to the bottom
of the document the last line of the
document you see I'm on line 256 if I
want to go back to line one GG if I want
to go back to the last line 256 capital
G and if I have a line number in M an
exact line number I want to go to for
example 10 capital G that takes me to
line 10 if I want to go to line 50 50
capital G takes me to line 50 you can
also use the HJ KL motions in
conjunction with numbers to move for
example up and down by a specific number
so for example maybe I want to go up 10
lines I could do 10 K and I just moved
up 10 lines right if I do 10 J I move
back down 10 lines to where I was
previously and of course I could also do
something like 20 L to move over 20
characters to the right 20h would move
me back to the left 20 characters so let
me GG to get to the top of the document
let's move down to this line here now
there's a couple of other very Nifty
motions you'll use all the time you'll
use the zero and the dollar sign on the
keyboard in normal mode what dollar sign
is dollar sign takes you to the end of
the line right dollar sign end of the
line zero takes you to the beginning of
the line dollar sign takes you to the
end zero takes you back to the beginning
now zero and dollar sign are are useful
for the most part but sometimes you'll
have weird lines for example I'm going
to add some blank characters to the
beginning and the end of this line now
if I do zero to go to the beginning of
the line that's great but now I'm on
that blank space and maybe every line
starts with I don't know two spaces four
spaces you'll often encounter this in
certain programming documents and you
don't want to go to that space you
really wanted to go to where the actual
line really starts the first nonblank
character well you can do this as well
what you can do instead of doing zero to
go to that first character use the
carrot symbol to go to the first
nonblank character and similarly instead
of doing the dollar sign to go to the
very end of the line if if it's a blank
character what you could do is you could
do Gore to go to the last nonblank
character so in summary remember zero is
the beginning of the line dollar signs
the end of the line carrot symbol is the
first nonblank character Gore is the
last non-blank character me you to undo
those changes another thing you can do
to move around quickly in Vim is you can
use the braces you can use the uh
opening brace and the closing brace the
closing brace moves you to the next
paragraph the opening brace moves you to
the previous paragraph So if I do the
closing brace we will go down by
paragraph if I do the opening brace we
will go back up by paragraph if I want
to move down by a half page I can do
contrl D to move down by a half page you
can see my cursor is always in the same
spot I'm moving down by a half Page
worth of lines there if I want to go
back up by a half page I can do control
U so contrl D for down contrl U for up
that'll and if I wanted to go all the
way to the top GG or all the way to the
bottom remember capital G shift G right
GG first line capital G last line
sometimes you want to search for a
specific string of characters in a
document you can do that with the slash
just hit slash and now type the string
you want to search for I want to search
for x port and you can see it finds
export in several places here and if I
hit enter it goes to the first instance
and then if I hit in for next it will go
to the next instance of export n again
will take me to the next export if I do
capital N it will take me backwards to
the previous export capital N to the
previous export now if you know you're
going to search for string backwards
instead of doing the slash name of
string right you could also do a dollar
sign name of string so let's do dollar
sign export and now what this does when
I type in for next it just begins us
going backwards and if I do capital N it
actually goes reverse so you know you
have two different ways to search the
slash is the normal way the dollar sign
name of string is the backwards way
another thing you can do is you can move
forwards and backwards by word you will
often do this instead of just using hjkl
remember H&L move uh left and right by
one character but if you just want to
move forward and backwards by word you
can do W for word so if I just type w I
move forward to the first character of
each word now W also includes special
characters such as the period and the
semicolon you know all of those kind of
punctuation marks if I do B for back I
move back by word I go to the first
character of the word except we're going
in reverse and again it's going to
include special characters such as that
semicolon and the period so w forward B
for back if you want to go forward and
backwards by word but instead of the
first character of the word you want to
be on the last character of the word you
can use e for end of word so e will go
forward by end of word and GE will go
backwards by the last character of each
word and just like w and B included the
special characters e and G also include
the special characters such as the
punctation marks there are versions of
these same commands that do ignore the
punctuation mark so instead of using WB
e g e what you could do is you could use
a capitalized version of those for
example capital W shift W will take me
to the next word but when I get to
config period here and I do capital W it
skips the period right we we're going to
ignore those special punctuation marks
so no longer would I stop on semicolon
here with the next W it is skip to the
beginning of the next word just in this
case and capital B does the same thing
in Reverse it's going to ignore that
semic
right we're going to ignore the period
with it as well capital E does the same
thing we go to the end of the word the
end of the word is the period on config
period right so before it would uh the
regular e if I hit it on config it goes
to G and then the next e goes to the
period right so G it sees G and period
as the end of two separate words where
capital E I hit zero on the keyboard and
do capital E you know it's going to see
the period as the end of the word config
period and to uh do GE in reverse and
have it also act the same way what you
could do let me go forward a word with
capital E now let me do g capital E to
go back and you see when it goes back it
treats the period as the end of this
word if I do g capital E again the next
time it jumps back to the last character
of this this word here bash so WB e g e
and then you have capital W capital b
capital e g capital E depending on
whether you want to ignore the special
punctuation marks or not for those of
you that are programmers one of the
really cool things Vim does is it really
helps you manage keeping track of your
parentheses braces and brackets because
if you want to quickly navigate in
between a pair of braces or a pair of
brackets or a pair of parentheses all
you need to do is I'm on the opening
bracket in this case on this line right
here if I'm in normal mode all I have to
do is hit the percent key and I go to
the closing bracket so basically
parentheses will always take you to the
matching pair for that parentheses brace
or bracket if I hit parentheses again I
go back to the opening bracket so you
know I can quickly switch between the
opening and closing of that pair and
just to verify that this actually works
with parentheses braces and brackets I'm
going to get into insert mode and what
I'm going to do let's do opening braces
parentheses brackets and then
some stuff here and then let's do
closing brackets parentheses braces then
I'll escape to get into normal mode zero
to get at the beginning of the line if I
hit parentheses while I'm on the brace
it will take me to the last brace but
let me do H to move back to a character
to the closing parenthesis and now when
I do parentheses it takes me to the
opening parenthesis if I hit L on the
keyboard to move over write a character
so I'm on the opening brace I hit
percent sign you can see it takes me to
that closing brace now let me you to
undo all of that let's talk about
deleting and copying and pasting because
those are actions you're going to do all
the time in normal mode in Vim let's
first talk about deleting an entire line
you will often delete entire lines and
then go paste them somewhere else for
example maybe I want to move this line
one line down well I would do DD for
delete the entire line DD deletes the
entire line ped paste the line right so
that's how I move that line basically
one spot down really DD and then P what
it does it swaps the two lines right
because I if I undo you can see undo
twice we have this line here that begins
with the hashtag this is a commented
line then I have a empty line right and
if I do DDP I just swap those places is
all that is let me you to undo so DD
deletes an entire line YY copies an
entire line Vim doesn't call copy copy
it calls a copy A Yank so why why just
yank that line copied that entire line
so now if I do p it paste that line on
the next line you to undo now let's
imagine that you want to delete
everything in this document or copy the
entire document well if you want to
delete the entire document let me GG to
go to the very first line we already
know D is delete right and we also know
capital G takes us to the end of the
document takes us to the last line well
what if I do D capital G for delete and
then g go to the end of the document I
just deleted this entire document you to
undo what if I wanted to copy the entire
document you've probably already guessed
why for yank capital G and you can see
256 lines yanked I just copied this
entire document if I hit P right now I
just pasted this entire document so if I
scroll down you will see eventually I'm
going to begin again yeah here's the top
of my bash RC again so now instead of
256 lines I have 512 lines because I
yanked the whole thing I cop the whole
thing right and then pasted it again so
let me you to undo so D capital G delete
the whole document y capital G copy the
whole document and of course those only
work if you start on the first line of
the document what if I was 10 lines down
in the document well first you would
need to GG before you do your D capital
G your y capital G right so if I wanted
to yank the entire document but I wasn't
on the first line I would simply do a GG
y capital G right so you can see how
very quickly you you're you'll get the
hang of these commands these normal mode
commands and it's so powerful especially
once you get the hang of using them all
the time for example swapping lines I I
I do that DDP thing so many times where
I have two lines and I want to swap them
I want to reverse the order of the lines
for example maybe these two lines here
DDP right and I just put those lines in
different places you to undo and while
I'm mostly sticking with the absolute
basics of the Vim motions and the Vim
key Bings I'm not going to really dive
too deeply into command mode other than
then uh colon W for right colon Q to
quit colon WQ for right and quit I will
show you how to yank an entire document
instead of having to do GGY capital G
the entire document you could actually
do this quite a bit easier in command
mode because you could do Colin and then
you could do percent percent in command
mode means the entire document and then
give it a y right behind it for yank and
you can see that achieves the same
effect 256 lines yanked and as you
probably guessed if I did a colon and
then percent D for delete that would
also achieve the same effect for
deleting the entire document now let's
talk a little bit more about pasting a
line let me copy this line here so y y
Yanks that line right it copies that
line I'm going to go up a couple of
lines now if I do a p it's going to
paste that copied line on the next line
but what if I want to paste that line on
the previous line instead of the next
line well it may youd undo and instead
of P if I do shift p it actually pastes
on the previous line so that is how you
paste so typically you're pasting to the
next line but if you ever want to paste
on the previous line capital P paste to
the previous line also earlier I
mentioned insert mode I gets me into
insert mode so I could start typing
something right well what happens if you
want to get into insert mode but not on
the line you're currently on what if you
want to get into insert mode on the next
line or the previous line well on the
next line you just hit o o takes you to
the next line and automatically gets you
into insert mode and if let me escape to
get back into normal mode and if I do
capital O that gets me into insert mode
on the previous line that I was on so
now I could do this is a another line of
course I misspelled all that let me you
to undo so those are very useful insert
mode commands remember I always get you
into insert mode on that line o always
gets you into insert mode on the next
line capital O always gets you into
insert mode on the previous line and one
other thing sometimes you want to get
into insert mode but you want to get
into insert mode at the very end of the
line that you're currently on Capital a
gets you into insert mode at the very
end of the
line Escape let me undo all of that now
rarely are you wan to delete or copy an
entire document more often you're
deleting or copying entire lines but
much more often you're deleting or
copying just words or sometimes even
single characters let's talk about
deleting single characters X on the
keyboard deletes the character you're
currently on so if I hit X right now I
just uncommented this line right the
hashtag that was under the cursor point
it's deleted if I hit X again that space
is gone if I hit X again the capital M
is gone right let me 3 U to undo three
times CU I knew I'd hit x three times
right remember you can always type a
number before a command to run that
command multiple times let's talk about
replacing a character so imagine that
instead of my bash config right here
here I wanted that word to actually be
cache I wanted this to say my cache
config well the cursor is currently on
the B I can do R for replace if I do R
and then do c it just replace the B with
a c so R name of character is a replace
so if I wanted instead of cache I wanted
this to be my cast config so I could do
RT and it just replaced that H which was
under the cursor with a t now let me you
to undo a couple of times if you want to
delete a word type D for delete and W
for word so DW deletes word right DW
again deletes word now let me to you to
undo that and just like deleting a word
is DW yanking a word copying a word is
YW for yank word right if I go back a
character and hit P you can see I did
copy that word so let me undo that so
just a quick refresher DD deletes the
entire line you undo DW deletes a word
you to undo what if I wanted to delete
from this character that I'm currently
on to the end of the line you've
probably already guessed D for delete of
course and then remember dollar sign for
end of line so D dollar sign deletes
everything to the end of the line you to
undo what about delete everything from
the cursor point to the beginning of the
line remember zero is the beginning of
the line D zero right deletes everything
from the cursor point to the beginning
of the line you to undo and of course
that works with yank as well if I go to
the middle of the line here and I do a y
dollar sign I just copied everything
from the cursor point to the end of the
line if in instead I did y0 I copied
everything from the cursor point to the
beginning of the line one of the things
you often want to do when you delete a
word you want to delete a word and
replace it with another word so instead
of doing DW to delete it and then I to
get into insert mode and then type a new
word just do all of that in one command
because there is a command in Vim for
change what change does it deletes
whatever characters or section of text
you tell it to delete and then it
automatically puts you into insert mode
so you can type the replacement so for
word you can do CW for change word it
just deleted that word and now I could
do something like Capital ba for my bash
config and then escape to get back into
normal mode and just like DD deletes an
entire line y y Yanks an entire line CC
changes an entire line it deletes it
puts you into into insert mode so you
can then write a new line now for me I'm
going to escape to get back into normal
mode you to undo a couple of times and
just like we could do uh d dollar sign
d0 y dollar sign y0 you could also do
this with change as well if I go to the
first letter of config here and I do a c
dollar sign for change everything in
this line from the cursor point to the
end of the line you know it puts me into
insert mode and then I could write the
rest of the end of this line let me you
to undo that if I want to change from
the cursor point to the beginning of the
line I would do c0 and it deletes
everything from the precursor point to
the beginning of the line puts me into
insert mode so I can type a new
beginning to this line let me you to
undo now DW CW YW you know all of those
word commands for for example DW deletes
a word right DW would delete that word
let me you to undo but sometimes you're
not exactly on the first character of
the word if you're not on the first
character of the word say I'm on the
second character of the word DW deletes
the word but it left the first character
because the cursor point was after it so
what you want to do if you're in the
middle of a word and you want to delete
the word you have to diw for delete
inside the word so basically if I'm in
the middle of a word DW is the command
to delete the word so and if I same
thing with yank if I'm in the middle of
a word why I W to yank that word because
if I'm in the middle of the word and I
just do YW it's only going to yank where
the cursor point is to the end of that
word sometimes you want to delete or you
want to yank a word and you want to
include the spaces around it so in that
that case what you would do instead of
DW to delete inside word you would D aw
to delete around the word so it deletes
the word and the spaces around it you to
undo that you could also do that with
yank y a w Yanks around the word and
similarly if I was in the middle of this
word not and I did a c i w for change
inside the word I'm changing the word
let me escape you to undo and if I was
in the middle of the word and I did c a
w for change around the word it also
will delete the word and the spaces
around it and put me into insert mode so
I can you know make the necessary change
for those of you programmers if you want
a really neat thing you can do again
with braces brackets and parentheses
what you could do is you could actually
delete everything that is inside a pair
of braces or brackets or parentheses for
example I'm in the middle of these
brackets here this is a line and you can
see it's wrapped in Brackets well if I
delete in side and then the opening
bracket I just deleted everything that
was in that pair of brackets you to undo
if I wanted to delete everything that's
in a pair of parentheses I could again
just go anywhere inside this and do Di
and then the opening parentheses and I
deleted that text also I deleted the
brackets because the brackets were
inside the parentheses you'd undo and
one more time let's try this with the
braces so I'll do Di and then the
opening brace and I deleted uh
everything was inside the brackets and
the parentheses including the brackets
and the parentheses themselves because I
deleted everything inside the braces I
hope that makes sense but if you're a
scripter or programmer obviously these
kinds of commands the delete inside
braces brackets parentheses you're going
to use these all the time the last basic
Vim motions I want to show you are F and
T So f is go forward till the next
character it's basically find the next
character so if I do FB it goes goes
forward to the next instance of that
character B if I do FB again there's not
another B in this line but what about
how about F FF goes to the next F FF
goes to the next F FF goes to that last
F and then FF won't do anything from
that point now let me hit zero on the
key very similar to F is the T command
so F what F did remember if I do FG it
takes me to the next instance of that
character G what T does it takes you to
the next instance of G except instead of
putting the cursor point on the G it
puts the cursor point on the character
in front of the G so think of f as find
find the G and take the cursor point to
it think of t as until with a capital T
until right so go till the G but stop
the cursor point right in front of it so
if I do t g you can see I actually go to
the place right before G TG again well
there's not another G how about t f goes
to the U and stuff and that's as far as
it'll go now also in conjunction with
the F and T commands you can use
semicolon and comma to move forward and
back by the next instance of those
characters so remember FF you know takes
me to the F in config but there's also
two other Fs in this line and what I
could do instead of doing FF again I
could just do the semicolon and it will
take me to the next F semicolon again
will take me to the next F comma would
take me back to the previous F comma
again would take me back to the previous
F and again that works with f it also
works with t if I go to the beginning of
this line and do TF and then the
semicolon again will take me to the next
F but remember T always takes you to the
cursor point on the character right
before that particular character you
were searching for and the comma once
again will take me in Reverse except
when you do it in reverse with a T it's
actually the character right after cuz
again it goes in in reverse all the way
to F but it's not going to stop on F
it's going to go a character right after
it in this case because we're going in
reverse so I think we covered really all
of the basics as far as your basic
motion Keys you how to move around a
document inm the basic editing as far as
deleting copying which is yanking
pasting we even covered a little bit of
command mode but you know really the VM
motion Keys they're not that hard to
learn once you know the absolute basic
Motions like I showed you today I mean
really what I covered on camera on this
one video that's like 95% of everything
you'll do in Vim there's really not much
more to learn there's still some stuff
to learn but you can get by in Vim just
fine if you know exactly what I showed
you today on camera now before I go I
need to thank the producers of this
episode Gabe James Matt Paul Steve West
Artic armor Dragon Commander angry
daroff George Lee Matthew methos Nate
Aran Paul peace AR door reality for Less
red profit rolling Soul as 10 R tools
Del waru and abtu and Willie these guys
they're my highest tiered patrons over
on patreon without these guys this quick
look at the absolute basics of them
would not have been possible the show is
also brought to you by each and every
one of these fine ladies and gentlemen
all these names you're seeing on the
screen these are all my supporters over
on patreon I don't have any corporate
sponsors I'm sponsored by you guys the
community if you like my work support me
on patreon subscribe to Dro tube over on
patreon peace
guys