Let's say we have the following code:
function test() {
const a = {<HERE>}; // `<HERE>` is the caret position
}
When enter
is pressed, the leading whitespaces are added as expected:
function test() {
const a = {
<HERE>};
}
When the next enter
is pressed:
function test() {
const a = {
<HERE>};
}
How to go back to the previous line without loosing leading whitespaces? When I press <C-o>
and k
:
function test() {
const a = {
<HERE>
};
}
Seems it doesn't depend on the filetype. I have the same behaviour in TypeScript and Rust files (I haven't checked other types).
The behaviour you describe is intentional and very much in line with the whole idea of using insert mode only to… insert text. After all, no one wants rogue whitespace in their files, right?
What you are actually trying to do is expand the pair of braces around the cursor, from state A:
{<CURSOR>}
to state B:
{
<CURSOR>
}
all without leaving insert mode.
Pressing <CR>
twice, and then moving the cursor to the line above is not a good approach because there is nothing on that line and the best k
can do is move the cursor to column 1.
The proper method is to…
press <CR>
once to put the closing brace on its own line:
{
<CURSOR>}
open a new line above the current line with :help O
, which respects the current indentation rules:
{
<CURSOR>
}
All the following command sequences do the job in slightly different ways:
<CR><C-o>O
<CR><Esc>O
<CR><C-c>O