Indenting Multiple Lines in Vim

I’ve been working with Ansible of late, and just like Python it’s very finicky about indentation. When modifying Ansible playbooks in Vim I found the need to be able to indent blocks of code all at once. Vim to the rescue! Normally to indent1 one line in Vim you would put the cursor anywhere on the line that you’d like to indent and hit >​ (with < used to unindent). Now to indent multiple lines we can leverage Vim’s visual mode to select more than one line. Put the cursor on the first line you would like to indent and then type v to enter into visual mode. From here move the cursor down to the last line you would like to indent and press >.


Want to indent the same block of code twice? You may have noticed by now that once you send your indent command you’re then exited out of visual mode, meaning you’ll have to select which text you want to indent all over again. After you’ve used visual mode once, type ​gv and you you’ll re-enter it with your previous selection already highlighted!


  1. Indenting is set to 8 spaces by default, but this can be changed by modifying the shiftwidth setting in Vim. It can be changed on the fly to 4 by running :set sw=4 in Vim, or permanently by adding the line set shiftwidth=4 into your .vimrc file in your home folder.