While editing code I always need this feature: create a new line after current line, move cursor to a new line (saving curent indention!) and remain in normal mode. For example (assuming █ is a cursor):
function a() {
foon█tion()
}
After I type the command, I need to turn out like this:
function a() {
foonction()
█
}
I can achieve the same effect if I, for example, press <Enter><Esc>
while being in Insert mode with cursor on the end of a line. The o
command also acts similar, but it deletes indention after I quit insert mode. So I need a single keypress to insert one line down.
inb4 nmap: I know how to map a command for doing such thing, but I'm wondering if there is a standard way to do this.
Press Enter to insert a blank line below current, Shift + Enter to insert it above.
vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.
In insert mode, Ctrl-o escapes user to do one normal-mode command, and then return to the insert mode. The same effect can be achieved by <ESC> ing to normal mode, doing the single command and then entering back to insert mode. Ctrl-i is simply a <Tab> in insert mode.
The "o" command creates a new, empty line below the cursor and puts vim in Insert mode. Then you can type the text for the new line.
To insert at the end of a line in Vim, use the A (uppercase) key. Pressing uppercase A will put you into insert mode and move the cursor to the end of the line. This is particularly useful when you are properly navigating your file in NORMAL mode.
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.
Also you can use <C-o> o
combo
There is no such key, but it turns out that we can do this - sans indentation - with four keystrokes: :pu_<Enter>
This is a vim faq question, where the answer is to use the Ex command :put
:
12.15. How do I insert a blank line above/below the current line without entering insert mode?
You can use the ":put" ex command to insert blank lines. For example, try
:put =''
:put! =''
For more information, read :help :put
:put
puts the text from the given register after the current line and leaves you in normal mode. :put!
puts the text on a line above the current line.
The examples above are using the expression
register =
to send an empty string to the put
command. We can trim this down by using vim's black hole register, _
: :put _
.
Finally, this can be abbreviated to: :pu_<Enter>
and :pu!_<Enter>
.
See also: :help registers
.
Finally, note that this is also a feature of Tim Pope's unimpaired plugin.
From the unimpaired README:
There are linewise mappings.
[<Space>
and]<Space>
add newlines before and after the cursor line.[e
and]e
exchange the current line with the one above or below it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With