How do I Join the line above after current line?

久未见 提交于 2020-01-01 09:59:07

问题


I know these commands in Vim:

J : Join line below after current line
-J : Join current line after line above

but how do I join the line above after current line?


回答1:


There are many ways to do it. One would be… deleting the line above and appending it to the end of the line below:

k move up one line
^ move to the first printable character
y$ yank to the end of the line
"_d get rid of the now useless line by deleting it into the black hole register
$ move to the end of the line
p put the deleted text



回答2:


You can also use the ex-command

:m-2|j
  • m-2 has the effect of moving the current line to 2 lines above its current position; this switches the position of the current line and the line above.
  • j joins the current line and the line above, inserting a space between the two. Use j! if you don't want the space.
  • | separates the 2 ex-commands

This ex-command is a short way of writing the following

:move .-2
:join  



回答3:


I added the below lines to my .vimrc. Now, in normal mode, I can either press @j or < leader >j. Leader for me is space. I've also seen people set it to ,.

" join with previous line with @j
let @j="kJ"
nnoremap <leader>j @j

If you haven't set leader yet, you can set it to space like this:

let mapleader = " "
let g:mapleader = " "


来源:https://stackoverflow.com/questions/13609736/how-do-i-join-the-line-above-after-current-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!