Insert a newline without entering in insert mode, vim
Asked Answered
G

9

100

I want insert newlines in normal mode in vim using Shift-Enter and Ctrl-Enter. I try some solutions and mixing solutions from Vim Wikia - Insert newline without entering insert mode but Shift-Enter and Ctrl-Enter didn't respond:

" put a new line before or after to this line
nnoremap <S-CR> m`o<Esc>``
nnoremap <C-CR> m`O<Esc>``

" reverse J command
nnoremap <C-J> vaW<Esc>Bi<CR><Esc>k:s/\s\+$//<CR>$
Grasshopper answered 21/4, 2013 at 18:4 Comment(4)
What is your $TERM (if *nix)?Madsen
echo $TERM -> xterm Why?Grasshopper
It may be relevant how the keys are interpreted. Do you use screen or tmux aswell? That being said, I tried a couple of the alternatives, and it would only work for map <CR> o<Esc> here.Madsen
well, I don't use screen or tmux with vim. And yes, map <CR> o<Esc> work. But, how map O<Esc>? . I'm sorry by my bad englishGrasshopper
R
98

My alternative is using oo (resp. OO) to insert a new line under (resp. over) the current through this mapping: nmap oo o<Esc>k (resp. nmap OO O<Esc>j)

Rainer answered 21/4, 2013 at 20:57 Comment(5)
Great answer. Just 'o' works and goes to insert mode too. ThanksSherasherar
The thing that was bothering me most with the other answers was the shortcut. This one favors my preferences.Macaroon
I did nmap <C-S-J> o<Esc>k since Shift+J deletes newline.Monomania
Another enhancement is to use nmap oo m`o<Esc>`` and nmap OO m`O<Esc>`` to preserve cursor position when opening newlines into different indentation levels, as described in these comments.Alanalana
You may also want to use a shorter timeout value than the default 1-second to more quickly go into insert mode with plain o and O. I like set timeoutlen=200. You'll just need to press the double oo quick enough, and this shrinks the window to execute other non-blocking time-outable commands sequences too.Alanalana
C
31

Yank an empty line and shift-paste it:

Starting with cursor on empty line:

yy + (shift + p)

"yy" yanks the line, and "shift + p" insert it below, without entering insert mode.

Chan answered 10/4, 2018 at 10:44 Comment(1)
(shift + y) then p as alternativeDarceydarci
P
10

Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today. (This particular case should work in GVIM, though.) Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This also applies to <Tab> / <C-I>, <CR> / <C-M> / <Esc> / <C-[> etc. (Only exception is <BS> / <C-H>.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals.

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

Publicness answered 21/4, 2013 at 18:40 Comment(1)
It's mostly due to historical reasons, but unfortunately quite hard to change.Publicness
C
4

How about this if you just don't want to press ESC

yypd$
Crystalloid answered 25/10, 2019 at 2:20 Comment(6)
since I have D to delete to the end of line, mine would be yypDHenden
The downside to this is you lose whatever is in your clipboard (yankboard? not sure what it's called in vi(m))Nicolanicolai
yypdd works well in different enviroments.Trichotomy
@Henden better yet: YpD (below) or YPD (above). Is a good aproach, but the problem is it doesn't take the cursor back to its initial positionSalesin
yankboard is absolutely hilarious, I'm only going to refer to it as that from now on.Haematinic
@Nicolanicolai it's called a register.Germain
C
2

I use :s/\n/\r\r/g (subsitute the newline with two newlines, which is the same as "o").

Cris answered 10/10, 2013 at 4:17 Comment(1)
needed for this a chained cabbrevCanine
C
2

This is what I use:

nmap <CR> :a<CR><CR>.<CR>

I tried nmap <CR> o<Esc>, but it made UI glitchy as it was switching to insert mode and back.

Constrictive answered 2/2, 2014 at 12:52 Comment(0)
D
0

You can copy an empty line to a register and use that. I have built the habit of copying a new line to the n register. You can copy a new line to a register, in this case n using: "ny You can paste the new line anywhere without leaving normal mode like: "np

Dasha answered 2/4, 2023 at 4:47 Comment(0)
U
-1

If you want to add a newline without the cursor jumping around you can:

(Written in Lua for Neovim, but you could convert it to Vim script.)

vim.keymap.set("n", "<CR>", "mao<esc>0<S-d>`a<cmd>delmarks a<cr>", { desc = "Add new line below" })
vim.keymap.set("n", "<S-CR>", "maO<esc>0<S-d>`a<cmd>delmarks a<cr>", { desc = "Add new line above" })

This uses marks to preserve your line and column position. And deletes any characters that are automatically added when opening a new line (such as comment characters).

  1. ma: mark current cursor position.
  2. o: open a new line and enter insert mode.
  3. <esc>: enter normal mode.
  4. 0<S-d>: go to the beginning of the line and empty it.
  5. `a: go to the original marked cursor location.
  6. <cmd>delmarks a<cr>: delete the mark.

Note that if you want to avoid mapping the carriage return in non-normal windows (e.g. hitting enter in the quickfix list should still take you to the file) you can:

-- Set keymap for normal windows only (e.g. not quickfix)
local normal_window_keymap = function(mode, lhs, rhs, opts)
  local merged_opts = vim.tbl_extend("force", { noremap = true, expr = true }, opts or {})

  vim.keymap.set(mode, lhs, function()
    local buftype = vim.fn.win_gettype()
    return buftype == "" and rhs or lhs
  end, merged_opts)
end

-- Open new line (like o/O) without moving the cursor, without entering insert mode and removing any characters
normal_window_keymap("n", "<CR>", "mao<esc>0<S-d>`a<cmd>delmarks a<cr>", { desc = "Add new line below" })
normal_window_keymap("n", "<S-CR>", "maO<esc>0<S-d>`a<cmd>delmarks a<cr>", { desc = "Add new line above" })
Urban answered 4/11, 2023 at 15:9 Comment(1)
Am I missing something, going back to the original mark and then deleting the mark? This just puts you back where you started? (I think the point was to be on the next line in normal mode)Haematinic
H
-3

You can use a hack to do this.

In edit mode, you can use p to paste the current clipboard. Since o adds a newline, you can use o<ESC>ddp to add a new line below the cursor; from there, p will add a new line until you delete something else.

Hylophagous answered 29/7, 2016 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.