Html indentation in vim

I don't know if this is possible, but does anyone know of a backtracking script that will support this script?

(| - cursor)

given

<div>|<div>

if I press enter, I want to see

<div>
  |
</div>

instead

 <div>
|<div>
+3
source share
3 answers

You can 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>
+1
source

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

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

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

and tell me which template you want to add after insertion

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

(instead of inserting two a, insert two s, press up, then paste the tab)

+3
source

,

"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

Source: https://habr.com/ru/post/1756950/


All Articles