Basically, I need to indent a block of R code. Unfortunately, vim's tab size is bigger than the two spaces that I'm using. Is it possible to accomplish this without hitting the space bar twice for every line I need to indent?

The motivation for this may be of note: I originally wrote the script in Rstudio before moving it to a server. Many indents just go away when I edit the server-side file with vim. However, if I transfer the file to my laptop (via winSCP) and open it in rstudio, all the indents look perfect!

  • why don't you change the tabstop and shiftwidth options?
    – Mass
    Mar 1, 2018 at 16:33
  • that would probably work, but not the best solution since I only want to change it for .R files specifically (since I use vim to edit other types of code too)
    – 3pitt
    Mar 1, 2018 at 16:38
  • 2
    @MikePalmice To follow Mass's suggestion you can create ~/.vim/after/ftplugin/r.vim containing setlocal tabstop=X which will change the value only for the buffers with the filetype r (Also this is worth reading before modifying tabstop)
    – statox
    Mar 1, 2018 at 17:19
  • @statox thanks! if you want to paste into an answer, I will accept it
    – 3pitt
    Mar 1, 2018 at 19:43

1 Answer 1


First, you probably want to set tabstop and shiftwidth to 2 to have the indentation work as you want.

Now you want these settings applied only for the files with the filetype r, the best way to do that is to create a custom ftplugin. To do that you want to create the file ~/.vim/after/ftplugin/r.vim.

This file will be sourced when the filetype of a buffer is set to r and after the ftplugin which comes with Vim by default has been sourced (this way you don't loose the already existing settings).

In this file you can simply add:

setlocal tabstop=2
setlocal shiftwidth=2

Here you need to use setlocal instead of set to make sure that the settings will only apply to the current buffer and not to all buffers.

Some interesting related help topics:

And a good read about indentation settings in Vim: /r/vim wiki

  • If OP is working with spaces instead of tabs, it’s generally preferable to leave tabstop at 8, and set shiftwidth and softtabstop to the same (eg 2). Then you’ll want expandtab as well. Otherwise youre using hard tabs that are made to look like 2 spaces.
    – D. Ben Knoble
    Mar 2, 2018 at 8:59
  • Just noted OP says « pseudo indent »...
    – D. Ben Knoble
    Mar 2, 2018 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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