kickstart.nvim icon indicating copy to clipboard operation
kickstart.nvim copied to clipboard

Thoughts on adding auto-indentation when entering insert mode via `a` or `A` on a blank line?

Open Crypto-Spartan opened this issue 10 months ago • 8 comments

I have the following in my personal config:

local function InsertModeWithIndent(key)
    local current_line = vim.api.nvim_get_current_line()
    -- %g represents all printable characters except whitespace
    if string.len(current_line) == 0 or string.match(current_line, '%g') == nil then
        return [["_cc]]
    else
        return key
    end
end
-- Auto indent on empty line with 'a' or 'A'
vim.keymap.set('n', 'a', function() return InsertModeWithIndent('a') end, { noremp = true, expr = true })
vim.keymap.set('n', 'A', function() return InsertModeWithIndent('A') end, { noremp = true, expr = true })

This function checks to see if the line is completely blank when you press a or A. If the line is blank, it will execute "_cc instead, which "changes" the line and puts the blank line into the black-hole register (to avoid messing with your other registers). "Changing" the full line automatically puts the cursor at the correct indentation level. And of course if there are any characters on the line, it will execute a or A as normal.

Also, the reason for adding expr = true is because the function returns a string that is an expression to be executed.

Crypto-Spartan avatar Feb 18 '25 05:02 Crypto-Spartan

Thanks for your contribution!

This is neat but it's a very special case change.

I'll need to see quite a number of "Heck yes merge this please!" before I'll feel confident enough to merge it.

feoh avatar Feb 18 '25 05:02 feoh

I understand. My thoughts were that it wouldn't break any functionality since it only impacts blank lines and if you want to enter insert mode at the beginning of the line, i is still available. I figured I'd throw it out there since it's something that was frustrating to me and this was how I fixed it. All good if the collective decides against it.

Crypto-Spartan avatar Feb 18 '25 05:02 Crypto-Spartan

Thanks! I have nothing against it so, other folks from the community, please speak up if you want this!

feoh avatar Feb 18 '25 05:02 feoh

Let's say if we don't hear anything by next week I'll close this. Feel free to poke other folks into commenting if you really want this :)

feoh avatar Feb 19 '25 19:02 feoh

I think this would be a really nice feature to have. However, I could see this getting in the way of some people's coding when theyre explicitly trying to avoid the indentation

CWood-sdf avatar Feb 19 '25 20:02 CWood-sdf

That's my concern too. Thanks for chiming in.

feoh avatar Feb 19 '25 20:02 feoh

-- Auto indent on empty line with 'a' or 'A'

Have you tried S in normal mode? does the same thing you want without any additional code.. on both empty and non empty lines. Or am I tripping?

[!note] Edit - ok my mistake, it does the same thing, except putting any contents into the blackhole register which messes anything you yanked

hrutvikyadav avatar Feb 21 '25 05:02 hrutvikyadav

That's my concern too. Thanks for chiming in.

Thanks for your contribution!

This is neat but it's a very special case change.

I'll need to see quite a number of "Heck yes merge this please!" before I'll feel confident enough to merge it.

would a wiki with useful 'recipe' snippets be a nice middle ground for this sort of thing. That keeps the config nice and svelte but also gives interested parties 'additional reading'.

It might give a nice jumping off point for people to grow their config without adding clutter.

rfletchr avatar Feb 21 '25 09:02 rfletchr

@rfletchr Maybe create a discussion in the main Neovim discussions?

I don't have the power to create wiki or discussions here. I've got one super power - I CAN MERGE!

And that's it :)

The rest of the 'maintainers' don't really check in at all. Ever :)

feoh avatar Apr 09 '25 18:04 feoh

I'm sorry I think this is going to cause too many problems for folks relying on the default behavior.

I don't mind the idea at all but I'm afraid you'll need to stick to your personal configs for this.

Closing. Feel free to re-open if you feel strongly and can get a bunch of folks behind you to support this change and make me feel confident I won't get pilloried for it :)

feoh avatar Apr 09 '25 18:04 feoh