tags:

views:

134

answers:

6

I have 2 methods in a source file:

def Foo
  puts "hello"
  puts "bar" 
end 

def Bar
  puts "hello"
  puts "bar" 
end

I would like to swap the order of the methods, so Bar is first.

Assuming the cursor is on the d in def Foo, the simple way is to:

shift v -> jjjj -> d -> jjj -> p -> O -> esc 

But that feels a little long winded and does not account well for arbitrarily long methods:

What is the most efficient way to do this in Vim, keystroke wise?

EDIT Keep in mind, I would like the solution to account for a situation where the methods are in a context of a big class, so G is probably best avoided

A: 

From line 1, 5ddGp , or 5dd:5p is the most concise/shortest I can think of.

meder
any way to avoid the line counting in the head problem?
Sam Saffron
A: 

personally, I would go '4dd' then down under bar and press 'p', but I'm not a vim guru

Luke Schafer
+13  A: 

Assuming the cursor is somewhere in the first method, press dap}p and they should be swapped.

What dap does is simply "delete a paragraph". Try :help object-select to learn other way of deleting/selecting text objects in VIM.

EDIT: Replaced G with } in the command.

spatz
You, sir, just blew my mind :)
johnw188
+1 for complete awesomeness, is there a way to solve this without the G key? so it works in context of a big class
Sam Saffron
wow :)
Luke Schafer
Also can you explain dap? I can see what it does, but do not follow what it means.
Sam Saffron
try dap/end\n[enter][down]p
Luke Schafer
You can use `dap}p` (replace `G` with `}` to move to the next method instead of the end of the file, and thus handle the situation where there is more code after the second method.
spatz
Awesome answer, you are my new vim guru :P
Sam Saffron
Wow again. Ignore every comment I've made, I'm a mouse among giants
Luke Schafer
Explaining dap:The 'a' character, in visual mode or after an operator, allows you to select an object. p stands for paragraph, so dap = delete a paragraph. You could say das to delete a sentence, and so on. Look up text object selection in vim help for more.
johnw188
Very cool... but if there are any empty lines in a method, `dap` won't select the entire method. Still, +1 for reminding me that I'm totally in a vim rut and need to start spending 15 minutes a day with the docs.
Dave Ray
+1  A: 

A couple of ways off the top of my head. You could say

5dd/end[enter key]pO

Deletes five lines, searches for end, places the lines underneath, adds a space.

If you have VimRuby installed, I believe you can use % to jump between def and end. In that case, you could say

v%x5jpO

Edit: I defer to spatz on this :P

johnw188
+2  A: 

Found another method ( from godlygeek on #vim ):

with:

def function():
    first
    first
    first

def lol():
    second
    second
    second

From line 1, count up until the 'def lol', which is 5. Then:

:1,5m$
meder
+3  A: 

Similar to Spatz's

d}}p

delete to the next blank line (below Foo), skip to the next blank line (below Bar), paste.

glenn jackman