Vim cursor position after expanding html tag

后端 未结 4 1688
悲哀的现实
悲哀的现实 2020-12-31 10:02

I most IDEs and modern text editors (Sublime Text 3) the cursor is correctly indented after inserting a newline in between an html tag (aka \'expanding\" the tag):

B

4条回答
  •  粉色の甜心
    2020-12-31 10:33

    This little snippet will remap Enter in insert mode to test whether or not the cursor is between > and < and act accordingly if it is. Depending on your indent settings the \ may need to be removed.

    It will not play nice with other plugins that might be also be mapping the Enter key so be aware that there is probably more work to do if you want that compatibility.

    function EnterOrIndentTag()
      let line = getline(".")
      let col = getpos(".")[2]
      let before = line[col-2]
      let after = line[col-1]
    
      if before == ">" && after == "<"
        return "\\O\"
      endif
       return "\"
    endfunction
    
    inoremap   EnterOrIndentTag()
    

    I have only tested the simple cases (beginning of the line, end of the line, inside and outside of ><), there are probably edge cases that this would not catch.

提交回复
热议问题