Vim: indenting a level deeper when enter is pressed (and some conditions are met)

StackOverflow https://stackoverflow.com/questions/22104443

  •  18-10-2022
  •  | 
  •  

Вопрос

I'm writing a plugin for the dg language, what can I do to auto-indent when a line terminates with some specified symbols?

For example, if the line terminates with -> or ~> or => and I press Enter, then the cursor would go on a new line indented a level deeper. I guess I should put the code in the indent directory?

Это было полезно?

Решение

The built-in indent methods (cindent, autoindent, smartindent) won't suffice for a custom language. You need to write your own indent plugin, which would go into ~/.vim/indent/dg.vim.

Basically, you need to write a Vimscript function that examines the current (and previous) line(s) and determines the indent level from this. This function is then installed into 'indentexpr'. See :help indent-expression, which unfortunately only recommends to have a look at the existing indent scripts.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top