0

I'm configuring my Vim editor. Now I want it to automatically indent when I press enter after a parenthesis (like in modern IDEs)

function test() {

    |

}

I already have autoindent and smartindent enabled. I think I used a plugin in an old Vim config, but I can't remember which one it was. I hope you can help me, thanks

3
  • 4
    What is the file type of the file you are editing? :set filetype? Jun 26 at 12:53
  • Cross-posted here: reddit.com/r/vim/comments/vl2xb1/autoindent
    – r_31415
    Jun 26 at 23:40
  • 'smartindent' should do this, so it sounds like your setting may be overridden by something else in your config. What is the output of the command :verbose set si? inde?
    – Rich
    Jul 28 at 8:24

1 Answer 1

1
  1. You're correct that smartindent and autoindent should do what you want, but it might be overruled by the archaic compatible setting.

Be sure you're not in vi compatible mode. You want set compatible? to return nocompatible. This means that Vim will not be backwards compatible to the old vi program. In older Vim versions, it started up in compatible mode by default and left it to the users' .vimrc files to include set nocompatible. Newer versions of Vim will automatically start up in nocompatible mode, which is what you want.

  1. Many common file extensions will get the filetype setting assigned. If :filetype returns detection: ON and indent:ON, then the filetype indentation might be setting the indentexpr to a function which should be better than smartindent, but maybe it's worse. What is your filetype? If you're writing Javascript, then the filetype should be javascript. If filetype detection and indentation is on, then the indentexpr should be GetJavaScriptIndent() which will do exactly what you want. I usually recommend not using autoindent and smartindent and instead use filetype indentation.

In summary, these two lines in a ~/.vimrc should help out:

set nocompatible
filetype plugin indent on
3
  • The quesion is tagged with "Neovim". So neither of these two apply.
    – Matt
    Jun 26 at 18:10
  • Even if it is not applicable to neovim, this is good info to know.
    – r_31415
    Jun 26 at 19:20
  • 1
    PS a vimrc really needs if &compatible | set nocompatible | endif, or nothing at all, because vim now automatically sets nocompatible when a vimrc is processed.
    – D. Ben Knoble
    Jun 27 at 20:05

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