berkes
berkes

Reputation: 27553

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

Upvotes: 3

Views: 500

Answers (1)

Benoit
Benoit

Reputation: 79175

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

Upvotes: 10

Related Questions