Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select disjoint chunks of code in Vim for yanking

I am wondering if I am able to do this in Vim:

Sample code:

require 'abstract_controller/collector'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/array/extract_options'
require 'IDONTWANTTHISLINETOBEINCLUDEDINMYYANKREGISTER'
require 'IDONTWANTTHISLINETOBEINCLUDEDINMYYANKREGISTER'

    module ActionMailer #:nodoc:
      class Collector
        include AbstractController::Collector
        attr_reader :responses

        def initialize(context, &block)
          @context = context
          @responses = []
          @default_render = block
        end

        def any(*args, &block)
          options = args.extract_options!
          raise "You have to supply at least one format" if args.empty?
          args.each { |type| send(type, options.dup, &block) }
        end
        alias :all :any

        def custom(mime, options={})
          options.reverse_merge!(:content_type => mime.to_s)
          @context.freeze_formats([mime.to_sym])
          options[:body] = block_given? ? yield : @default_render.call
          @responses << options
        end
      end
    end

Now suppose I want to yank just some lines and put them in another file. Suppose I want to yank these block of lines:

Chunk 1:

require 'abstract_controller/collector'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/array/extract_options'

Chunk 2:

    module ActionMailer #:nodoc:
      class Collector
        include AbstractController::Collector
        attr_reader :responses

        def initialize(context, &block)
          @context = context
          @responses = []
          @default_render = block
        end

Chunk 3:

        def custom(mime, options={})
          options.reverse_merge!(:content_type => mime.to_s)
          @context.freeze_formats([mime.to_sym])
          options[:body] = block_given? ? yield : @default_render.call
          @responses << options
        end
      end
    end

These lines don't form a continuous line group, they are separated. So to achieve what I want I have to yank these blocks in 3 steps, which I find quite annoying. Because I have to yank, switch buffer, put, switch buffer, yank, switch buffer, put... so on...

So, is there a way to do this more efficiently (in one step)?

like image 298
flyer88 Avatar asked Nov 10 '11 18:11

flyer88


1 Answers

Use a register in append mode:

  • Visually select first three lines, "ay
  • Visually select next 10 lines, "Ay (note the capital letter)
  • Visually select chunk 3, "Ay
  • Go to other buffer, "ap

You like registers? This answer is more in-depth.

like image 186
Benoit Avatar answered Nov 05 '22 12:11

Benoit