LoginSignup
71
67

More than 5 years have passed since last update.

vim tips: 1. 複数行の先頭に文字を挿入したり、コメントアウトしたり、もはや正規表現のtips^^;

Last updated at Posted at 2013-05-09

お題

before.rb
require 'test_helper'

class ProductTest < ActiveSupport::TestCase
  test "product attributes must not be empty" do
    product = Product.new
    assert product.invalid?
    assert product.errors[:title].any?
    assert product.errors[:description].any?
    assert product.errors[:price].any?
    assert product.errors[:image_url].any?
  end
end

これを

after.rb
require 'test_helper'

#class ProductTest < ActiveSupport::TestCase
#  test "product attributes must not be empty" do
#    product = Product.new
#    assert product.invalid?
#    assert product.errors[:title].any?
#    assert product.errors[:description].any?
#    assert product.errors[:price].any?
#    assert product.errors[:image_url].any?
#  end
#end

こう!!

☆・:゚オォヾ(o´∀`o)ノォオ゚:・☆

答え1

1. 矩形選択モードになります

**CTRL + v**

そして選択範囲をカーソルで指定します。

2. 挿入モードになります

**Shift + i**

挿入したい文字を打ち込みます。複数でもOKです。
今回はコメントアウトしたいので "#"で行きます。

で、打ち込んだらescを押します。

完成

☆・:゚オォヾ(o´∀`o)ノォオ゚:・☆

答え2

1は同じで、
矩形選択で選択して、そのまま、コロンを入力し...

:'<,'>s/^/#/

コッチのほうがそれっぽいですね。
☆・:゚オォヾ(o´∀`o)ノォオ゚:・☆

ちなみに、コメントアウトを取るには、

矩形選択で、選択して、

:'<,'>s/^/#//

です。
☆・:゚オォヾ(o´∀`o)ノォオ゚:・☆

....せっかくなので行頭だけじゃなくって行末にも入れるとしましょう。

矩形選択の後...

:'<,'>s/$/#/

/* */ みたいなコメントアウトの時

:'<,'>s/^\(.*\)$/\/* \1 *\//
:'<,'>s/^...\(.*\)...$/\1/

解読していきます。

: の後の s/置換しますよー という宣言('<,'>は一旦無視)
s/hoge/fuga で「hogeをfugaに置換します」 となる
^(.*)$ のところですが、 ^行頭$行末.任意の文字1文字、* は 前の文字の0回以上の繰り返し
行頭から行末までの間で

疲れたので続きは後ほど

71
67
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
71
67