Video summary
This video provides a practical guide on configuring Vim specifically for programming tasks, using JavaScript as the primary example. The presenter begins by highlighting built-in features like syntax highlighting and comments but notes that line numbers are missing from the default setup. He demonstrates how to enable line numbers using the `set number` command, explaining their utility when debugging compiler errors, while also showing how to disable them with `set no number`. Additionally, he discusses incremental search, a feature where typing a search term immediately jumps to the first match; viewers can toggle this behavior on or off depending on their preference using `set incsearch` or `set no incsearch`.
A significant portion of the tutorial focuses on managing indentation styles to adhere to specific coding conventions. The presenter addresses an issue where pressing the Tab key indents by eight spaces, which he finds excessive. He solves this by setting the `tabstop` and `shiftwidth` options to four spaces and enabling `expand tab` so that the Tab key inserts actual spaces rather than literal tab characters. This ensures consistent formatting across different style guides that prefer smaller indentation units. The video illustrates how these settings automatically adjust code structure, such as properly unindenting closing braces, making the code cleaner and easier to read.
To avoid having to manually re-enter these configuration commands every time Vim is launched, the presenter introduces the `.vimrc` file located in the user's home directory. By adding the desired `set` commands to this hidden file, users can permanently customize their Vim environment so that line numbers, indentation rules, and search behaviors are applied automatically upon startup. The video concludes by encouraging viewers to explore the extensive documentation available for Vim options, noting that while only a few settings were covered, there are many more customization possibilities to help tailor the editor to individual workflows.
Read the full video transcript
In this video, I'm going to talk about
setting options for Vim.
And I'm going to be doing this in the
context of writing programs, because
that's what we use Vim for quite often.
I'm going to open up a JavaScript file.
And you can see that Vim has some things
built in to help us programmers.
Keywords are highlighted.
Comments are colorized.
And so on.
One thing that we don't have with the
default setup of Vim on this system
is line numbers.
And it's really nice to have line
numbers.
When you have a compiler error in line
58, you want to see, "Oh, yeah, this is
line 58."
To turn on numbering in Vim, I press
colon to go to last line mode.
And set
number.
I like that a lot more.
If you happen to not like the line
numbers, you can turn them off again by
saying colon, set no number.
In general, if you have a Vim option
that's either on or off,
you use the prefix no to turn it off.
And set no number gets rid of the line
numbers, but I like them and I'm going
to turn them back on again.
There, I'm happy now.
The way Vim is set up right now, one of
the features that it has is something
called incremental search.
Let's say I'm looking for the word mean.
I type slash to go to last line mode,
and as I start typing the word mean,
you'll notice that the cursor jumps to
the first possible match. When I type A,
it gets to there, and I type N, and when
I press enter, the cursor remains at the
beginning of the first match that it
found.
Again, this is called incremental search
and some people love it a lot.
Some people aren't fans of it.
If you want to turn it off, you say set
no incsearch.
Now, let's go back to the beginning of
the line here and watch what happens
when I type /mean.
The cursor doesn't move, but when I
press enter, I do get to the first
occurrence of the word.
So, that's incremental search. Again, if
you're a fan of it, keep it turned on.
If you're not a fan, turn it off.
Okay, let's add another function here.
Um let's call it function geometric
mean.
And it'll take two parameters A and B.
And here's my opening curly brace. And
watch what happens the moment I press
enter, it indents, but it indents eight
spaces, which as far as I'm concerned is
a bit too much.
Now, every programming language has
what's called a style guide.
And some style guides say indent four
spaces. Some say indent two spaces. Some
people say indent eight.
And you want to be able to adjust this.
The way we do this is going to colon set
and then say set tabstop
equal four.
That means that every time I press tab,
it goes over to the next fourth
position.
I also want to set
shiftwidth
equal to four.
That's how far to indent every time you
do indentation.
Along with how far to indent, a lot of
style guides say when you press tab, you
should put the tab key into the file.
Other style guides say when you press
tab, it should expand into the correct
number of spaces.
If you want to expand the tabs into the
correct number of spaces, you set expand
tab.
So, now let me delete this line and
start it over again, and I'm going to
just have function geometric mean
with parameters A and B.
And now I get only four spaces of
indentation, which is what I want.
Let's let result equal math.sqrt.
Oops.
Capital math.sqrt. Thank you. Of A * B.
And then return result.
Put in the closing curly brace
and it automatically unindents for me.
And you'll notice here that the tabs
have been expanded because when I go
here and use the right arrow key,
there are really four spaces on this
line.
Let's write the file
and let's quit.
That's all well and good.
Now, let's say I want to add another
function
and I go back to Vim and enter this.
And now I come here.
Oh, wait a minute. My line numbers
aren't there anymore.
Hm, if I were to look for the word
result,
I have my incremental search.
Does that mean I have to retype every
one of those options every time I start
Vim?
That would be really terrible. There
must be a solution.
And in fact, there is.
Let's quit
and the exclamation point means quit and
don't write any changes.
In order to preserve the set options
from one session of them to the next.
I'm going to edit a file
that's in my home directory.
And its name is .vimrc. It begins with a
dot, so it's a hidden file.
And now, in this file, I am going to put
in the commands that I want to be done
every time I start Vim.
I don't use the colon in this case.
I want to set number
to get my line numbers. I want to set no
incremental search.
I want to set my tabstop to four.
I want to set my shiftwidth also to
four.
And I want to expand tabs into spaces.
If I want syntax highlighting, I have to
add another option here, and that is
saying when you recognize a file type,
use the plugin for that file type
for indenting.
And turn that on.
Now, I'm going to write that file and
quit.
And now, when I do Vim functions.js,
I have my line numbers.
I don't have incremental search. If I
search for result,
the cursor doesn't move until I press
enter.
And
let me insert a comment.
Harmonic mean is not
used often, but is here
for the sake of completeness.
And then I can add my function harmonic
mean
of A and B.
And I'm indenting only four instead of
eight. So, all the stuff that I wanted
to set up has been done for me and it
will happen every time I enter them.
By the way, here's the whole function in
case you wanted to know what the formula
was.
And now I can write and quit.
These are only a few of the options that
are available with them.
If you look at the documentation for
them, you'll see that there's an
enormous number of options available to
you.
In any case, this should get you started
with setting options in them.