0

If my cursor is at position * below, how can I enter insert mode at the beginning of the next line without creating a newline? o does what I want, except that it adds an additional unwanted line into the block that I then have to delete.

*let(:foo) do

 end
  • AFAIK there is no special command for this. Use ji maybe followed by <C-T> to get the indent. See :help i_CTRL-T. Maybe better: jA in case there are already leading spaces/tab. – Ralf Dec 30 '18 at 18:16
3

As Ralf said in the comments, there is no built-in command that does this. I would recommend using either ji, jI, or jA for this purpose, since those are all only two keys, and they're fairly normal vim idioms.

If you'd really like to do this with a single keystroke, you could always overwrite the o command to not make a newline. To do this, add one of the following lines to your .vimrc (depending on which behavior you want):

" Enter insert mode at the beginning of the next line
nnoremap o j0i

" Enter insert mode at the end of the next line
nnoremap o jA

" Enter insert mode at the first non-whitespace character
nnoremap o jI

(Note that the lines starting with " are just comments)

Unfortunately, this overwrites o which I think is a very useful command. Normally, to add your own shortcuts without overwriting the default ones, it's standard to use <leader>. However in this case, something like

nnoremap <leader>o ji

would be pretty pointless since it's not any shorter than just manually doing it with ji

  • Definitely entering overkill land here but you could make it shorter than the original command by picking a control+key combo that's either unmapped or mapped to something you don't use. Ctrl-J is just a synonym for j so there's that... – B Layer Dec 30 '18 at 21:48
  • @blayer Unfortunately, <C-j> can be hard to map to sometimes because the terminal interprets it differently. – DJMcMayhem Dec 30 '18 at 22:55
  • I use all manner of *nix and Win machines and have a <C-J> map...not once have I had an issue. Test it, sure, but it's pretty safe in my experience. Otherwise it's likely something that can be resolved (e.g. terminal configuration, disable a readline bind, etc.). Worst case, use Ctrl-whatever instead. :) – B Layer Dec 30 '18 at 23:29
  • @BLayer See this answer I use GVim, and it generally works pretty well for me, but I've definitely had some problems in various terminal emulators. – DJMcMayhem Dec 31 '18 at 3:55
  • I read that and said "Aha, Insert mode must be the difference...I don't use Ctrl-J there." But, alas, even with that everything still works fine for me on Win/gVIm, Cygwin and one instance of Linux I tried. Anyways, I don't mean to suggest that everyone else is imagining things! It's certainly worth pointing out as you've done. Cheers. – B Layer Dec 31 '18 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.