tags:

views:

82

answers:

3

I don't know if this is possible, but does anyone know of an indent script that will support this scenario?

(| is cursor)

given

<div>|<div>

if I press enter, I want to see

<div>
  |
</div>

instead of

 <div>
|<div>
+2  A: 

delimitMate will take care of this for you. You will however, need two additional settings...

add the >:< pair to the list of html files:

au FileType html let delimitMate_matchpairs = "(:),[:],{:},>:<"

and tell it what pattern you'd like to add after inserting a

au FileType html let b:delimitMate_expand_cr = "\<CR>\<CR>\<Up>\<Tab>"

(this will, instead of inserting two a , insert two s, press up, then insert a tab)

sleepynate
Note that this is probably going to be removed in future versions. See [here](http://github.com/Raimondi/delimitMate/commit/89cc59821753eec21eb050ee1ba8db671deac39d#L0L331).
Randy Morris
Wow, thanks for the heads-up, Randy.
sleepynate
voted this up, but ended up going with the second answer cause of that commit
Matt Briggs
+1  A: 

You could do something like this:

function! NewlineInTag()
    let lnum = getline('.')
    let cnum = col('.')

    let chars = strpart(lnum, cnum - 2, 2)
    if chars =~ '><'
        return "\<CR>\<ESC>\<UP>$o"
    else
        return "\<CR>"
    endif
endfunction

imap <CR> <C-R>=NewlineInTag()<CR>
Brian Carper
ended up slightly modifying this
Matt Briggs
+1  A: 

Ended up going with brian Carpers answer, only modified very slightly

"fancy html indenting
function! NewlineInTag()
    let lnum = getline('.')
    let cnum = col('.')

    let chars = strpart(lnum, cnum - 2, 3)
    if chars =~ '></'
        return "\<CR>\<ESC>\<UP>$o"
    else
        return "\<CR>"
    endif
endfunction

autocmd FileType eruby,html imap <CR> <C-R>=NewlineInTag()<CR>
Matt Briggs