Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a newline without entering in insert mode, vim

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>$ 
like image 469
helq Avatar asked Apr 21 '13 18:04

helq


People also ask

How do I add a line after a line in vim?

Now if you want to add line break after a specific pattern, you can easily add line break '\r' after the cur pattern above. For example, if you want to add line break after '}' character in file data. css, you need to go to open the file in vi editor. Go to command mode, by hitting Esc key.

How do I insert a blank line in vi editor?

Opening a blank line: Type o (lower case) to insert, or open, a blank line below the current line. Type O (upper case) to insert a blank line above the current line. Press ESC to get back to command mode.

Does vim add newline at end of file?

Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim.


2 Answers

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)

like image 118
TKrugg Avatar answered Sep 21 '22 03:09

TKrugg


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.

like image 26
Rasmus Larsen Avatar answered Sep 20 '22 03:09

Rasmus Larsen