I am Vim newbie, and I'm using MacVim on OSX Snow Leopard. One of the most common actions I have to take is to move the cursor to a new line but also move the text after the cursor to the new line. I know that pressing 'o' in normal or visual mode moves the cursor to a new line and switches the mode to insert.
What I'd like to do is move the cursor to a new line, and move the text after the cursor to that new line as well, preferably staying in the normal mode? Is this possible? How can I accomplish this task?
Depending on exactly the effect you want, either give the command
or the command
Thereafter, pressing
<F2>
will do the job with a single keystroke.But, of course, @Alex is right, too. Just pressing
o
thenEsc
whenever you need it isn't bad.So you want to move everything in the current line, which comes after the cursor to the next line? Read: insert a line break??
To map this sequence of keystrokes to a single key, follow @thb's suggestion and use the
:map
command:As I answered in this post, How do I insert a linebreak where the cursor is without entering into insert mode in Vim?.
Please try
Control + j
.You need to map some keys to do a line break at the cursor, I found the following mapping easy to use, just go to your vimrc and add this line:
:map <silent> bl i<CR><ESC>
to assign a line break at cursor to "bl" comboThe code below achieves the same behavior as "normal" editors (for the lack of better terms on the top of my mind) except that you'd have to press "enter" twice instead of once.
I also wanted to get rid of the space if it's right before my current character.
There might be an easier way and I totally welcome edits :-)
This remaps pressing enter twice to going to insert mode, placing a carriage return and escaping. The reason I'm using this mapping (enter twice) is because I was used to this functionality with other text editors by pressing a enter; also, typing enter twice is fast.
Another thing that I found useful in this context was allowing vim to move right after the last character (in case I wanted to move the last character to a new line). So I have the following in my
~/.vimrc
as well.Note that I'm using
nnoremap
(normal mode non-recursive) instead ofmap
(which is VERY dangerous) (check this out for more information on the differences http://learnvimscriptthehardway.stevelosh.com/chapters/05.html)If the cursor is on a
<space>
as in ([]
marks the cursor):the simplest is to do
r<CR>
, that is "replace the current character with a linebreak".Otherwise, use @knittl's solution.