I use the following code to make cc preserve the existing indentation when the line is not empty, and to use the calculated indentation (using indentexpr) when the line is empty.
function SmartCC()
if getline('.') !~# '\S'
return 'cc'
else
let spaces = repeat(' ', indent('.'))
return '0d$i' . spaces
endif
endfunction
nnoremap <expr> cc SmartCC()
The only problem I have with it is that, unlike the built-in cc, this inserts “real” spaces and not “tentative” spaces that get deleted automatically if nothing is typed on that line.
Is there a way to insert such tentative spaces?
Or maybe what I’m trying to achieve is already available by setting some secret option?
To make it clear, a plugin that removes all trailing spaces is not a solution.
Try the following hack:
. The idea is to set
&l:indentexpronly for the period of evaluationcc. As it is known thatccchecks indent only once and nothing triggers the check beforeccunder such circumstances then the&l:indentexpritself can be used to restore&l:indentexprback before indent is checked for the second time for another reason.