Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: C++ back indent with #

I use Vim in a C++ code with openmp sentences.

And in my ~/.vimrc

 set ai " auto indent

my problem: when I use an openmp sentence (this begins with #) the cursor jumps to the beginning of the line without the auto indent.

Example:

int main()
{
  int idx = 100;
#pragma omp parallel private(idx) // jump to begin of line

, when I like this:

int main()
{
  int idx = 100;
  #pragma omp parallel private(idx) // This is OK

Can I set this in the autoindent in Vim?

like image 313
JuanPablo Avatar asked Sep 18 '12 02:09

JuanPablo


People also ask

How do I indent C in Vim?

First, go to the start point of codes to be formatted, then press v to start selection. Second, go to the end point. Third, press = to format the codes that have been selected. All braces and indentations will be aligned.

How do I turn on auto indent in Vim?

How to Turn On Auto Indent in Vim. To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing.

How do I tab a whole section in Vim?

To tab or add the indentation at multiple lines, try “shift+dot” i.e., “.” Shortcut once. You will see it will add an indentation of one character at each selected line from the start. If you want to add indentation without stopping, then you have to try the “.” Key from the keyword after using “shift+.”.

What is Vim Smartindent?

By Juliet Kemp. April 20, 2021. Vim is a modal text editor that allows users to alter text in different modes. These different modes determine how the alphanumeric keys on keyboards function.


1 Answers

Vim puts a line in column 1 when it starts with # (preprocessor directives), if cinkeys contains #.
So you can remove # from cinkeys to disable this feature:

:set cinkeys-=0#
like image 93
kev Avatar answered Sep 20 '22 00:09

kev