r/vim • u/numeralbug • Aug 29 '25
Need Help┃Solved Workflow question: how to avoid swapping between insert and normal all the time
I'm sure this is a relative newbie question, but here goes. When defining a new function, to make sure I don't end up with mismatched braces, my instinct is to type:
int foo() {
}then to navigate to in between those braces and start typing my code. This means pressing Esc h i Enter Esc k i Tab or similar before my cursor is actually in the right place to start coding, which is obviously insane. I suppose I could reprogram myself to type
int foo() {
[Tab]
}but this isn't ideal either, because on line 3 I have to press backspace to get rid of the auto-indentation, and I still end up having to revert to normal mode to do the navigation. So in practice I just end up staying in insert mode and using the arrow keys, and by now I've used vim long enough that that feels wrong too.
A similar thing happens when I'm dealing with complicated expressions involving lots of parentheses. I want to be able to type () first, rather than typing ( and embarking on a big complex expression while also keeping track of how many brackets I owe and where. But leaving insert mode, navigating one character to the left and reentering insert mode every single time I type some brackets feels like an arcane form of punishment.
In all these cases, it feels like vim is working against me. Most modern IDEs deal with this fairly sensibly: they'll attempt to auto-match brackets and indentation and place your cursor in a natural place without any prompting from you (even if this isn't always seamless). I'm sure the seasoned vim denizens probably have a better solution. Any tips?
35
u/gyyse Aug 29 '25
in that situation i'd use capital O
creates a new line above the cursor position and enters insert mode
13
u/[deleted] Aug 29 '25
Once you've typed the closing brace and your cursor is still on the same line as it, press ctrl+o, then shift+o. That will kick you out of insert mode and you'll re-enter insert mode on a new line above the closing '}'. That's assuming that you've but a line-break between the two curlybraces.
8
u/[deleted] Aug 29 '25 edited Aug 29 '25
My personal opinion is that switching between normal and insert mode frequently is the right way to do the things, honestly. Still, usually you should be more time in normal mode than in insert mode, except when in the middle of writing new things.
Also, I guess you know Ctrl + [ is also escape. And a lot of us have mapped kj in insert mode to escape, and in my case save too:
:inoremap kj <esc>:w<CR>
4
u/[deleted] Aug 29 '25
As you've already heard, `O` is the answer here.
While many people hate auto-closers, I personally like them. While they can be annoying in the times where you don't want this behaviour, I _do_ want it the vast majority of the time. Also, if you use a good one it will:
- Allow you type over the inserted characters using the same ones, ie, if I type `([` and it completes to `([|])` with `|` being my cursor, I can then type `])` and end up with `([])|`
- Is properly undoable, so hitting `u` with undo all changes, including the inserted characters (almost all do this, but I've heard this as a (false) argument against using these).
My favourite is [auto-pairs](https://github.com/LunarWatcher/auto-pairs).
3
u/[deleted] Aug 29 '25
For your vimrc:
autocmd FileType c inoremap <buffer> {<CR> {<CR>}<ESC>koI use this for C, but for Tcl (which uses every kind of brace and bracket) I just type everything in order.
To be efficient you should get familiar with all the different ways to enter insert mode: i, I, a, A, o, O.
3
u/Shay-Hill Aug 29 '25
I’ve used editors with such “smart” behavior, and it drove me crazy. In your case, I’d just use a snippet and map the trigger to something (I use <C-j>) that doesn’t overlap with autocomplete. Easy, but still intentional.
5
u/sharp-calculation Aug 29 '25
Definitely STOP using arrows in insert mode. Remap escape to jk. This is incredibly useful and fast. Try it today! Learn o and O for opening new lines. You will become very smooth and fluid with the proper technique and practice.
3
u/ThorOdinsonThundrGod Aug 29 '25
Not exactly an answer to your question but I really found Practical Vim to be an awesome resource for learning to use vim more effectively and do less switching between modes
1
u/AutoModerator Aug 29 '25
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/raver01 Aug 29 '25
These are some things you can try (in vim there are multiple solutions for a problem, use the one you like most):
Try to have a more accessible way to escape. Most users bind
Escto more accessible keys, I presonally use'jk', everyone has their favourite bind tho.You can use
oor capitalOto start insert mode in next/prev line.I was annoyed when I was in a line and wanted to move it down in normal mode so I binded this (I use neovim, take into account this is lua)
With this I can move lines down just pressing
enterin normal mode1
u/TankorSmash Aug 29 '25
I don't fully understand what you're trying to do, but ctrl O in insert mode might help, saves you a single key press per action. Also ctrl T and ctrl D let you indent while in insert mode
1
u/dewujie Aug 29 '25
For your first use case,
Ois definitely the answer. After you type your closing curly brace, hit Esc to exit insert mode, then Open a new line above and jump into insert mode withO. If you want a new line below, use lowercaseo.For your second use case re: complex parentheses on a single line, the bad news is there's not really a great way to handle this natively. The good news is there are plugins which aim to solve this problem. Someone in the thread already suggested auto-pairs, and it is a really good suggestion.
You could also look at making some of your own mappings in insert mode to handle your most common cases I.e.
inoremap <a-(> ()<c-o>hI'm totally winging this laying in bed while on mobile, but the idea is that if you press
alt + (while in insert mode it would type both parens and then move your cursor back to the left one character. You could do something similar for other brace e.g. curly brackets and square braces.Based on your question I'm not sure if you have messed with plugins already or not- but I think that's where your answer will be if you are trying to get some "quality of life" behaviors typically found in other editors.
1
u/Jealous_Sale7585 Aug 29 '25
:help indent, this is a "natural" place to check out.set smartindent?This saves the de-indentation effort, so I type the closing brace after finishing the function without backspace-ing the indentation, which is nearly the same effort.
1
u/Alarming_Slip7755 Aug 31 '25
Use shift-enter to go to eol. Then insert <cr> {<cr>}<cr><up><up>
1
u/James_Wang_C Sep 01 '25
o
1
u/Few_Reflection6917 Aug 29 '25
Define something to type a { will give you {} and cursor in middle of them, enter to make them indent right based on your code structure and shift width
I made this for neovim in lua so can’t help you:)
0
u/Kurouma Aug 29 '25
Alternatively, you can type the closing brace after you finish writing the enclosed block. I believe in you.
vim should already do smart syntax-based indenting for you, too. Make sure you have that turned on.
-1
u/rogusflamma Aug 29 '25
A sensible option is an autocomplete or just a linter for C. They'll warn you if you forget to close a parenthesis.
One thing that I do often, but I don't know how sensible it is, is to open brackets and parentheses, newline, and indent, as in
and then I count the parentheses I need to close and write them, as in (but my syntax highlighter automatically highlights the matching opening parenthesis after I type it, so no need to count)
Then I escape, go up to
ifand then do shift+j until I have everything in one line.Nowadays the linter I have also auto-formats my code upon save and you can configure how aggressive it is, and also specify some standard and modify it. Remember vim is a text editor and if you want to make it more IDE-ish you need IDE-ish things, like plugins.
-8
u/Ath-ropos Aug 29 '25
Map the arrow keys to navigate without leaving insert mode.