""" General
set nocompatible            " No compatible mode with the old Vi
filetype plugin indent on   " Useful plugins
set history=2000            " keeps 2000 commands in mind (q:)
set encoding=utf-8          " Specify the utf-8 encoding
set fileencodings=ucs-bom,utf-8,latin1 " Allows encoding detection

""" Interface
syntax on                   " Color syntax for numerous languages
set number                  " Display line number
set numberwidth=3           " Minimal number of columns of the line number
set tabpagemax=50           " Vim can open up to 50 tabs
set wmh=0                   " window min height
set t_Co=8                  " 8 colors only for desert colorscheme
colorscheme desert
highlight clear             " Reset all highlighting to the defaults.
" Status info
set showcmd                 " Useful live command status
set ruler                   " Useful highlightned status line
set laststatus=2            " Always displays the status
" Tab list
set tabline=%!ShortTabLine()

""" Keyboard / Mouse
set backspace=2             " Compatibility mode for the OS X bs behavior
if has("gui_running")
    set mouse=a             " Enabling mouse
else
    set mouse=""            " Disable mouse
endif

""" Editing
" Indentation
set autoindent              " Autoindentation refering to the previous line
set smartindent             " Syntax auto indent, ie following braces...
" Tab
set expandtab               " Tabs with spaces, not tabs
set smarttab                " After a blank line, reset the autoindentation
set softtabstop=2           " One tab equals to two spaces
set shiftwidth=2            " Same as above (may be redundant)
" Special chars
set list                            " Shows trailing chars
set listchars=tab:\ \ ,trail:.      " How special blank chars are displayed
set showbreak=>>\                   " String to put at the start of lines that have been wrapped
" Search
set incsearch               " Incremental search
set ignorecase              " Ignoring case in a pattern
set smartcase               " Override above if an Uppercase is supplied
" Completion
set completeopt=longest,menu,preview " Brings a cool completion view
set wildmode=list:longest,full " Autocompletion settings

" When editing a file, always jump to the last cursor position
if has("autocmd")
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
endif

""" Keyboard shortcuts
""" Auto brackets/parenthesis/tags/quotes...
inoremap (( ()<Esc>i
inoremap [[ []<Esc>i
inoremap {{ {}<Esc>i
inoremap "" ""<Esc>i
inoremap '' ''<Esc>i
inoremap `` ``<Esc>i
inoremap << <><Esc>i

""" Functions
" Append modeline after last line in buffer.
" Use substitute() (not printf()) to handle '%%s' modeline in LaTeX files.
function! AppendModeline()
  let save_cursor = getpos('.')
  let append = ' vim: set ts='.&tabstop.' sw='.&shiftwidth.' tw='.&textwidth.' sts='.&softtabstop.' '
  $put =substitute(&commentstring, '%s', append, '')
  call setpos('.', save_cursor)
endfunction

" tabline setup
function! ShortTabLine()
    let ret = ''
    for i in range(tabpagenr('$'))
  "Select the color group for highlighting active tab
  if i + 1 == tabpagenr()
      let ret .= '%#errorMsg#'
  else
      let ret .= '%#TabLine#'
  endif

  "Find the buffername for the tablabel
  let buflist = tabpagebuflist(i+1)
  let winnr = tabpagewinnr(i+1)
  let buffername = bufname(buflist[winnr - 1])
  let filename = fnamemodify(buffername, ':t')
  "Check if there is no name
  if filename == ''
      let filename = 'noname'
  endif
  if strlen(filename) >= 8
      let ret .= '['.filename[0:4].'..]'
  else
      let ret .= '['.filename.']'
  endif
    endfor

  "After the last tab fill with TabLineFill and reset tab page #
  let ret .= '%#TabLineFill#%T'
  return ret
endfunction
"""EOF
