在vim中缩进HTML

时间:2010-07-28 13:48:59

标签: html vim indentation

我不知道这是否可行,但有没有人知道会支持这种情况的缩进脚本?

(|是光标)

给定的

<div>|<div>

如果按Enter键,我想看

<div>
  |
</div>

而不是

 <div>
|<div>

3 个答案:

答案 0 :(得分:3)

delimitMate将为您解决此问题。 但是,您需要另外两个设置......

添加&gt;:&lt;配对到html文件列表:

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

并告诉它在插入

后要添加的模式
au FileType html let b:delimitMate_expand_cr = "\<CR>\<CR>\<Up>\<Tab>"

(这将是,而不是插入两个,插入两个,按下,然后插入一个标签)

答案 1 :(得分:2)

结束了与brian Carpers的回答,只是稍微修改了

"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>

答案 2 :(得分:1)

你可以这样做:

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>