move the text after the cursor to a new line

2020-08-09 10:10发布

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?

标签: vim
7条回答
\"骚年 ilove
2楼-- · 2020-08-09 10:43

Depending on exactly the effect you want, either give the command

:map <F2> o<Esc>

or the command

:map <F2> O<Esc>j

Thereafter, pressing <F2> will do the job with a single keystroke.

But, of course, @Alex is right, too. Just pressing o then Esc whenever you need it isn't bad.

查看更多
3楼-- · 2020-08-09 10:45

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>
查看更多
太酷不给撩
4楼-- · 2020-08-09 10:45
劫难
5楼-- · 2020-08-09 10:48

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

查看更多
不美不萌又怎样
6楼-- · 2020-08-09 11:00

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)

查看更多
▲ chillily
7楼-- · 2020-08-09 11:05

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.

查看更多
登录 后发表回答