Reputation: 1011
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?
Upvotes: 23
Views: 28014
Reputation: 439
I have found some of the easiest ways to repeat a process that requires more than one step (i.e. move text to a newline, indent, etc.) is to record a macro, perform one command, then stop recording and type @a
on every line you want that to run.
e.g.
Turning
{"hi", "there", "you"}
Into:
{"hi",
"there",
"you"}
You could type (in normal mode):
qa <--- start recording
f [space] <--- move cursor to next space
d$ <--- delete from cursor to end of line
o <--- move cursor and create new line below current line
[esc] <--- exit insert mode (default when pressing "o")
p <--- paste the deleted contents^^^
Where f [space]
is the "f" key, followed by the space key.
Then you just move to each line you want to apply this and type (in normal mode):
@a
Or you could just move the cursor, go into insert mode, and press enter on each line :)
Upvotes: 1
Reputation: 597
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" combo
Upvotes: 0
Reputation: 14051
The 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 :-)
" in ~/.vimrc or ~/.vimrc.after if you're using janus
nnoremap <cr><cr> :call ReturnToNewLine()<cr>
function ReturnToNewLine()
let previous_char = getline(".")[col(".")-2]
" if there's a space before our current position, get rid of it first
if previous_char == ' '
execute "normal! \<bs>\<esc>"
endif
execute "normal! i\<cr>\<esc>"
endfunction
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.
set virtualedit=onemore
Note that I'm using nnoremap
(normal mode non-recursive) instead of map
(which is VERY dangerous) (check this out for more information on the differences http://learnvimscriptthehardway.stevelosh.com/chapters/05.html)
Upvotes: 0
Reputation: 4095
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
.
Upvotes: 4
Reputation: 265151
So you want to move everything in the current line, which comes after the cursor to the next line? Read: insert a line break??
(move cursor)
i (or a)
<return>
<esc> (or ^C)
To map this sequence of keystrokes to a single key, follow @thb's suggestion and use the :map
command:
:map <F2> i<CR><ESC>
Upvotes: 10
Reputation: 196476
If the cursor is on a <space>
as in ([]
marks the cursor):
lorem ipsum[ ]dolor sit amet
the simplest is to do r<CR>
, that is "replace the current character with a linebreak".
Otherwise, use @knittl's solution.
Upvotes: 31