Search code examples
vimcygwinindentationvi

how to get the same result of smartindent after :%left and gg=G


I am fairly new using vim, so bear with me if this is a very simple question. The thing is that I have set smartindent in my .vimrc (basically only this one w.r.t. indentation) and it works as I expect, for instance after writing the code below in a js file it indents 2 tabs (or 8 spaces) spacing (I think default option):

 1 sjsjs function(sjdhd, jdjd,{
 2         aaaa,
 3         bbbb,
 4         cccc,
 5         dddd
 6 });

If I delete indentation with :%left and then automagically indent the whole file with gg=G I get:

 1 sjsjs function(sjdhd, jdjd,{
 2                 aaaa,
 3                 bbbb,
 4                 cccc,
 5                 dddd
 6                 });

if I use 'autoindent' and 'filetype plugin indent on' I get a strange behavior, it indents the row dddd, and if I add eeee it gives a double indentation (smartindent keeps the same indentation inside the curly brackets):

  1 sjsjs function(sjdhd, jdjd,{
  2         aaaa,
  3         bbbb,
  4         cccc,
  5                 dddd
  6 });

How can I get the smartindent way after :%left and gg=G? also, I want to tidy my codes up (not only in js, but c++, shell, html), so they look nicely indented (not too much indentation for instance, I think cindent produces too much indentation). Could you also suggest a nice indentation setup? I know this last question can have thousands of thousands of answers/ways, but still would like to have some suggestions.

Thanks in advance,

PS.

$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 28 2014 01:24:00)
Included patches: 1-417
Compiled by <[email protected]>


$ grep set .vimrc
set nocompatible
"set backspace=indent,eol,start
  set nobackup          " do not keep a backup file, use versions instead
  set backup            " keep a backup file (restore to previous version)
  set undofile          " keep an undo file (undo changes after closing)
set history=50          " keep 50 lines of command line history
set ruler               " show the cursor position all the time
set showcmd             " display incomplete commands
set incsearch           " do incremental searching
  set mouse-=a
  set hlsearch
  autocmd FileType text setlocal textwidth=78
"  set autoindent               " always set autoindenting on
  command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
set background=light
set number
set smartindent

Solution

  • After reading this and this, I found that the best is to use specific plugins related to the file format you're using. For javascript, this Better Javascript Indentation plugin does the trick. I just removed smartindent et al and used the plugin.