Implementing Standard and Smart Indentation Rules in Vim
In this guide you'll learn how to automate your indentation rules inside of a Vim file, including standard and smart indentation behavior.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

One thing that you may have noticed in some of our previous guides, especially the ones where we were dealing with code, is that the indentation was kind of annoying.

So lemme open up our Ruby file here, and let's come down and make a new method. So it's gonna be def new method, and you'd expect that when you hit return, that I would come down and it would automatically give us two spaces, because that's what you'd get in Sublime and that's what you would get in pretty much any code text editor.

However, Vim needs to be told to do that, 'cause by default, it's not going to automatically pick up those kind of indentation rules. So I'm gonna close outta here, and we're gonna open up our VimRC file. And coming down all the way to the bottom, I'm going to set some additional values and these are ones that specifically deal with indentation.

If you switch over to the Vim settings and you come into this Vim settings file about halfway down, I have some indentations rules. So right here, I'm gonna grab all of this code here and then just paste it in and let's just walkthrough and see exactly what this is doing.

So we have a comment that shows that this has to do with indentation, and then we have some rules. So we're gonna set autoindent, we're gonna have smartindent, which means that it's going to automatically know exactly when it should use indentation and when it should not indent at all, this works, I would say probably about 95 times out of a hundred.

And then we have smart tab, that is going to convert our tabs into spaces, because I'm not sure if you've ever noticed in a Ruby file, but especially this happens a lot on GitHub, where you'll have a Ruby developer who pushes up a file, and all of a sudden, even though it looked fine on his screen, or it looked fine on your screen, in GitHub, all the text is scattered all over the place. That usually is the result of not converting your tabs into spaces, so smarttab, and some of these, have to do with that.

So we have shiftwidth at two, softtab stop at two, and then tabstop at two. What this means is that we are doing two spaces per tab, so whenever you press tab, if you do press tab, then it's gonna convert that into two spaces instead of sometimes you may a default of four if you're a Javascript developer or something like that.

So that is where you can set these values. We do the expended tab which is just another setting that I have in here, and then we have some other items. So filetype plugin on and filetype indent on. This is just going to give us the ability to have these plugins associated with our tabs and also, this is kind of a neat thing, where we can display tabs and trailing spaces visually.

So what this means is that you're gonna be able to see when you have a tab and you'll be able to see exactly how many spaces that that equates to with this little dot. So right here at the end we have this little dot, what that is saying is that I want you to set, every time there's a space, set this little dot inside of it so we can designate whether it's a space or not. So that's something that's very helpful and in the demo you'll see how you can do that.

So next we have nowrap which means do not wrap lines, 'cause if you wrap lines, sometimes this can be nice, but other times wrapping can actually be kinda confusing 'cause imagine that you have a method call that's really long after you pass in all the arguments and you have split your panes up so that they're kind of thin, then when you pasted that in, it may actually look confusing 'cause it may look like the line breaks are in the code, when really it's just because they're wrapping.

And then set linebreak says wrap the lines at convenient points, not in the middle of a word or something like that.

large

So now that we have all of those set, let's come back into our Ruby file and let's see what this looks like. So I'm gonna come down again to the end, and this time I'm gonna try out my new method. So I'm gonna say def new method, and then look at that. Now we have our indentation, so by default, it saw that we're using Ruby, it took in all of our rules, and now I can say puts asdf, and there you go.

Where before that was taking us all the way back here, which was very annoying, now it knows that because we're using puts right here, it has the intelligence to know that oh they probably wanna do another rule. So we could say something like this, like puts this, and we're good. Now what happens when we type end? Notice how this is taking advantage of the smart indent? So I typed end, I didn't type anything else, and it automatically knew because we're using Ruby, that the end needed to line up right here.

large

So this is an incredibly helpful tool and that's why I always set the smart indent because it gives you some very nice, not quite code completion, but some kind of an indent completion, as it were. So that is how you can set all of your indentation rules inside of Vim.