Lesson 6 of 26
Indentation
In this lesson
Roy indents lines of an HTML file by selecting them with Shift-V and pressing the greater-than sign, de-indents them with the less-than sign, and repeats the last action with the dot key. For automatic indentation he adds filetype indent on to the vimrc file, so that pressing = on a selection makes Vim detect HTML and indent it. He notes that the indentation settings can be overridden in a special vim directory.
Transcript
[00:07] (external link, opens in new tab) Okay, we still want to get a bit more productive with the HTML for example. So the indentation kind of sucks. So one way to do indentation in Vim is to select the rows you want to indent. So shift V in this case. Let's say all the style and then press shift and the right arrow or the dot. So like this shift and then kind of the larger than sign. And if you want to repeat the command, you can you can actually press dot with nothing and the last thing you did will be repeated again.
[00:40] (external link, opens in new tab) So if I press dot again, you can see that this is repeated. I'm going to press undo once and undo twice. And then if I want to select stuff inside the style, I say I'll say shift V again, select these rows and then shift larger than and then I get the following. And if I want to indent everything inside HTML, I can select this stuff and press shift larger than again. And I want to indent just the body. I can select stuff here and indent, etc., etc.
[01:13] (external link, opens in new tab) So, that's one way to indent things. But I also like to be able to get Vim's help to do all these things. So, I'm going to if I want to de-indent, I can select everything. So, shift V and then GG to go all the way to the beginning of the file and then shift smaller than. So, and I can just repeat this again with a dot. So, let me try this. Shift V then shift G and then unindent and then dot to repeat unindent.
[01:48] (external link, opens in new tab) You can see that this has just repeated the de-indentation of everything. Now there is an auto indentation in Vim. So I can select everything and ideally if I press equals Vim should just detect how to indent HTML. But I don't have indentation turned on and detection in Vim. For that I'm going to have to save the file and then edit the vimrc file. So e home
[02:20] (external link, opens in new tab) Dot vimrc file. And here I need to add one command which is file type indent on this this basically tells Vim to start detecting which file types I'm loading not just for syntax but also for indentation. And if I close Vim write q so right quit and then I in open index html again it doesn't indent automatically.
[02:55] (external link, opens in new tab) I still have to tell it to indent. So I'm going to say shiftv and then shiftg to go all the way down to the file. Now if I press equals, vim detects that this is the HTML and indents things for me. And I can change how it indents things because all of my indentation information can actually be overridden in a special directory which is called the vim directory. We're going to use that for a bit very very soon. So that's how we fix indentation. Let's look at color schemes and other bunch of other useful configuration in the next