I've found that indenting, with or without a selection, doesn't always work, and it seems to be because of these settings.
From what I can tell it's when the code is valid and correctly indented, but it doesn't happen everywhere. I have yet to find a pattern except for the examples below.
set filetype plugin indent on
set smartindent
Take for example this C++ code:
#include <iostream>
#include <sstream>
int main(void) {
std::string move;
int x, y;
char c;
while(true) {
std::cout << "Enter move (x,y): ";
std::getline(std::cin, move);
std::stringstream ss(move);
ss >> x; ss >> c; ss >> y;
std::cout << "x: " << x << "\n";
std::cout << "y: " << y << "\n" << std::endl;
}
}
Indents:
- Any correct code except
#include
, for example:- << or >> at
std::getline(std::cin, move);
- viB< at
std::cout << "Enter move (x,y): ";
, thus indenting the block
- << or >> at
- >> or >> if a
#include
is wrongfully indented - If
#
is removed from the include, making the code incorrect, >> works
Doesn't indent:
- >> at
#include <iostream>
- vip> at
:1
, thus selecting the includes and trying to indent them
If the indentation fails, the file is still marked as changed, even though no characters were actually changed.
- Is this the correct behaviour?
- Why does Vim discriminate between correct code?
- I would like to keep the
smartindent
feature, but still be able to manually indent code even if Vim think it's correct.
My Vim configuration if needed.