Vim smart insert semicolon

前端 未结 7 826
遇见更好的自我
遇见更好的自我 2021-02-04 00:49

Is there a Vim plugin that can handle smart semicolon insertion, like the one in Eclipse?

Example (pipe character is insertion cursor):

foobar(|)
         


        
相关标签:
7条回答
  • 2021-02-04 01:16

    I use the following function and a mapping to insert a semicolon to the end of the line and to delete the remaning spaces:

    imap <leader>. <C-R>=Semicolonfun()<CR>
    fun! Semicolonfun() "{{{
      call setline(line('.'), substitute(getline('.'), '\s*$', ';', ''))
      return "\<End>"
    endfunction "}}}
    

    So if you have something like:

    log.warning("you miss the |identifier")
    

    Pressing . or ,. if you remap the leader, you get the following:

    log.warning("you miss the identifier");|
    
    0 讨论(0)
  • 2021-02-04 01:19
    inoremap <expr> ;<cr> getline('.')[-1:] == ';' ? '\<Nop>' : '<End>;<CR>'
    

    The code above will check if there is ; at the end of the line.

    If ; not exists, then it will add ; otherwise do nothing.

    0 讨论(0)
  • 2021-02-04 01:25

    You should try Cosco.vim plugin.

    0 讨论(0)
  • 2021-02-04 01:25

    For vscode vim users:

    "vim.insertModeKeyBindings": [
          {
            "before": [";", ";"],
            "after": ["<Esc>","A",";"]
          },
        ],
    

    I was inspired by romainl's answer above, but <C-o> seems have a switch problem in vocode vim, so just use <Esc> will work.

    0 讨论(0)
  • 2021-02-04 01:27

    I use this mapping:

    inoremap <leader>; <C-o>A;
    

    It's not ; because I use semicolons often and in more than one context.

    • <C-o> is used to input a single normal mode command.
    • A; means "add a ; at the end of the line".
    0 讨论(0)
  • 2021-02-04 01:28

    I want to do the same thing, I works on it whole night, tons of code, but at last, I got a simple solution.

    inoremap ;<cr> <end>;<cr>
    

    and if I am in a brace, I want to add a semicolon or a dot at the end, like this

    foo({
      |
    })
    

    I press ;;<cr> or ..<cr> to do this

    inoremap ;<cr> <end>;<cr>
    inoremap .<cr> <end>.
    inoremap ;;<cr> <down><end>;<cr>
    inoremap ..<cr> <down><end>.
    
    0 讨论(0)
提交回复
热议问题