views:

2159

answers:

6

Is possible to insert a linebreak where the cursor is in Vim without entering into insert mode? Here's an example ([x] means cursor is on x):

if (some_condition) {[ ]return; }

Occasionally, I might want to enter some more code. So I'd press 'i' to get into insert mode, press enter to insert the linebreak and then delete the extra space. Next, I'd enter normal mode and position the cursor before the closing brace and then do the same thing to get it on its own line.

I've been doing this a while, but there's surely a better way to do it?

+10  A: 

For the example you've given, you could use 'r[Enter]' to replace a single character (the space) with Enter. Then, 'f[space].' to move forward to the next space and repeat the last command.

Depending on your autoindent settings, the above may or may not indent the return statement properly. If not, then use 's[Enter][Tab][Esc]' instead to replace the space with a newline, indent the line, and exit insert mode. You would have to replace the second space with a different command so you couldn't use '.' in this case.

Greg Hewgill
I think this is the easiest solution. Thanks Greg.
Mark A. Nicolosi
'f[space]' should be equivalent to 'W' (i.e. move to the next whitepsace)
Nathan Fellman
'W' moves one character too far, it moves to the next character *after* the next whitespace.
Greg Hewgill
+5  A: 

Here's how to create a macro that inserts a newline at the cursor whenever you press 'g' while not in insert mode:

From within vim, type:

:map g i[Ctrl+V][Enter][Ctrl+V][Esc][Enter]

Where:

  • [Ctrl+V] means hold the Ctrl key and press 'v'
  • [Enter] means press the Enter key
  • [Esc] means press the Esc key

You'll see the following at the bottom of your vim window until you press the final Enter:

:map g i^M^[

Explanation:

[Ctrl+V] means "quote the following character" -- it allows you to embed the newline and escape characters in the command.

So you're mapping the 'g' key to the sequence:

i [Enter] [Escape]

This is vim for insert a newline before the cursor, then exit insert mode.

Tweaks:

  • You can replace the 'g' with any character that's not already linked to a command you use.
  • Add more to the command, e.g. f}i^M^[O -- This will find the } and insert another newline, then escape from insert mode and Open an empty line for you to enter more code.
  • You can add the command to your .vimrc or .exrc file to make it permanent. Just omit the colon from the beginning, so the command starts with "map"

Enjoy!

Adam Liss
You answered the badly phrased question in the subject, instead of the one in the posting. What he wants to know is how to easily get rid of the whitespace that was at the point where the line was broken but no longer serves a purpose.
Aristotle Pagaltzis
Thanks. Updated the "Tweaks" section to accomplish what (I think) the author requested. When I tested it, vi automatically removed the extra spaces for me; use 'x' to delete them if yours doesn't. And why do vi commands look like modem line noise? :-)
Adam Liss
+1  A: 

Vim will automatically kill any whitespace to the right of the cursor if you break a line in two while autoindent (or any other indentation aid) is enabled.

If you do not want to use any of those settings, use s instead of i in order to substitute your new text for the blank rather than just inserting. (If there are multiple blanks, put the cursor on the leftmost and use cw instead.)

Aristotle Pagaltzis
A: 

This mapping will break up any one-line function you have. Simply put your cursor on the line and hit 'g' in normal mode:

:map g ^f{malr<CR>`a%hr<CR>`a

This assumes that you have a space after the opening brace and a space before the closing brace. See if that works for you.

Lucas Oman
+3  A: 

If you're usually expanding a one line block to three lines, a simple substitution would work well. Change the opening bracket into bracket/return, and the closing bracket into return/bracket.

The command for substituting bracket/return for bracket looks like this:

:s/{/{\r/

Since you want to use this often, you could map the full sequence to an unused keystroke like this:

:map g :s/{/{\r/ ^M :s/}/\r}/ ^M

Where you see ^M in the sequence, type [Ctrl-V], then press enter.

Now with your cursor anywhere on your sample line, press the 'g' key, and the carriage returns are added. Note: you might want to use the command ":map g" before you do your own map -- just to make sure the 'g' key isn't already in use.

slothbear
+1  A: 

A simple mapping to break the line at the cursor by pressing Ctrl+Enter:

:nmap <c-cr> i<cr><Esc>

essentially enters 'insert' mode, inserts a line break and goes back to normal mode.

put it in your .vimrc file for future use.