[Seminar Vim]
[Brandon Liu] [Harvard University]
[This is CS50.] [CS50.TV]
Today is going to be an introduction to Vim.
How many of you guys have heard of Vim?
What have you heard about Vim?
[Student] It solves all your problems.
[Brandon L.] It solves all your problems.
Anything else you guys have heard about Vim? Any impressions?
[Student] Almost everyone I've talked to likes to use Vim.
Oh, sweet.
[Student] A lot of people I know like to use Vim.
Okay, that's good, because a lot of people in CS51
start to pick up Emacs because Emacs has slightly better support
for the language you're going to use in CS51,
and I worry that not enough people at Harvard use Vim,
so I really try and do a good job of encouraging Vim usage.
But here is a classic xkcd comic which you guys might find interesting.
Have you guys used Nano before at all?
This comic says Nano? Real programmers use Emacs.
Hey, real programmers use Vim.
No, real programmers use Ed which is this—you don't ever want to use Ed.
It's really, really old and really bad, and real programmers use 
a magnetized needle and a steady hand.
And you can read the rest, but it's pretty amusing.
And the funny thing is at the end. 
They say, "Oh, yeah, of course, there's an Emacs command to do that."
Good old C-x M-c M-butterfly, which is sort of a jab at Emacs
because Emacs is historically known to be a little bit bloated
and to have commands for almost anything, so that's sort of a joke for Emacs.
What is Vim? Vim is an advanced programmer's text editor.
I really want to make the distinction between text editor
and something like a document processor.
Microsoft Word is known as a document processor.
You can set margins, make things bold, underline things,
strike through things, change font size and whatnot.
A text editor does nothing more than just edit text.
Literally you just have characters.
There's no sort of styling, sizes, fonts, colors, or anything like that.
All you do is edit text, and Vim has been especially made for programming.
It has a lot of features built in just for programming.
It's designed expressly for efficient text editing.
In general, Vim is known to be basically the fastest text editor
you can possibly use compared with any other text editor that's possible.
A lot of IDE-like features are not built directly into Vim,
so if you've used an IDE and you're used to things like compile and run automatically
or debugging what's in your IDE or a whole host of different things
Vim doesn't come with that naturally,
but there's a whole host of plugins that you can use to add that sort of functionality to Vim.
Vim by itself focuses on text editing and does text editing really, really well.
And finally, it's really customizable and extensible,
and basically Vim has been around, or Vi, which Vim is built upon,
has been around for over 40 years, and it is a very, very deep program,
and people will use it for a lifetime and never truly master the program to its fullest,
and you will always continue learning throughout your life over the years,
always becoming better and better.
There's basically no end to what you can learn and how much you can improve with Vim,
which is really exciting.
You don't want to view it as something scary because you can become very, very proficient
in Vim in just a few weeks but it's sort of like a continual—
you can always learn more and learn more and do things better and better,
which is kind of exciting for me.
This is an example of what Vim looks like.
This is not mine. You'll see some live demonstrations right now.
This is an example of what Vim might look like.
What we're going to do today is we're going to start off with
 a quick showcase of what Vim can do.
I'm going to do some tricks and whatnot for you guys
so you can see why I like Vim so much and why people like Vim so much,
and then from there we'll go into a gentle introduction.
Actually, it will be a bit fast, an introduction to Vim.
I'll ask you guys to download a text file and to follow along with me if you can.
This is Vim, and right here I'm using MacVim,
so if you guys have Macs you may want to consider using MacVim
because it's a graphical version of Vim, although the regular Vim,
what's in your command line, works just fine.
I'm going to show you a few random tips and tricks
that you can use in Vim to show you why it's so cool.
Just a few random things I'm going to go through.
The first thing is text objects, and there are a few commands around text objects,
and I'll actually teach you what text objects are in more detail later in the seminar,
but for example, let's say I spelled my name wrong, 
Barndon Liu, and I want to change it.
All I have to do is get my cursor somewhere in my name and type in the command
ciw, and it will delete my whole name, put me in insert mode,
and I can type in my name and fix it.
Similarly, if I want to change something inside quotes there's also a command for that.
I can delete things inside quotes.
No more having to double click, move  your hand over to the mouse,
double click, and then type something back in.
You can do everything from the keyboard, whatever it is.
Or alternatively, let's say I have this image tag inside this anchor tag.
Have you guys covered HTML in class yet?
Yeah, so maybe I want to delete this tag,
which normally what you have to do is you have to use your mouse
and carefully highlight exactly what you want to delete and delete it.
But in Vim that's simply dit, and you can delete it out really quickly,
and I'll explain to you what all those commands mean and how you use them in a second.
Another example, I have some code here,
and let's say I want to indent the code, but I want to indent the code
just within the parentheses, and I can also indent the code using 3 commands
using basically >iB.
Another thing that's really cool about Vim is something called visual block editing.
I don't know if you guys have ever wanted to highlight
a rectangular block of text in your code sometimes
instead of having to do this weird sort of line thing that spans all different lines.
For example, in Vim you can do that.
I can highlight rectangles like this, which is pretty cool, 
but furthermore, I can edit using that functionality.
For example, if I want to give some sort of prefix to all of our names
I can highlight a block and then add something.
Similarly, I can also highlight this block and choose to delete it,
or maybe I'll highlight this block and change it to D because we're all doctors.
Whatever it is, you can do all those different things.
Finally, folding is really cool. 
A lot of IDEs have this where you can fold different functions out of view
in case you don't want to see them.
For example, suppose I have this bunch of text here, and I don't really want to see it.
I want to keep it out of view because I'm editing something else in the file.
I can fold it up so it's all collapsed,
and that way it's out of view, 
and I can edit other things that are on the page, and this is really great.
You can fold up functions and whatnot or fold up classes when you're programming 
just to keep them out of view when you're not editing them,
and later when you want to see them you can open them up.
And you can see it just opened everything up.
Tabs, buffers, and windows.
Vim is pretty cool in that
normally in a lot of text editors you can only edit one file at a time,
like in Nano, or maybe if you're using Notepad or Gedit.
I've actually never used Gedit very much, but at least in Notepad++
you can have tabs, but Vim has the concept of tabs, buffers and windows.
For example, you can open up a bunch of new tabs.
You can see here at the very top I have now a bunch of tabs,
and I can close them.
Furthermore, there's this concept of buffers,
which is the idea that once you've opened a file
it stays alive in the buffer, and if you want to navigate to that file really quickly
you can navigate to it really quickly within the buffer as well,
and furthermore, within one pane you can also split your window up into multiple windows.
For example, if I want to split this window up into multiple windows
you can see right here that I can have the same code twice,
and I can split more and more and more, and right now it doesn't look that good
because by default I have the default window size be kind of big
so all the others are squashed out,
but normally when I'm on my computer and not on this projector
I can have several panes open at the same time, which is really nice for 
editing multiple files at the same time.
You can also  have things like select and replace,
and we'll actually cover that in the seminar,
so I'll skip that for now.
You can also interact with the command line in all sorts of different ways.
For example, if I have some sort of list here and I want to sort it—
I don't know, let's add another fruit, grape,
and I can highlight this using the highlight function 
and call sort, and it will sort it automatically for me.
Similarly, if you're going to be editing CSS, which I presume you probably will be doing 
for your final project, most people do, 
and maybe you want to keep your CSS organized by sorting them in alphabetical order.
You can also highlight them and call sort, and it will sort them for you.
These are some of the examples of the things that Vim can do,
and you'll notice that the key thing that I really want to draw your attention to
is that I'm doing everything from the keyboard,
and if you are able to see where my hands are placed
you'll also see that my hands never really move from the home row.
Normally when you're editing text you usually have to use a mouse
and click around and whatnot, and that sort of movement drags you down
and wastes time, or perhaps you're moving between the home row and the arrow keys,
and so you're moving back and forth like that, which is a bit cumbersome 
but also slows you down, and the thing is once you get really good at Vim
everything you want to do is done without having to move your hands
from the home row, which is great.
Hold on, I'm going to open up this application. There we go.
Now you can see what commands are running down the lower right window.
All the functions that I just showed you are things that are included by default with Vim,
but there's also a huge number of plugins that you can also use
to improve your Vim experience.
If you downloaded the configuration files that I included,
and there are instructions on how to install them in the PowerPoint
and also in the email I sent out, I have about 40 plugins installed
or maybe 30 plugins, and every single one
brings additional functionality to Vim that improves your Vim experience.
For example, one of my favorites is something called surround.vim,
and it basically allows you to work with surrounding 
or delimiting characters much more easily.
One example, I don't know if you guys ever code in LaTeX,
but LaTeX is a math typesetting language that is used to make
really nice looking math, and whenever you want something to look like math
you wrap it in dollar signs, and let's say I'm typing some equation  like F=ma,
and I forgot to wrap it in dollar signs.
Then I can highlight this and wrap it in dollar signs just like that.
Alternatively, Vim allows you to delete things inside tags
and delete tags and whatnot, but Vim by nature
doesn't allow you to change tags, so for example, I have this div tag,
and I want to upgrade to HTML5 and use a new header tag.
I can do change surrounding tag and type in header,
and it will automatically change that for me.
Similarly, let's say we have something in parentheses,
and I wanted to change that from parentheses into brackets or something like that.
I can also change that really easily by typing in c for change,
s for surround, parentheses.
Oh, I guess I have to type it faster.
Parentheses and braces or brackets, and it will automatically change that for me,
which is really nice, otherwise normally it's really cumbersome.
This is really nice to switch between perhaps single quote 
and double quote strings or something like that.
That's one example use case.
There's a plugin called Fugitive
which integrates with Git.
I'm guessing maybe you guys have not used Git that much in CS50,
but you'll find yourself using Git more and more and more,
not only in classes throughout Harvard but also at jobs and whatnot,
and normally you have to switch back and forth between 
the command line and Vim to interact with Git,
but there's plugins to integrate with Git from within Vim
and also to give you even additional functionality on top of Git.
There's something called NERDTree, which you can use to explore
the file structures from within Vim.
For example, I can open up NERDTree on the right pane,
and you can basically see random files
and open—not taxes, don't look at my taxes.
You can look at files and whatnot and open other files and so on and so forth.
And I won't go into these other plugins, so we can basically dive in
and start working on Vim, but there are plugins that allow you to
comment more easily, plugins that allow you to jump in between files more easily,
plugins that give you syntax checking from within Vim,
which is really cool, and it gives you syntax checking
for most languages right out of the box, 
things like rainbow parentheses, which is actually really, really cool.
Let me give you an example of that right now.
Let's see.
Here's some random code that I was writing,
and you'll notice that the brackets and parentheses are all colored,
and parentheses and brackets of the same color are matching, which is nice
and sometimes improves the readability of your code.
You also have autocompletion, tabular formatting.
When you have something that you want to get all the comments to line up
or you want to make a table in your text and you want to 
get everything to look like a nice table, instead of having to format that manually
there are plugins that will format that for you and so on and so forth.
The list goes on and on and on.
I could spend hours exploring all the different Vim plugins that exist,
and it's incredible what's out there.
Any questions so far? Sweet.
In that case, we're going to start playing around with Vim,
and I want you guys to work with some Vim with me
so you can get a feel for it, and I can guide you in your first steps working with Vim 
because a lot of people get overwhelmed when they open Vim up,
and sometimes if you don't even know the command to exit you can't even get out of Vim,
but it's not that bad.
You just need a few minutes of guidance from someone who has used Vim before
or to read some tutorials online, and you'll be on your way in no time.
Here are some instructions to install Vim if you haven't already.
You can look at these slides after the presentation if you want to install more things.
Also, these are some instructions for installing my configuration files,
and you can also follow these instructions later if you haven't already.
If you guys could go to this link and download a text file.
The link will take you to this text file, which I would like you to download and open up in Vim,
and there are a bunch of exercises and whatnot in there that you can follow around with me
and start getting some practice with some of the basic operations in Vim.
Good.
While you're opening that up I'm going to give a brief intro to 2 really key principles
in Vim editing which are really important to understand before you dive into Vim,
and that's modal editing and operators and motions.
I'll cover modal editing first, and then we'll cover operators and motions
in a little bit.
The first key distinction between Vim and pretty much all other
text editors of any sort is that Vim is a modal editor,
and what that means is when you're using Vim 
you could be in one of many different modes.
There's normal mode, there's insert mode, there's visual mode, 
so on and so forth, and the thing is, your keystrokes take on different actions,
take on different meaning, depending on what mode you're in.
Just as an example, normal mode is the mode you're in when you move around,
and so I would use the key J to move downward,
but in insert mode, you use the key J to actually type out J on the screen,
and that's why a lot of people get confused because they're not used to
being in different modes all the time, and so they'll be typing things out, 
and you're used to having whatever you type appear on the screen,
but in Vim maybe your cursor is moving all over the place 
because you're typing weird characters, and suddenly you get
shifted into insert mode because you typed some key 
that puts you into insert mode, and then text starts flying out,
so people get confused because of that.
But you have to keep track of what mode you're in,
and if you look at the very bottom toolbar of Vim 
there should be an indicator that tells you what mode you're currently in,
which should be helpful.
But in general, the 3 main modes you should know about,
there are only probably 6 or 7 or 8 modes or so,
but you spend most of your time in 3.
The first thing is normal mode. This is where you spend most of your time.
All keystrokes are logged as some sort of command,
and we'll be learning a lot of those commands throughout the day today.
The other mode is insert mode, which is where you actually are typing things out,
and insert mode is basically the mode you're usually used to.
You type something, and it just comes on the screen,
and visual mode is something where you can do highlighting.
You saw earlier where sometimes I would be highlighting text
and manipulating it in different ways, and that's an example using visual mode.
We'll start off with the absolute basics you need to survive in Vim,
and it's basically these 7 commands right here.
HJKL are used to navigate around the file,
and they correspond to left, down, up, and right,
and you're free to use the arrow keys as well,
but we really encourage you to use HJKL as much as possible
because the idea is that while you're editing, you can move around the document
without having to shift in between the arrow keys 
and where you're typing, which is really, really nice.
You just want to move a few characters over.
You don't have to shift your entire hand over, and it's kind of nice to have that.
Some Vim experts actually map their arrow keys to other functions
because they already have HJKL, and they can use their arrow keys for other functions as well.
HJKL moves you around while you're in normal mode.
If you want to go into insert mode and type something 
you can press i for insertion mode, and that puts you in insert mode,
and you can type something, and you type escape
to get from insertion mode back to normal mode.
Escape always sends you back to normal mode
no matter what mode you're in, and if you want to leave
you can type in :wq enter, 
and the way you can remember this is w stands for write, 
and q stands for quit, so :wq,
and colon runs the command, and so :wq enter to run the command
saves and exits.
If you open the file up we'll go through the first exercise together. 
Step 1 is surviving in Vim,
and hopefully you guys all have this open.
Good, and we have some simple exercises for you to get used to using these keys.
HJKL really doesn't seem intuitive at first, 
and this is one of the initial blocks that people have while using Vim 
is using these keys to navigate around.
It's not intuitive, but once you get used to it
it really integrates into your mind, and many people often find themselves
using J and K in Word or also in Chrome, 
and actually people have created this Chrome plugin called Vimium
which you can use in Chrome or Chromium to navigate through Chrome
using your keyboard using Vim-like shortcuts, 
so people are very enthusiastic about it, but go ahead and try and use
HJKL to navigate between the numbers just so you can get familiar
with how these things work.
Once you finish that, you can also go and play around with this maze I created
just for fun, and you can sort of follow the dotted line
and try not to crash into the walls
and try to make it to the end of the maze.
If you use shortcuts in Gmail or any number of different applications
you'll find that J and K, for example, are often used to move up and down,
and that basically comes from Vim essentially, so you have J and K, 
and it's really nice because you can move up and down without having to
move your hand back and forth from the arrow keys.
Now let's try going into insertion mode and getting out of insertion mode.
You'll notice that right now my cursor is a big, black box,
and when you go into insertion mode you should find that
it turns into a narrow cursor like you're used to.
If you're using Vim in the terminal you may not find this to be the case
because the terminal doesn't support this kind of thin cursor,
but if you're using MacVim or GVim on Windows
then you should find that it turns into a cursor, and that's a really quick visual indicator.
It's for you to tell whether you're in normal mode or insertion mode.
Let's go ahead and try editing this line.
Fill it in with your own information or whatever it is,
and how you can do that is you can move up to the end of the blank,
type i to get into insertion mode, and you can backspace 
and type in your name, and when you're done typing
you can press escape to get out of insertion mode,
and you can move over here, type insert to delete that,
type escape to get out of insertion mode, and you can type :w enter
to save the file. 
You can type in just :w to save the file.
You can type in just :q to quit without saving,
or you can type in :wq to save and quit. 
Granted, we don't want to quit yet, so you can type in :w.
And maybe that seemed a little inefficient at first
because to move to that spot you had to type J a bunch of times,
maybe L a bunch of times to get to that spot,
and you have to press backspace a bunch of times, and it seemed really inefficient
whereas normally you would probably move your mouse over there,
double click and start typing, and of course,
the thing is, when you first start with Vim and you only know these basic commands,
yes, it will be a little bit slower, but soon you learn some more powerful commands, 
which will allow you to move across documents much more quickly,
and you won't have to have this very cumbersome use of 
repeating Js and repeating Ks and Ls and so on and so forth.
Any questions so far? Solid.
Back to the PowerPoint.
Now we're going to introduce a few more commands for advanced movement.
You experienced having to type maybe L a lot or H a lot, 
and here are some more commands you can use to move around more quickly.
Suppose that your cursor is at the O over in the some in "This is some text,"
and here are 5 more commands you can use to move across the line more easily.
So ^ and $ move you to the beginning and end line,
and at some point when you learn regular expressions or other various things
you'll find that these sort of conventions are very common.
The ^ generally refers to the beginning of the line.
The $ usually refers to the end of the line.
These are very easy to remember, and similarly, b, e, and w, 
they're easy to remember as well because b corresponds to back or beginning, 
e corresponds to the end, which is the end of the word,
and w corresponds to the next word, so w for word.
And if you keep typing e or keep typing w
you notice you can jump between words, and it's much faster than just typing l.
Now, the next thing is the Operator-Motion Model.
This is actually one of the key reasons why Vim can be so powerful,
and it's because you can essentially compose different elements
into more powerful functions, so generally
when you do anything in Vim you can think of it as
combining an operator with a motion.
So far we've just dealt with motions, like HJKL. 
They move you around, and then b and w, they also move you around different ways,
but we haven't gone yet to being able to delete things or change things or whatnot,
and when you combine operators with motions then you're able to have
much more powerful effects, so for example, 
if I wanted to delete 2 words, I can use d for the deletion operator,
and I can add a 2 to indicate that I want to delete 2 words,
and then I include a motion.
I could include w to say delete 2 words.
I could use something like J to say delete downward 2 times
and delete 2 lines below me.
I could say d2b to delete backwards twice,
and there are even more motions than the ones that I've shown here
to really, really make your Vim editing much more powerful.
These are some of the most popular or the most common operators
and are pretty easy to remember as well.
D for delete, c for change.
C is basically the exact same thing as change except that
when you use c you automatically get put into insert mode afterwards.
Normally when you use deletion you use it from normal mode, 
and after you delete something you stay in normal mode.
But if you want to change something, you can use c 
to delete something and then immediately go into insertion mode
so you can start typing and fix something.
Y is for yanking.
It's basically Vim's notion of copying.
For example, if I want to copy the next 2 words I can do y2w or something like that,
and v is for highlighting, essentially.
And also what's really cool is if you type an operator twice
it will act on an entire line, so if I want to delete an entire line
I can type dd to delete the entire line, 
or if I want to copy the entire line I can use yy or something like that.
Let's try this some more.
There's an exercise here for an advanced movement operator as well
and even some other things I'm not going to cover in the PowerPoint today,
but you can feel free to look over them later and play around with them
and get familiar with them.
Operators and motions.
For example, suppose there are these 2 words you just want to delete in the sentence.
What you'll want to do is you'll want to make sure that your
cursor is at the beginning of the first word you want to delete,
and you can type in d2w, and it will delete those 2 words for you.
And using this exact command, d2w, may seem a bit cumbersome
and pointless, but it's supposed to express the idea 
that you can compose these different things, and also just in case
you do happen to be in the situation, then you can end up using dw or d2w,
which I do use occasionally. 
Dd and cc I use much more often because you can act on entire lines.
For example, let's say I have this random list here,
and maybe I want to delete the line that says Huskies because it doesn't fit, just because,
and I don't want it to go into insertion mode afterwards.
I want to delete it, in which case I could type dd,
and that will delete the entire line.
But let's say now there's this other line that I want to delete,
but I also want to fix it and rewrite it, in which case I'll try something like cc,
and that deletes the entire line but puts me in insertion mode, as you can see, 
and I can fix it from there.
And there are many, many different ways you can combine operators and motions,
and as you use Vim more and more you learn more about different ways
you can use these commands, and you'll become better and better. 
Awesome, now here are some other assorted commands 
that also are very, very commonly used and also will help you become better at Vim.
For example, we talked about lowercase v for visual highlighting.
We can also use uppercase V for visual line highlighting.
That way you can highlight entire lines at a time much more easily.
You can use o when you're in normal mode to basically insert a new line
below the line you're currently on and put it into insertion mode to start typing.
You can use lowercase x to delete the character that's currently under your cursor.
You can also use u to undo, which is kind of nice,
and you can use capital A to append to the end of the current line,
so perhaps your cursor is somewhere on the line, and you want to add to the end of the line.
Then you can type capital A to immediately jump to the end of the line 
and go into insertion mode so you can add to the end of the line.
I think I have a few more exercises for that which we can try out.
Actually, no, I don't. Not for those.
But we have some more exercises for the next section which we'll see.
But these you can try out yourself as well.
For example, if you type in regular v, you'll get regular highlighting that you're used to like this.
If you type in capital V, you can get full line highlighting like that,
or if I type in o it basically will open a new line for me, which is kind of nice.
X I can use to delete characters on the cursor.
I can press x a bunch of times and delete stuff.
I can press u to undo all that.
A, I can type in A to immediately go to the end of the line,
and I also included a few other nice keys 
that you can also look at after the presentation to learn some other commands.
[Student] What's replacement about?
[Brandon L.] Replacement is also really cool
and basically if you go somewhere—
have you ever—people don't really use it that much anymore these days,
but have you guys ever seen in Microsoft Word if you type insert
you suddenly go into this weird mode where every single character you type
basically replaces the next character?
I don't know if you guys have ever seen that. 
This is similar to that. 
For example, if I want to overwrite what I currently have for some reason
I can type in capital R, and I'll now be in replace mode, as you can see in the bottom left.
If I type stuff it overwrites things, and I can also backspace, which is kind of cool,
and I can undo that.
I don't use that very often, but every now and then it becomes handy.
Let's talk about text objects, which is one of my favorite things about Vim, honestly.
Text objects are basically this notion of defining some sort of chunk of text
inside your text file, and I'll go over these keys first,
and then I'll give you some demonstrations for you to more fully understand what that means.
W refers to a word, like a text object as a word,
so some word as you normally know it, 
and if there's punctuation that's next to the word
then it won't include the punctuation generally.
But for example, if you want to include the punctuation for some reason
or you're doing something that's not English and you want to include
all the characters that are delineated by whitespace 
you can also use capital W to get everything that's surrounded by whitespace.
You can also use parentheses to refer to a text object
that's inside a set of parentheses.
You can do the same thing with square brackets, angle brackets, 
braces, and quotes as well, and finally,
you can also use t to refer to some tag.
Let me show you how that actually plays out.
For example, this is my very first example in my showcase.
There's some word in this sentence which I want to change,
and we talked about W being used as a motion,
but you can also use it as a text object like so.
For example, one way I could change Barack's name or basically modify it
is I could go to the beginning of this word and type in c for change and w for word,
and that will delete the entire word and put it into insertion mode.
But it's very annoying for me to have to go to the beginning of the word all the time.
That's really annoying. I don't want to have to do that.
I can actually be anywhere inside his name
and type in ciw, and the i stands for inner, 
so if I type in ciw it stands for change in a word,
or in other words, change the word I'm currently inside,
and it will give you the exact same effect.
You can fix that. 
Similarly, perhaps I have some case like this 
where for some reason there are some delimiters or something like that,
or there's some punctuation, and I wanted to delete the whole thing.
If I type in ciw it will delete what's inside these delimiters, 
but I want to get the whole thing that's surrounded by whitespace, 
so I can use ciW to delete the whole thing. 
I'll type in five, do the same thing, and type in six. 
Similarly, if I want to change something that's inside the parentheses
I can type in ci) to delete everything that's inside the parentheses
and then fill in whatever I want to do, like int argc, whatever you want to do,
and similarly, the same thing with quotes. 
I can fix text that's inside of quotes,
and I can also change things that are inside of tags.
Does that make sense?
Another thing you can do is you don't have to use c.
You can also use d if you want to delete what's inside these tags. 
If I want to delete the text inside a tag I can use dit to delete that tag.
I can also use vit to highlight what's inside the tag, for example.
The other thing is instead of using i in the middle you can also use a,
which stands for all, so remember if you use i it will refer to the text object
or use a text object that's contained inside the delimiters, 
but if you use a it will include that text as well as the delimiter itself.
For example, if I want to highlight this text and the surrounding tag
I can use vat, v for visual highlight, a for all, and then t for the tag.
You can do something like that as well.
Maybe I want to delete this google.com with the quotes around it as well,
and I can use something like da" to delete all of that, for example.
Any questions so far?
I realize that I'm dumping on you a lot of Vim commands all at once,
and this is basically because I want to expose you to all the different commands
and give you an introduction to what these commands do in Vim.
But what's going to have to happen is when you go back 
you're going to have to slowly pick up these commands one by one.
You don't want to overwhelm yourself, because that's too overwhelming.
If you want to pick up Vim what you have to really do is 
start with the first basic set, and I've broken it up into lessons for you
that make it easier for you to follow, and try and master each set of commands
one by one, one at a time.
It's too much to do everything at once, so what you should do when you go back is
go to the first lesson, which is HJKL, insertion mode, escape into normal mode,
and saving and quitting, and that's all you really need to survive in Vim,
and get really, really comfortable with that. 
And after a few days once you're very comfortable with that
you should move on to the more advanced motion operators,
the more advanced motions, and on to the operators and so on and so forth. 
I know I'm going through a lot, but I want to get you exposed to a lot of different things.
But in case you're up for the challenge, I can show you 
how you can put this all together using the most of what we just learned today,
and you can also follow along if you want, 
but I created some random exercise.
Suppose we have some sort of roster like this 
where you have these blocks with people's names, 
houses, years, and concentrations and whatnot,
and you want to add a new entry for yourself.
What's the best way for you to do this?
Let's do it like this. 
Why don't we first highlight this block.
You're going to select it, and we can press y to yank it, 
so now we can basically copy and paste this template first
without having to rewrite the whole thing from scratch.
Now I'll press j down a bunch of times to go down,
and I'll press p to paste it.
Maybe I'll add some extra lines for a space,
and let's say now I want to change your name to someone.
Then I can go here and type in c2w like before to change that name.
Bob Jones, who knows. 
Maybe if I want to change the house I can go into the middle here 
and type in ciw and put Cabot House.
Here may be an example of where you may want to consider using
the replacement mode to change your graduation year if you want,
or we can also use ciw as well.
It doesn't matter. 
Maybe you want to change your major.
And that's what you do.
And maybe I will give you a demonstration of what I might do
if I were—if you continue to learn more advanced commands in Vim 
because we're barely scratching the surface here,
and I could do this sort of editing even faster than I've shown you here
because I know more commands that I haven't gotten a chance to show you for today.
For example, if I want to highlight this I can do it like that,
move down.
I've added my own mapping
to basically automatically highlight stuff for me
and I can—I don't know. 
I mean, this is just trying to show you that you can really, really 
start editing things more quickly, especially when you have all these different fields,
and normally what you might be doing is sort of clicking
and highlighting and then typing.
You can do everything all at once, copying and pasting,
moving up and down between the file, 
just delimiting stuff, formatting stuff.
All this can be done much, much more quickly in Vim.
Just one last little thing which I think may be really nice,
which is find and replace.
Those of you who have used Gmail shortcuts
you'll also know that if you type in / it sends you to the search box,
and in a lot of different applications in general you'll find that slash
or maybe control slash or something to do with slash
will put you into some sort of search mode,
and that also basically comes from Vim essentially
because the slash operator or the slash command allows you to input
some sort of string that you can use to search within the document.
You can n and capital N to go to the next and the previous occurrence, 
and you can also use something similar to search and replace within the document.
I'll give you a demonstration on what can be done.
Maybe if I want to find all instances of the string yank within this document
I can type in /yank, as you can see in the bottom left corner,
and when I type enter it highlights all instances.
I can use basically n to jump back and forth between all those different instances,
and let's suppose I want to search and replace yank with some random word
that's also in the document.
I can press : to start typing in some sort of command within Vim,
some command that's more sophisticated than moving around 
or deleting or something like that.
I will type in the % sign to refer that I want to use this substitution command
on the entire document.
Type in s for substitution. This comes from PEARL essentially.
And this next command is a pretty standard command that you'll find in PEARL
instead and in other programming languages that you'll 
probably start to learn as you do more computer science,
and I can basically use / to type a /, type in the word I want to find and replace
and then type another / and type in whatever word I want to replace that word with.
Who knows, maybe nyancat.
I do that, and then you'll see that everything got replaced.
But you'll notice that there's this word here.
For some reason some of our instances of yank are replaced but not all,
and that's because the way this sort of language has been written
not only in Vim but in other places is that when you run the substitution command
it will only substitute the first instance of that word in each line,
so if I want to actually substitute all instances of the word on every single line
then I have to do the same thing but add a g flag at the very end,
which stands for global, and then that will actually 
substitute all instances.
And once you learn the regular expressions, there's actually a CS50 seminar
on regular expressions, so if you're curious about regular expressions
you can also take that seminar and learn more.
But for example, let's say I have a list of names like this,
and I want to rearrange it, so instead of having last name, comma, first name
I want to rearrange it so it says first name, space, last name.
You can actually use this command right here
to essentially do that for you, so as you can see here 
we start with an s, which means we want to do a find and replace,
and then I added a flag called \v, which is something called this magic flag 
in Vim which allows you to do regular expressions more easily.
Then from there we basically match for any single line
that is of the form a set of words, comma, space, and a set of words or a set of letters.
\w+ basically means one or more characters,
and the parentheses are used to capture these groups
so I can refer to them later.
As you can see, once these capture 2 blocks of words 
that are separated by a comma, space 
then what we want to replace it with is I use \2 and \1
to refer back to the words that were captured
in the actual find command,
and so \2 refers to whatever is contained in the second set of parentheses,
\1 refers to whatever is contained in the first set of parentheses,
and as you can see, I'm putting 2 before 1 separated by a space
to try and move the first name before the last name.
Does that make sense?
And what I can do is I can hide these first so I only run this command on these lines,
and then I'll type in this command.
You can see it does exactly as divided.
And again, this is barely scratching the surface,
and if you go to the regular expression seminar then I'm sure you'll learn 
a lot more different things you can do with regular expressions,
and this basically starts to hint at what you can really do with Vim 
if you really start to become a master of it.
Perhaps you want to refactor something, and you want to find all instances
of some function that follows some sort of naming pattern.
You want to change the way that's named.
You can write some sort of command like this to do that for you,
and that's the sort of flexibility you can get within Vim
that you're just not going to get in other text editors.
That basically covers all the things I wanted to teach you today in today's seminar,
but again, I really want to stress that if you want to pick up Vim 
it is true that there's a slightly higher learning curve in the beginning.
In the very beginning you will be slightly less productive
than you probably would be otherwise, but if you stick with it 
within a few days or a few weeks, depending upon how fast you learn,
you'll become much, much faster, and I tried to give you a demonstration
on what Vim is capable of but I didn't really get a chance to show you 
what happens when I normally—for example, in class I type into my notes with Vim.
That's because I can work so much faster in Vim than I can with any other program.
It is literally a 2 or 3 time productivity increase in Vim compared to other programs,
not only with the speed of text editing but also in the way I can move around the document
and the way I can access other files on my computer
and jump in between them and so on and so forth.
It's just a really, really huge productivity boost.
But when you're learning, you want to make sure you learn one thing at a time,
and try not to overwhelm yourself with too many different commands
because there are a lot of commands and a lot of different shortcuts you can learn in Vim.
You want to pick up a little at a time and over the years
gradually amass a larger and larger understanding of Vim.
If you want to continue to learn more, these are some of the more advanced topics
you can continue to learn about, and even this barely scratches the surface.
Here are some popular plugins you can feel free to check out and Google them
and play around with them if you want to and just see what sort of plugins are out there.
And then at the end of this PowerPoint, I include a bunch of links 
to various resources you can use to learn from. 
Here are a bunch of different links you can use to get started with Vim.
They have different tutorials and whatnot,
and finally, these are some more advanced things you can use 
to also check out as well,
some screen casts and some more advanced techniques in Vim 
and also some different pieces of source code you can look up on GitHub
just to look at other people's configuration files 
or to look at plugins in Vim and so on and so forth.
That's all I have for you.
[CS50.TV]
