Question

I currently have the following set in my indentation settings for C in VIM:

set cinoptions=l1

This allows autoindent to automatically handle indentations for case statements so I get the following type of alignment automatically:

switch(intForSwitching) {
   case 1: {
      // Comment
      // More comments
      break;
   }
   case 2: {
      //Comment
      break;
   }
   default: {
      break;
   }
}

However, this only works if each case (after the statement) is wrapped with braces, {}. I only use braces in case statements if I need to declare new temporary variables in just that case, as it introduces a new level of block scope. So, the following example, gives indentation I do not want, as the case statement is aligned with the code that it executes, which makes it harder to separate case statements from code chunks associated with them:

// This is what I get
switch(intForSwitching) {
   case 1: {
      // Comment
      // More comments
      break;
   }
   case 2:
   //Comment
   break;
   default:
   break;
}

// This is what I want
switch(intForSwitching) {
   case 1: {
      // Comment
      // More comments
      break;
   }
   case 2:
      //Comment
      break;
   default:
      break;
}

Additionally, ifdef statements are not working anymore. Previously, autoindent would align all preprocessor directives at column zero, ie:

char c;
if (c) {
#ifdef TESTING
   printf("%c", c);
#endif
}

Now, it aligns it with the code, which I do not want. ie:

char c;
if (c) {
   #ifdef TESTING
   printf("%c", c);
   #endif
}

Is there a way to retain the existing alignment for when I use braces to surround an individual case, and also have similar alignment for the case where I don't use braces?

Thank you.

Was it helpful?

Solution

In the end, I changed some environment variables and fixed everything, despite using the same VIM binary.

Previously:

DEV_PATH="/dev/bin:/dev/sbin:/dev/usr/bin:/dev/usr/sbin"
MACPORTS_PATH="/opt/local/bin:/opt/local/sbin"
SYSTEM_PATH="/bin:/sbin:/usr/bin:/usr/sbin"
PATH="${SYSTEM_PATH}:${DEV_PATH}:${MACPORTS_PATH}"

Now:

PATH="${MACPORTS_PATH}:${SYSTEM_PATH}:${DEV_PATH}"

I noticed this after issuing ctags commands from within VIM, and they were suddenly giving me grief on the command-line arguments used.

The other half, was deleting a script for viewing QML syntax which I removed from ~/.vim/plugins, which finally resolved my issue.

In any case, issue resolved. Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top