Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any default key in vim to create a new line after current line and remain in normal mode?

Tags:

vim

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.

like image 888
gvlasov Avatar asked Jul 22 '12 21:07

gvlasov


People also ask

How do we make a new line under the current line in Vim?

Press Enter to insert a blank line below current, Shift + Enter to insert it above.

What is the default mode of vim editor?

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.

What does Ctrl o do in Vim?

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.

What is the single vim command you can use to create an empty line on line 21 and enter 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.

How do I insert a line at the end of vim?

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.

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

Also you can use <C-o> o combo

like image 93
AYarulin Avatar answered Oct 01 '22 20:10

AYarulin


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.

like image 30
pb2q Avatar answered Oct 01 '22 20:10

pb2q