20

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Anup
  • 951
  • 2
  • 10
  • 25
  • You could press `o` then `Esc`, two keystrokes isn't too bad in Vim. – alex Apr 01 '12 at 13:23
  • Are you asking how to move a line up/down? – Rook Apr 01 '12 at 13:24
  • 3
    @alex pressing `o` and then `ESC` will only take the cursor to the next line, it won't move the text on or after the cursor to the next line. – Anup Apr 01 '12 at 13:52

7 Answers7

28

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
10

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>
Community
  • 1
  • 1
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thanks @knittl, that is what I've been using but it switches the mode from normal to insert, and then back to normal. I was hoping to do this action without leaving the normal mode. I guess it's too much to ask for. :) I'll keep this question open for some more time before marking it answered. – Anup Apr 01 '12 at 13:55
8
:map <F2> i<CR>

This keeps vi in insert mode.

prongs
  • 9,422
  • 21
  • 67
  • 105
  • 1
    @Anup happy to help :) By the way to learn vi better, you should try `vimtutor` command. Also you can start by using gvim and then move to vim. It makes transition easy. That's how I learned vim. For some advance moves, I'd suggest to keep [This SO question](http://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim?page=1&tab=votes#tab-top) in your bookmarks :) – prongs Apr 02 '12 at 10:00
  • 1
    Add `` to the mapping and you stay in normal mode … – knittl Apr 02 '12 at 12:39
  • I do that, but I map it to `gs` as in "go split" - it's a nicer mnemonic than F2. – zelk Jan 05 '13 at 01:45
5

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.

Community
  • 1
  • 1
Alan Dong
  • 3,981
  • 38
  • 35
0

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)

Abdo
  • 13,549
  • 10
  • 79
  • 98
0

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

agotfrid
  • 507
  • 5
  • 7
0

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 :)

Keenan
  • 399
  • 3
  • 6