I am wanting to wrap the <h2> and the <div> blocks below and surround them both with another <div>


    <h2>Im a heading</h2>
    <div class="row">
        im some content


From normal mode, I am entering -- VISUAL LINE --

  • V, select block, S (enter surround mode), then type <div>

vim-surround successfully wraps the block, but loses all indenting, e.g:


<h2>Im a heading</h2>
<div class="row">
im some content


I notice elsewhere, people are saying capital S tells vim-surround "surround AND indent", but for me capital S only enables vim-surround and lowercase s doesnt work at all.

This SO answer suggests there is some possible setup required to enable the indenting behaviour for S.
Or is it an operating system specific issue? (My OS is Ubuntu 14.04)

How can I get vim-surround's automatic indenting to work?

  • Might have something to do with tags you're surrounding them with: How do I get vim to auto indent block-level html elements? ... Nope. Vim indents it just fine for me on Ubuntu 14.04, patch level 843 (pi-rho's PPA). – muru Sep 20 '15 at 23:33
  • @muru I struggled to believe it would not work for block - but that it would work for inline elements, thanks for eliminating that as a possibility. I have got a solution. will post an answer now. – the_velour_fog Sep 21 '15 at 0:05

I found a workable solution by changing the following vim core settings.

:filetype indent on
:set smartindent  

In my case I was editing an HTML file - which vim had no trouble detecting as the correct filetype, but if you were editing any other HTML-containing file that vim didn't recognise correctly, you may need to add:

:set filetype=html

Those changes enabled visually selecting a block of text and hitting = to automatically indent HTML tags correctly.
After that vim-surround also automatically indented when surrounding code - similar to pressing =.

More background

Reading :help filetype gave an excellent overview/introduction to what was going on.

For example, to see what your current filetype setting is:

To see the current status, type: 
The output looks something like this: 
        filetype detection:ON  plugin:ON  indent:OFF
| improve this answer | |
  • 1
    You didn't have filetype indent on? O.o I usually have filetype plugin indent on so that all three (filetype detection, filetype-based plugin and filetype-based indentation are enabled). – muru Sep 21 '15 at 0:21
  • @muru I wasnt explicitly setting it in my .vimrc - other than that I dont know how to check whether filetype indent is on or off. I just know when I applied :filetype indent on and :set smartindent auto indent started working – the_velour_fog Sep 21 '15 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.