vim screenshot enter image description here

My problem

I am working on a js file, using eslint as my linter with the ale plugin, configured using the airbnb standard which mandates 2 space indentation.

When I open a new line by hitting o, the new line is indented with 4 spaces, regardless of the filetype. I have set tabstop=4, but I want this to change for js files according the the rules of my linter.

My .vimrc

" General settings
syntax on
set rulerformat=%l,%c%V%=%P
set ruler
set relativenumber      " Current line number is absolute, others relative. Makes j-k jumps easier
set number              " Show line numbers
set tabstop=4           " Show tabs as 4 spaces
set shiftwidth=4        " Indent with 4 spaces when > is pressed
set expandtab           " Indent with 4 spaces when > is pressed
set cursorline          " Highlight current line
filetype on             " :help filetype - Auto-detect the current file type, by name and content
filetype plugin on      " :help filetype - Enable loading the plugin files for specific file types

" Appearance
colorscheme tayra

" Plugin installation using Plug
call plug#begin('~/.vim/plugged')
Plug 'w0rp/ale'
Plug 'junegunn/fzf.vim'
Plug 'mhinz/vim-sayonara'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
call plug#end()

" Plugin options
let g:ale_linters = {'javascript': ['eslint']}
let g:ale_javascript_eslint_executable='/usr/local/bin/eslint'

What have I tried

Searching the net, and adding filetype on and filetype plugin on to my .vimrc file.

My question

How can I set the indentation to automatically suit the current file type?

  • is your vim always doing 4 spaces for a tab, or is the problem just the new line ? – Doktor OSwaldo Jul 21 '17 at 8:12

Vim help says:

You can enable loading the plugin files for specific file types with:

:filetype plugin on

If filetype detection was not switched on yet, it will be as well. The result is that when a file is edited its plugin file is loaded (if there is one for the detected filetype).

Plugin file is just a regular Vim script. To make indentation 2 spaces long for js files you need to create a file .vim/ftplugin/javascript.vim:

setlocal tabstop=2
setlocal shiftwidth=2 

You want to use setlocal instead of set because set changes both local and global values (thus changing the value for all open buffers).

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.