Oscar Godson
Oscar Godson

Reputation: 32716

Vim auto indents curly brackets wrong

I have smart indent and auto indent turned on, but it still acts weird with curly brackets (at least in JavaScript). If i type:

if(x==y){

then I get this:

if(x==y){
  }

And I have no idea why. I constantly have to backspace once to get the normal:

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

syntax. Would it at all be possible to have it also put the cursor inside the if block with the cursor at the first / in the comment above?

Here's my current .vimrc file.

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

Upvotes: 3

Views: 2837

Answers (2)

evnu
evnu

Reputation: 6680

I recommend using the vim javascript indent plugin JavaScript Indent, which gave me very satisfying results when programming javascript.

Upvotes: 1

romainl
romainl

Reputation: 196516

I don't think Vim can do that by default. See my answer to this similar question for a good enough workaround.

EDIT

The mapping is like a macro that replays your keypresses instantly:

  1. starting point in insert mode, | is your cursor

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

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

    if(x == y){
    
    |}
    
  4. <C-o>k (could be <Esc>k)

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

    if(x == y){
      |
    }
    

It's very dumb, actually, and should work anywhere you have a Ctrl key and a Return key.

ENDEDIT

Upvotes: 0

Related Questions