Vim自动缩进大括号错误

时间:2012-03-15 05:54:35

标签: javascript vim

我已经启用了智能缩进和自动缩进功能,但它仍然使用大括号(至少在JavaScript中)很奇怪。如果我输入:

if(x==y){

然后我明白了:

if(x==y){
  }

我不知道为什么。我不得不退缩一次才能恢复正常:

if(x == y){
  //yay!
}

语法。是否可以将光标放在if块中,光标位于上面注释中的第一个/

这是我当前的.vimrc文件。

"Color syntaxing of course
syntax on

"colorscheme molokai

:colors molokai

"Lots of undo history... just in case
set history=700

"Set to auto read when a file is changed from the outside
set autoread

"highlight the current line
set cul

"set color of the highlighted line
hi CursorLine term=none cterm=none ctermbg=234

"auto indent
set autoindent
set smartindent

"Soft tabs FTW
set expandtab
set smarttab                                        

"Size of the (soft) tabs
set shiftwidth=2                                    
set softtabstop=2

"Show line numbers
set number

"Set line number colors to something other than that god awful orange
hi LineNr ctermfg=234 ctermbg=black


"Change the color of the matching brackets
highlight MatchParen cterm=bold ctermfg=black ctermbg=DarkYellow

"Keep at least 5 lines of space above and below and then left and right
set scrolloff=5
set sidescrolloff=5

hi StatusLine cterm=NONE ctermbg=darkgreen ctermfg=white

"Scrolling with your mouse!
set ttymouse=xterm2
set mouse=a

let g:molokai_original = 1

" Find file in current directory and edit it.
function! Find(name)
  let l:list=system("find . -name '".a:name."' | perl -ne 'print \"$.\\t$_\"'")
  let l:num=strlen(substitute(l:list, "[^\n]", "", "g"))
  if l:num < 1
    echo "'".a:name."' not found"
    return
  endif
  if l:num != 1
    echo l:list
    let l:input=input("Which ? (CR=nothing)\n")
    if strlen(l:input)==0
      return
    endif
    if strlen(substitute(l:input, "[0-9]", "", "g"))>0
      echo "Not a number"
      return
    endif
    if l:input<1 || l:input>l:num
      echo "Out of range"
      return
    endif
    let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*")
  else
    let l:line=l:list
  endif
  let l:line=substitute(l:line, "^[^\t]*\t./", "", "")
  execute ":e ".l:line
endfunction
command! -nargs=1 Find :call Find("<args>")

2 个答案:

答案 0 :(得分:0)

我不认为Vim默认情况下可以这样做。请参阅my answer to this similar question以获得足够好的解决方法。

修改

映射就像一个宏,可以立即重放你的按键:

  1. 插入模式的起点,|是您的光标

    if(x == y){|}
    
  2. <CR>

    if(x == y){
    |}
    
  3. <CR>

    if(x == y){
    
    |}
    
  4. <C-o>k(可能是<Esc>k

    if(x == y){
    |
    }
    
  5. <Tab>

    if(x == y){
      |
    }
    
  6. 实际上,它非常愚蠢,并且应该在您拥有Ctrl密钥和Return密钥的任何地方工作。

    EndEdit中

答案 1 :(得分:0)

我建议使用vim javascript indent plugin JavaScript Indent,这在编写javascript时给了我非常满意的结果。