okay bene benvenuti alla terza lezione
del semestre mancante
di CI oggi parleremo
di text editors questo è uno
argomento che mi piace molto e penso che sia
uno dei argomenti più importanti che
insegneremo in questa classe perché come
programmatori spenderete molto del vostro tempo
a modificare testi e programmi
se investe tempo nel rendervi
più efficienti nel farlo, risparmierete
un sacco di tempo probabilmente centinaia di ore
durante il corso della vostra laurea o
durante la vostra cariera lavorativa,
i text editors sono leggermente differenti da
altri programmi che potresti usare per modificare ad esempio
cose come la prosa Inglese perchè
i programmare è differente dal scrivere
una prosa Inglese, quando programmate
spendete molto tempo nel leggere cosa
avevate scritto, spendete molto tempo
navigating around a buffer and you spend
a lot of time making little edits to
code all over the place rather than just
writing in a long stream like you do
when you're writing an essay or
something and so it makes sense that
there different programs for these
different purposes right so yeah things
like Microsoft Word for writing essays
and things like vim and Emacs and vs
code and sublime for writing code so the
way you learn a text editor and become
really good at it is you start with a
tutorial and so that's basically going
to be the function of today's lecture
plus the exercises we've given you and
then after the tutorial you need to
stick with the editor for all your
editing tasks and when you're learning a
sophisticated tool so today we're going
to teach you vim which is one powerful
editor that a lot of programmers use
when you're learning such a
sophisticated tool it may be the case
that initially switching to this tool
slows you down a little bit when you're
programming but stick with it because
I'd say that in about 20 hours of
programming using a new editor you'll be
back to the same speed at which you
programmed using your old tool and then
after that the benefits will start and
you'll get faster and faster as you
learn more with these sophisticated
programs like them it takes not way too
long to learn the basics but a lifetime
to master and so throughout the time
you're using this tool make sure you
look things up as you go if you ever get
to a point where you're like oh this is
a really inefficient way of doing things
is there a better way the answer is
almost always yes because these text
editors were written by programmers for
programmers and so of course like the
peep
I wrote these tolls ran into the same
kinds of issues and fixed them so that
you don't need to deal with these
anymore and so yeah as you're learning
make sure you look things up as you go
either use Google or feel free to send
us emails if you have questions or come
to office hours and we'll help you
figure out how to do things really fast
so as far as which editor to learn in
previous iterations of this class we
actually avoided teaching a specific
editor because we didn't want to enforce
our opinions on you guys but we actually
think that it's really useful to teach
you how to use one particular tool and
use it well and so people have really
strong opinions about editor so you can
see the course notes for more links on
this topic looking at which editors have
been popular over the years Stack
Overflow I'm sure you've all heard of
that does a survey every year asking
developers various questions and one
thing to ask is which TextEdit Derk do
you use and it seems to be that
currently the most popular kind of
graphical editor is vs code and the most
popular editor that is based within a
command line interface is vim and so
we're going to be teaching you vim and
there a couple reasons for this one is
that all the instructors so me John and
Jose use vim as our primary editor and
we've been doing this for many years and
we've been very happy with it
and we think that there are a lot of
interesting ideas behind them so even if
you don't end up using this particular
tool in the long term I think it's
valuable to learn these ideas and also a
lot of tools have actually been really
excited about the ideas in vim and so
they support a vim emulation mode for
example vs code what's apparently the
most popular editor in use today
supports vim bindings and this vim
emulation mode as of now has like 1.4
million downloads and as you'll see over
the course of this lecture a lot of
different tools like including your
shell including things like the Python
repple and like Jupiter notebook and all
sorts of other things even your web
browser can support of emulation mode
and so yeah we're going to be teaching
you this really neat tool today and in
this lecture we can't really cover all
of them right it's a very powerful tool
but our goal is to teach you and the
core philosophy of them like the really
neat ideas behind it and then in
addition to that some of the basics like
how do you open a file close a file
navigate around a file make edits and
things like that and
you may not remember every single little
detail from this lecture because we're
gonna go pretty fast through some of the
material but it's all in the lecture
notes and then the exercises actually
give you links to some tutorials and
things so I highly recommend that you
actually go through all the exercises at
least the non advanced exercises any
questions so far great okay so one of
the really cool ideas behind them is
that vim is a modal editor what does
this mean modal comes from the word mode
and this means that vim has multiple
operating modes and this is kind of
developed from the idea that when you're
programming there often times where
you're doing different types of things
like sometimes you're reading code
sometimes you're making small edits to
code like you're finding a particular
point like changing a little thing
somewhere sometimes you're just writing
a lot of code in one go like suppose
you're just writing a function from
scratch and so there's different
operating modes for doing these
different types of things and so I'm
actually want to write this down on the
blackboard so I'll have a useful thing
to refer to later so when you start vim
up it starts up in what's called normal
mode and in this mode all the different
key combinations behave in one way and
then there are different key
combinations that switch you between
normal mode and other modes which change
the meaning of different keys so for the
most part you'll be spending most of
your time in vim in normal mode or what
is called insert mode and to go to
insert mode you press the key I for
normal mode and to go from insert mode
back to normal mode you press the Escape
key a little note on notation because
we'll need this later in the notation
I'm going to be using in this lecture
and what's also in the lecture notes and
what vim uses to give you feedback they
have a couple different ways of talking
about different keys so when they're
talking about bare keys like just the
eye key on your keyboard they'll just
say eye but for a different key
combinations like when you press control
and something like say control V it's
notated in one of like approximately
three ways one way that can be notated
is a caret and then the control
character so this is control V another
way this might be written I think we've
written it this way in
lecture notes as control-v this is
probably the one you're more used to
seeing and then in some parts of them
this is written as angle brackets C - V
close angle bracket so just a little bit
of notation that will be useful later
so yeah vim has a couple different modes
where normal mode is designed for
navigating around a file reading things
going from file to file things like that
and then insert mode is where you type
in text so most keys that you press here
will just go into your text buffer where
as keys that you press here are not
being put into the buffer and instead
are used for things like navigation or
making edits and actually the picture is
a little bit more complicated than this
there are a whole bunch of other modes
and I'm just gonna write them down here
because we'll have them here to refer to
later and so if it also has a replace
mode for rather than inserting text and
kind of pushing what's ahead of it
forward it will overwrite text and then
it has a bunch of different modes for
selection so it has a mode called visual
mode and then it has visual line and
visual block this one is enter via the R
key this was entered via the V key this
one is entered via shift B and this one
is entered via control V and then
there's the command line mode which is
entered via the colon key
okay so now that we have that on the
board to refer to later we can actually
try some of this out all right so one
thing we noticed looking at that picture
is that to go from normal mode to any of
the other modes we press some key but to
go from any of the other modes back to
normal mode where we spend a lot of our
time we use the Escape key on our
keyboard so for this reason since you
under pressing the Escape key a lot when
using vim a lot of programmers rebind
one of the keys on their keyboard to be
escape because it's really inconvenient
to reach up with your left pinkie to
press that tiny little Escape key in the
corner of your keyboard and so a lot of
people use the caps lock key instead so
it's right there in the home row and we
have some links in the lecture notes for
how you can do this key rebinding okay
so now that we've talked about kind of
one of the core ideas of vim the idea of
modal editing we can talk about some of
the basics like how do you open up this
text editor how do you open file save
files and things like that and so this
is a command line based program although
there are some graphical variants and
the way you start this program is by
running vim one thing you might notice
is that in the bottom left corner of my
screen they actually saw what I just
typed this will be useful later in this
lecture where I'm actually typing in
commands for vim and I'll be saying what
I'm typing but you'll also see it on the
screen so if I press ctrl C see it says
ctrl C over there is that text big
enough for everybody to read great okay
so the way we open vim is just by
running the program vim on our command
line and this comes pre-installed on
most systems and if you don't have it
you can install it using your package
manager vim can also take an argument if
we want to use it to edit a particular
file instead of just opening up the
program then later opening a file so for
example I have a file in this directory
this is actually the lecture notes for
this lecture so I can do vim editors MD
and press ENTER and then boom them is
started and then in this lecture I'm not
running vim in the completely Exton I've
configured a couple of things that
behave a little bit nicer by defaults
little things like having line numbers
on the left or having some more status
information on the bottom and if you
want to start with this default
configuration we have a link to this in
the lecture notes so you can get a
slightly more sane config by default so
once you've opened them what do you do
well as I said earlier vim starts in
normal mode so if I just start typing
letters like say type X it didn't insert
X into the buffer you can see the cursor
up in the top left it actually deleted
one of the characters that's because I'm
in normal mode not insert mode so insert
mode is basically what you used to with
all the other text editors you've used
in the past where there's a cursor
somewhere you press the character and it
just goes into your buffer I'm instead
in vim you start a normal mode and you
can press I to go into insert mode so
see I've pressed I and then in the
bottom left notice that it says - -
insert the bottom left always tells you
what mode you're in unless this normal
mode in which case it's blank and now
that I'm an insert mode if I press the X
character for example see it just gets
inserted into my text buffer and I can
backspace over at type other stuff and
now my text editor kind of behaves like
you'd expect any other program to behave
so from this point how to go back to
normal mode if I want to stop inserting
characters yes exactly I press escape
and that's the symbol my keystroke
visuals I visualizer uses for escape so
just be aware of that and then vim has
this idea that using the mouse is
inefficient like your hands are on the
keyboard moving your hand over to the
mouse takes a lot of time right like you
don't want to waste those couple of
milliseconds while you're programming
like in the middle of things so instead
all vim functionality can be accessed
just through the keyboard and it's all
sorts of things you might be used to
doing like opening files by going like
file open or file save or things like
that or instead access through the
keyboard and how is that done that's
done through one of the other vim modes
that are on the board over there in
particular through command line mode so
if you're in normal mode and you press
the colon key you'll notice that the
cursor I guess my visualizer is covering
the thing right now but it's gone the
cursor jumps to the bottom the bottom
left and it shows that : I just typed
now I can type in a command so you can
think of this almost like the command
shell that we've talked been talking
about over the last few days except this
is VIMS
command shell so you give vim commands
here instead of shell commands and
they're a bunch of built-in commands
that do all the things that you're used
to like for example one command that you
might want to know is how to quit this
editor you might notice that if your
normal mode I can press escape to go
back from command line mode to normal
mode and I press control C I'm like what
happens the lot of programs this doesn't
quit them so how do I quit vim I can
press colon and then go into command
line mode and then I can type in the
command quit key uit you'll see that I
maybe I should move this over to the
middle or something ceases colon quit
and I press ENTER and it quits bin I can
open him up again there's actually a
short form for this command just : Q and
that'll do the same thing and there a
bunch of other useful commands like this
so some other handy wants to know are
how do you save a file so suppose I make
some edits here like hello world
so I pressed I to go into insert mode or
let me redo that I press I to go into
insert mode right now I can use the down
arrow to oh hey I think I've slightly I
should fix that can you fix the config
actually John never mind that
okay so I suppose I go down to this line
and I press I to go into insert mode and
type in some text and then press escape
to go back to normal mode now how do I
actually save this file well there's
another command for that so colon to go
into command mode and then I can type W
and press Enter w stands for write and
it says in the bottom editors MD
whatever blah blah written and so this
means it saved the file and so now if I
colon Q to quit and open the same file
again you'll see that the changes have
been persisted there are a couple other
there's so there's a ton of different
vim commands that are useful for
different reasons but I'll just explain
a couple more to you now one command
that's really useful is help colon help
and you can do : help and then type in a
particular key or a particular command
and get help for that keystroke or that
command so if I want to know what : that
was if I want to know what : w does I
can do : help : W and that'll give me
the documentation on : w or : right if I
do : q it'll close this window and bring
me back to where I was before
and notice that : help : w is different
from : help W because the W key is the W
that like when you're normal mode and
press W what happens is just the W key
here without the :
and if I look for help for : W that's
the help for the W command so now you
basically have the bare fundamentals
needed to use them right you can open
the editor use it to edit a particular
file press I to go into insert mode and
type in some text press escape to go
back to normal mode and then : W to save
your changes : W to quit so like already
you have the bare fundamentals necessary
to edit files using them albeit somewhat
inefficiently so any questions so far
yeah in the back yeah so the question is
what's the benefit of the nura of the
normal mode and we'll talk about that in
more detail in like five minutes
but in short insert mode is just for
typing in text so I'm an insert mode I
can type in text but when I'm
programming I actually spend a lot of
time moving around my file making small
little changes so I go here and like oh
maybe I want to change this HTTP link to
an HD HTTP to HTTP
I can make like small point edits things
like that in normal mode and we'll see a
whole lot more of that in about five
minutes good question
any other questions okay cool so moving
along one other thing that's kind of
useful to know I think is at a high
level VIMS model
buffers versus windows versus tabs so
it's probably the case that whatever
program you were using before like
sublime text or vs code or whatever
you could open multiple files in it
right and you could probably have
multiple tabs open and have multiple
windows open of your editor so vim also
has a notion of those different things
but it's models a little bit different
than most other programs so vim
maintains a set of open buffers that's
the word it uses for open files and so
it has a set of open files and then kind
of separately from that you can have a
number of tabs and tabs can have windows
the kind of weird thing which makes them
a little bit different than the
programming probably used in the past is
that there isn't necessarily a
one-to-one correspondence between
buffers and windows so one thing I can
do for example here and we'll show you
the key combinations and stuff for this
later but one thing you can do is create
two different windows so I have one
window up here and then one window down
here and notice that the same files open
in both windows so if I make some edits
over here they actually happen in the
bottom window as well because it's the
same buffer that's open in both windows
and this is kind of useful for say
looking at two different parts of a
single file at the same time like so you
want to be able to look at the top of a
file say at an import to your program
while you're down below working
somewhere else so this is one helpful
thing to keep in mind that vim has this
idea of they're a number of tabs and
each tab has some number of Windows and
then each window has a corresponds to
some buffer but a particular buffer can
be open in zero or more windows at a
time just one thing that confused me
when I was initially learning BIM so I
want to explain that early on and then
the : Q command which we talked about
earlier is not exactly quit it's kind of
closed the current window and then when
they're no more open windows then we'll
quit so here if I do : Q it'll only
close the window I think on the top here
because that's the one I was in and now
the remaining window becomes fullscreen
I can do : Q again to close this now in
the second tab that I'd open if I do : Q
a final time okay now vim exits and if
you don't want to press : Q way too many
times okay so here I have three split
windows if I do : QA for quit all it
closes all the open windows
all right so now answer your question of
what is normal mode actually for this is
another really cool idea in them and I
think this is actually like the most
fundamentally interesting idea of this
program it's that like you're all
programmers you like programming vim has
this idea that VIMS normal mode like
VIMS interface itself is a programming
language well and let me repeat that
that's like a kind of fundamentally
interesting idea the interface is a
programming language
what does that mean it means that
different key combinations have
different effects and once you learn the
different effects you can actually
combine them together just like in a
programming language you can learn
different functions and stuff and then
glue them all together to make an
interesting program in the same way once
you learn them's different movement and
editing commands and things like that
you can talk to them by programming vim
through its interface and once this
becomes muscle memory you can basically
edit files at the speed at which you
think like at least for me I don't think
I've been able to do this with any other
text editor that I've used in the past
but this one gets pretty close so let's
dig into how exactly normal mode works
so you can try to follow along with this
like open up some random file in vim and
follow some of the key combinations I
type in so one basic thing that you
might want to do is just navigate around
a buffer like move your cursor up down
left right and so the way you do that in
vim is using the hjkl keys not the arrow
keys though they do work by default try
to avoid them because you don't want to
have to move your hand all the way over
to the arrow keys like there's a ton of
time you're wasting right HT Cales right
on the home row and so J moves down K
moves up H moves left and L moves right
and this may seem a little unintuitive
now there was some historical reason for
it like the keyboard the original VI
developer used had the hjkl keys like
labeled and arranged in a way that made
this more reasonable but this will very
soon become muscle memory so this is the
basic way you can move your cursor
around while in normal mode now what
else can you do well if we had to move
around files like this it'll be really
slow we don't have to want to have to
hold down these keys and like wait for a
long time for them to do its thing and
so there are all these other different
key combinations for doing different
movements
also by the way this is all in the
lecture notes so you don't need to
memorize every single key and its
meaning right now just try to understand
the overall idea that bins interface is
a programming language so another thing
you can do is press the W key this moves
the cursor forward by one word and then
similarly the B key moves the cursor
backward by one word so this allows
slightly more efficient movement within
the line there's also the e key for
moving to the end of a word you're gonna
move this over a little bit so if I'm
here for example and I press the e key
it'll go to the end of this word end of
this word end of the next word and so on
you can also move by whole lines so zero
moves to the beginning of a line dollar
sign moves to the end of a line and the
caret key moves to the first non-empty
character on a line so let me find one
of those for example so here I'm on my
cursors right here if I press 0 my
cursor goes to the beginning of the line
dollar sign end of the current line and
if I press caret we're like on what
character will the curser end up can
anybody guess so caret goes to the first
non-empty character on a line kind of
like reg X carrot yeah exactly it goes
to this - let's talk about some more
movement commands there's a ways to
scroll up and down in a buffer so
control U goes up and control D scroll
down so this is better than holding down
the K or J keys for example this is a
lot slower than moving by entire pages
control D and control you there's also
ways to move by the entire buffer so
capital G moves all the way down G G
moves all the way up some of these
movement keys are mnemonics so they're
like a little bit easier to remember for
that reason right like double use word B
as beginning for it is end of word those
all seem pretty logical 0 caret and
dollar kind of inspired from reg X so
those make a little bit of sense there's
some other ones that like don't
necessarily make way too much sense but
there are only so many keys on your
keyboard so what are you going to do for
example the L key moves your cursor to
the lowest
line that's shown on the screen l4
lowest makes sense m4 middle and then h4
highest I guess no and there's a whole
bunch of other interesting movements
like this so we're obviously not going
to be able to cover all of them right
now but you'll be able to go through
them in the vim tutor exercise which is
exercise number one for this lecture
some other ones I want to talk about now
maybe I'll talk about one more there's
another movement called find this is
also kind of useful suppose I'm on this
line and I want to jump to the first
character that equal to something like I
want to jump to the first oh I can press
F oh and my cursor moves to the first
oh I've like found oh I can do FW and
it'll move to the first W which I think
is right here F C find the first C I can
also do the same thing but backwards if
I do capital F W I can find the W that's
before it a capital FS find the s that's
before that and then there's a variant
of F for find T for two so I can jump to
O and it jumps like until it's found oh
but not on top of it right before it and
capital T say T jumps backwards to the T
except not all the way on top of it one
character before and so you can already
see that idea I talked about of like vim
is a programming language you can really
compose these commands F and T are
finding two and you can say find a
particular character or jump to a
particular character so those are a
couple of them movement commands so any
questions about those so far so this is
yeah question right yeah okay cool so
those are have been movement commands
this is how you can navigate around a
file quickly in normal mode now another
category of useful commands are editing
commands so one we kind of already
talked about which is the eye command
for moving from normal mode to insert
mode where you can start just writing
text so suppose I go up here and I press
I now I can type in whatever text I want
hello world enter this back then press
escape to go back to normal mode and
I've made a change to my buffer but
there are a whole bunch of other
commands for making efficient
that makes sense for when you're dealing
with programming languages so one useful
command that I accidentally used earlier
before teaching you about it is the O
command so suppose my cursor is like
over here and if i press o from normal
mode what it does is it opens a new line
below where my cursor is
that's what o stands for and it so it
creates a new line and put me into
insert modes and I can start typing in
some text press escape and go back to
normal mode and then just like the O
command there's a capital o command so
if I'm here and I do capital o it puts
me into insert mode above where I
currently am there's another vim command
for deleting things so suppose my cursor
is like on top of this word right here
and I press the D key D for delete oh
nothing happens turns out that the D key
needs to be combined with a movement
command so remember we just talked about
different movement commands like hjkl
and like word and backward word and
things like that
so I press D whoops I press D and I can
press W and it's deleted a word so let
me undo that undoing vim is just you for
undo so notice my cursors right here I
do DW it's deleted a word I can move
around and then delete another word
suppose I'm keeps getting in the way
suppose I'm like somewhere in the middle
of a word and I want to delete the end
of a word any guesses for what
combination of keys I'd use for that D
and what de exactly delete the end of
the word another useful editing command
is the C command C stands for change so
change is really similar to delete
except change puts you in insert mode
because like I want to delete a thing
but change it to something else so if
I'm here and I do see e it's like change
to the end of the word and it gets rid
of the contents until the end of the
word and notice it put me in insert mode
so now whatever characters I type go
into the buffer if I press escape I go
back into normal mode and so C and D are
analogs they both take motions as
arguments and they will either delete
that motion or change
that motion so for example if you press
the seek there's also this pattern that
if you press a particular editing key
twice it'll have that effect on the
given line so if I press D D that
deletes the line if I press C C that
deletes the given line but puts me in
insert mode so I can replace it with
some other line well cover a couple
other editing commands because then
later we'll see how all these things
interact together so another useful one
is the X command so suppose my cursor is
over some particular character you've
had pressed X it just deletes that
character there's another command called
R if I'm over a particular character and
I press R it takes another character as
an argument and it replaces that
particular character with some other
character and I'll cover a couple more
editing commands so I think when I
talked about a moment ago but of course
you can undo changes you've made in vim
and the way you do that is by pressing
you while your normal mode so you for
undo is pretty easy to remember so I
press you a whole bunch of times it's
undone all the changes I've made and
then the opposite of undos of course
redo and the binding for that in vim is
control are all right one other editing
command I'm going to talk about is copy
and paste because oh yes question that's
it that's a great question so the
question is does undo undo everything
you've done since you've gone into
insert mode or just the last character
it's it's actually a little bit more
complicated than that
undo does like undoes the last change
you've made so if you went into insert
mode and typed in some stuff and went
back into normal mode and then press you
for undo it'll undo all you've done in
insert mode but if you've done some
other type of editing command like say I
press X to delete a character if I do
you for undo it'll just undo that change
that that editing command made now does
that answer the question yeah great any
other questions
cool so I'll talk about copy and paste
as well because that's a popular one the
Y command stands for copying and the P
command stands for pasting y for coffee
because yank like that's the word date
that's the term logic terminology that
vim uses for copying and these commands
are y also takes a motion as an argument
so if I do like YY it copies the current
line in an effect press P for paste
notice that now these two lines are
identical because I've just pasted a
line below you for undo but if I do
something like YW it's copied the word
and then I can do P and it just pasted
that word again
right where my cursor was one useful
thing especially in the context of copy
and paste is to be able to select a
block of stuff and copy it right like
this is probably how to used copy and
paste in whatever editor you were using
before and so that's where we get into
the visual modes so these are another
set of modes that are all related to
each other and that can be reached from
normal mode and they're used for
selecting chunks of text so one mode is
just regular visual mode you can enter
that by pressing V and then once you're
in this mode you can use most of the
regular normal mode commands to move
your point around and it selects
everything in between so I can use like
hjkl just to move the cursor or I can
use W to move by words or different
things like that and it will select a
block of text and once I've selected
this block of text there a whole bunch
of different types of useful things you
could do with it one of the most popular
things to do is copying this so once
I've selected I can do Y to copy and it
puts me back into normal mode and now
it's copied this to the to the paste
buffer and then if I go somewhere else
and press P it pastes in that whole
chunk of text I copied and it's similar
to visual mode which selects kind of a
contiguous stream of text there's visual
line mode so that can be reached by
pressing capital V and that selects
whole lines at a time and then there's
visual block mode which can be selected
by pressing control V and that can
select rectangular blocks of text so
this is something your old editor
couldn't do alright so there's a lot
more vim editing commands to learn
there's lots of like really weird and
fancy things like for example the tilde
command changes the case of the
character or the selection that you've
currently selected so for
I can take this like Visual Studio code
and flick the case on the whole thing by
selecting it and pressing tilde and
there's a whole bunch of other things
like that they get more and more
esoteric as you go so we're not going to
cover all of those but you'll get to
those in the exercises so those are vim
editing commands and a lot of them can
be composed with movement commands so
any questions about either of those so
far cool so moving along another
category of things of commands that are
mostly relevant to normal mode are
counts so you can give them a number to
do a particular thing some number of
times so suppose my cursor is here and I
want to move down like 1 2 3 4 lines one
way I can do that is by pressing J four
times go down four times KK KK it goes
up four times but rather than pressing a
particular key again and again I can use
a count so if I press four J it does J
four times right
thumbs interface is a programming
language if I do four K it moves up four
times if I am here and I press me to go
into visual mode okay so now I can move
my cursor around and select blocks of
text I can do like EEE to select a
couple of words but I could also go back
here v for visual mode and press three E
to select like three ends of words for
word and then of course these can also
be combined with editing commands so
like suppose I want to delete seven
words I can do that by moving my cursor
somewhere and doing seven dw7 delete
words and so this is particularly useful
for things like suppose my cursor
somewhere on the screen and I'm looking
somewhere else on the screen or I want
my cursor to go to that particular line
notice that I've set up relative line
numbering on the left so wherever my
cursor is it shows the current line
number but everywhere else it's just the
offset from where I am now suppose my
cursor is here but I want to move down
to the like Microsoft Word down here so
that's eight lines down so what
combination of keys would I press to do
that like what's the most efficient way
yeah exactly
let's try that out 8j and my cursor move
down to this line
okay and then one final category of key
meanings in BIM is something called
modifier so we have so far movement
edits counts and finally we have
modifiers so modifiers kind of change
the meaning of a movement command a
little bit and a couple modifiers that
are especially useful are the 8 and I
modifier so a stands for like around and
I stands for inside and to see where
this is really useful I can move my
cursor to somewhere like here for
example
so hopefully most of you are familiar
with markdown syntax and if not it
doesn't matter too much this is a
Lincoln markdown it's a text rendered in
square brackets and then the link in
parentheses suppose my cursor is inside
here and I want to change the text
corresponding to this link well one way
I could do that is like move back here
with B and like to DW and then I to go
into insert mode like that's one of the
many ways I can make this change and I
can type in whatever other thing I want
you to undo huge undo another way could
have done that is change two words C to
W and then type in some other text but
one final way I could do the same change
is using the modifier commands to talk
about how I want to interact with these
different types of grouping things like
parentheses and square brackets so one
final way of doing this is change inside
square brackets CI bracket and that puts
me into insert mode after deleting the
contents that are inside the brackets so
do you see how we can take all these
different ingredients like we talked
about change and we could combine that
with different movement commands we
talked about inside how it's a modifier
and then we talked about X we didn't
talk about parentheses but if your
cursor is hovering over a different
different types of grouping things like
parentheses or square brackets you can
press the percent movement key to jump
back and forth between matching
parentheses if I go over here and I do D
I parentheses I can delete the contents
inside these parentheses and so those
are vim modifiers I guess we talked
about I but we didn't talk about a if I
do D a parenthesis it deletes a whole
like parenthesized group including the
parentheses so I is inside
is around or including all right so
those are basically the different
categories of things you can combine
together when interacting with VIMS
interface so any questions about that or
the overall idea of this interface being
a programming language cool so let's do
a quick demo to kind of demonstrate the
power of this editor and it will kind of
help us see how this tool can work
really fast and kind of match the speed
at which we think so over here is a very
broken fizzbuzz implementation that
doesn't actually print anything
hopefully most of you have heard of
fizzbuzz if not I'll explain it super
briefly this buzz is a programming
exercise where you print the numbers 1
through n but when the number is
divisible by 3 you print fizz when it's
divisible by 5 you print buzz and when
it's divisible by both 3 & 5 you print
fizzbuzz and if none of those apply you
just print the number so you should
print like 1 2 fizz for buzz and so on
but if I run this program it doesn't
print anything here I have them on the
left in just a terminal on the right ok
so there's a bunch of issues with this
one is that main is never called so
let's start off with fixing that so
here's how I would make this change and
notice how few keystrokes this requires
capital G means go to the bottom of the
file o opens a new line below and now I
can just type in stuff so I'm an insert
mode okay so I've typed in whatever
change I want to make escape to go back
to normal mode if I do : W so command
mode right let me go back here okay now
at least my program prints something
when I run it another issue with this
program is that it starts at 0 instead
of 1 so let's go fix that so I want to
go over to this range whoops this range
thing and it shouldn't be going from 0
to limit it should be going from 1 to
limit plus 1 1 command which I didn't
show you about is how you search in vim
so you press forward slash
to close this and restart it if you
press forward slash it starts search so
if I type in range enter my cursor goes
from wherever it was before to the first
instance of range it found so it's a
really efficient way of moving where I
want to move WW to move forward two
words I to go into insert mode add the
one comma space escape I'm back in
normal mode this is a very common
pattern in vim you stay in normal mode
you go somewhere you go into insert mode
you make a tiny change and you jump
right back to normal mode like normal
mode is home and that's where you should
be most of the time I also want to add a
plus one so e to go to the end of this
word a for a pend plus one escape
alright fix that problem and other issue
is that this program prints fizz for
both divisible by three and five so
let's fix that slash fizz searches for
fizz been oppressed and it goes to the
next match see I press E I quote changes
what's inside the quote so it's deleted
the fizz and put me in insert mode right
in between those two quotes and I can
type in whatever I want
escape to go back to normal mode so
great I've fixed that particular problem
another problem with this program is
that it prints fizz and Buzz on separate
lines for multiples of 15 so let's let's
go and fix that let me go down to this
line here
one way I can don't actually worry about
like the actual contents of this program
like this some stupid program that
doesn't matter pay attention to what
keys I'm pressing in vim that allow me
to make changes to this program really
efficiently so my cursor is on this line
i press dollar to go to the end of this
line i for insert mode okay and i'm
typing some stuff escape to go back to
normal mode now I want to make the same
change the print below look at this JJ
dot so what dot dozen vim is it repeats
the previous editing command that was
made and so this is a really nice way of
doing repetitive tasks without typing
the same thing over and over again so in
that particular case that inserted comma
end quote and so it applied the same
thing on this line when I press dot and
then when I guess one final part of this
demo is
we will fix the issue that this program
maybe should take a command-line
argument instead of having this
hard-coded 10 down here so how do we do
that I'll press GG to go to the top
capital o so now I've opened a line
above and I'm going to type in some text
like imports this enter escape to go
back to normal mode and then I want to
go down to where this 10 is so slash 10
makes me jump straight down there CI
pren to edit what's inside the
parentheses and now I can type in like
whatever thing I need to type in here
and then once I've done this my program
does fizzbuzz correctly I think I missed
one change I wanted to make but it
doesn't matter this demonstrates that
you can make lots of changes really fast
so any questions about this demo or the
overall idea we've been talking about ok
so this will be covered Tuesday so the
kind of outside environment I'm running
vim on the left and my shell on the
right and then this is team ox on the
outside one variant of that question
might be like how do you switch between
different vim windows and you can see
the lecture notes for that but there's a
key binding for that so if you have the
same window open or multiple things open
there's a way of doing that question ah
good question so delete takes a motion
and then removes those contents but
keeps you in normal mode so you can keep
just moving around in a file what change
does is very similar to delete it takes
motions and treats them in the same way
deletes those contents but then puts you
in insert mode and so it saves you from
typing one extra keystroke so if I'm
here for example I want to delete main
DW deletes a word but now if I press
whatever key likes I press J it just
moved me down if I undo that do cw4
change a word now it's actually put me
into insert mode and I can type in
whatever I want it to insert so DWI is
the same thing as CW but it saves a
keystroke one thing we've linked in the
resources is something called vim golf
basically people have set up a game
online where you can get an editing task
and try to figure out the minimal number
of keystrokes necessary to complete that
editing
it's actually really addictive so I'd
only suggest going on their chest and
script time I think I saw a hand for
another question yeah uh period yeah one
of the most useful of em commands good
question any other questions
cool so I think we have about five
minutes left and I'm gonna briefly talk
about a thing that's also covered in
detail in the notes so make sure you
look at the notes for this so vim is a
programmers text editor and so of course
it's highly programmable not only
through its interface that's a
programming language but also a couple
of different ways there's lots of
settings that you can tweak to match
your preferences and you can also
install plugins for them that do all
sorts of useful stuff so the way vim is
configured is through a file on disk
called vim RC and you'll see this is a
common pattern in a lot of shell based
tools there'll be a plain text file that
configures how the Tool Works and so if
I edit this file and it may or may not
exist on your machine yet but I've
downloaded the we've created a kind of
default vim RC for you and linked it on
the course website so you can start with
that one if I do vim tilde slash boom RC
I can see here a bunch of comments and
then particular commands like by default
we want syntax highlighting on and we
want line numbers if we didn't do some
of these things like let me remove the
stuff that sets line numbers if I remove
those configurations and relaunch vim
notice that I no longer have line
numbers on the left but yeah so in short
there's a lot of stuff you can configure
with them we've given you a very basic
configuration that tries to like remove
some of the kind of weird behavior
that's on by default in vim but we don't
really try to enforce too many of our
other opinions on you but of course like
the three of us used them a lot and we
have heavily customized VMR C's so we've
linked to our personal configurations
too if you want to take anything from
that and also like thousands or like
millions of people share their var C's
on github so there's lots of places to
look for inspiration
there's also cool blog posts on this
topic another thing you can do in vim is
you can extend it with plugins that do
all sorts of useful things this lets you
do things like fuzzy file
finding which a lot of text editors come
with by default so you can get like a
pop-up window you can type in a name of
a file or approximately the name of a
file and find it very quickly or there
are things that show you like
visualizations of undo history there are
things that show you like file explorers
things like that so we've linked to a
couple of our favorite plugins on the
course website and so I highly recommend
becoming familiar with how to install a
plug-in because it takes like three
seconds and some of them are really cool
and then finally the last topic I'll
briefly mention before we finish today's
lecture is vim mode and other programs
so turns out that a lot of programmers
we're really excited about VIMS
interface and so they've implemented
similar functionality and other tools
for example like I've configured my
Python repple to run in vim mode so I
can like type in stuff here and if I
press escape now I'm in normal mode in
my Python repple and I can like move
back and forth and like press X here
delete a thing like CW change a word and
do all those good things and it's not
just the Python repple like I have my
terminal behaving this way too so like I
can type in whatever I want here and
like escape and I'm in normal mode I can
go here and like go into visual mode
inside my terminal and like select
blocks of text press tilde to change the
case whatever so we've linked sir like
how exactly you can enable vim mode for
like bash z shell fish a lot of read
line based programs like jupiter
notebook a whole bunch of other things
and if it's not another place you can
probably find it by googling it because
a lot of people like to have this sort
of functionality and if you're really
gonna like commit to learning them I
think it's valuable to enable this sort
of like vim emulation mode in every tool
you use is like one or like make you
learn the tool a lot better and to once
you become good at vim like those skills
will now transfer to all your other
tools you use okay so I think that's it
for our rapid introduction to them
there's some other neat material that we
weren't able to fit in today's lecture
but it's in the lecture notes and then
finally I highly recommend going through
the exercises for today like at least
for me personally I think spending time
learning my text editor has been like
the most beneficial thing out of like
the kinds of things we're teaching in
this class
so yeah that's it for today's lecture
and we'll see you tomorrow
note that we've changed tomorrow's
lecture to data wrangling Thursday and
Tuesday lectures are now switched this
is reflected on the course website in
case anybody was going to come to one
but not the other