简体   繁体   中英

How to move current line behind the line above it in Vim?

How would I move the current line behind the line above it? Say I have:

    function foo()
      {
      ^ Cursor is here

And want to turn that into:

function foo() {

I am still new to vim, so what I do now is i[backspace][backspace]...etc. :)

Several ways:

  • In normal mode, kJ or kgJ or VkJ or VkgJ (the last two commands do the same in visual mode).
    k will go to previous line, and J or gJ will merge with next line ( J inserts a space inbetween, gJ just removes the EOL characters)
  • In command mode, :-,j or :-,j!
    -, is a range that is abbreviation for .-1,. which means “from previous line to current line”
    j is the ex command for concatenating lines in a range. The banged (with exclamation mark) version acts like gJ.
  • With a substitution: :-s/\\s*\\n\\s*//
    - means previous line
    :s is probably known to you, else you should run vimtutor . /\\s*\\n\\s*/ is pattern for as many spaces as possible plus line terminator (matches different byte sequences according to the file format: LF, CR or CRLF) plus as many spaces as possible.
    Here, replacement pattern is empty.
  • in insert mode, hit CTRL-W twice (each time it deletes a word, or leading whitespace on a line, or newline) (as ib. suggests, this depends on the backspace setting).

References:

  • :help J
  • :help gJ
  • :help k
  • :help range
  • :help :j
  • :help pattern
  • :help i_CTRL-W

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM