1

VIMで以下を実装するにはどうすればよいですか?

substitute/regex_this_word/regex_with_another_word_from_the_same_line

例えば

select "ali" as name where _placeholder = _placeholder
union
select "sam" as name where _placeholder = _placeholder

適用後

:%s/_placeholder/anythin_between_quotation/

になります

select "ali" as name where ali = ali
union
select "sam" as name where sam = sam

例2

select id, "_placeholder" as vul members_repeater/vul/none

適用後

:%s/_placehold/\=something_like_regexp(getline('.'),'regexp_pattern_to_select_everthing_after_/vul/")

になります

select id, "none" as vul members_repeater/vul/none 

ありがとう

4

3 に答える 3

4
:%s/_placeholder/\=split(getline("."),'"')[1]/g

これはこの場合に機能します:

  • 各行に1 つの引用部分 (置換) のみ
  • 引用部分はこの行のどこにでもある可能性があります

例えば:

select "ali" as name where _placeholder = _placeholder
union
select "sam" as name where _placeholder = _placeholder
select _placeholder where _placeholder = _placeholder "foo"
select _placeholder where "bar", _placeholder = _placeholder

の中へ

select "ali" as name where ali = ali
union
select "sam" as name where sam = sam
select foo where foo = foo "foo"
select bar where "bar", bar = bar

編集

\=split(getline("."),'"')[1]
 |  |      |
 |  |      +--- get current line text
 |  |
 |  +------ split the line with " as separator, pick the middle part ([1])
 |
 |
 +---- using expression replacement

新しい編集

古いルーチンを再利用できます:

:%s#_placeholder#\=split(split(getline("."),"vul/")[1]," ")[0]#g

これはあなたの行に1つだけ必要ですが、キーワードの後に​​(セパレータとしてスペースを使用して)次のようなvul/テキストがある可能性があります。

select id, "_placeholder" as vul members_repeater/vul/none trashtrash

の中へ

select id, "none" as vul members_repeater/vul/none trashtrash

この例を参照してください:

ここに画像の説明を入力

于 2013-02-15T16:09:31.857 に答える
1

特にこれらの行の場合、それは

s/^\(.*\)"\([^"]*\)"\(.*\)_placeholder\ = _placeholder/\1"\2"\3\2 = \2/

\(説明:との一致するペアの間の式は\)、\1、\2 などでキャプチャされます。したがって、ここで進める 1 つの方法は、 までのすべてをキャプチャし_placeholder、そこに戻すことです。確かに、少し読めません。

このソリューションでは、各行に二重引用符で囲まれた式が 1 つしかないことを前提としています。

于 2013-02-15T16:01:25.337 に答える
1

知っていれば簡単に使用sedできます。拡張正規表現を使用できます。

example_with_no_link.txt:

from django.utils import unittest
from django.core import management
from app import subapp

vim コマンド :

:%! sed -re "/django/ s/from (.*) import (.*)/from \2 import \1/"

このコマンドは次のことを行います:
1 :%!.: すべての行を stdout に入れます
2 sed -re "/django/.: 行に 'django' がある場合
3 .:s/from (.*) import (.*)/from \2 import \1/括弧内にパターンを逆にします

sed では、検索した単語を括弧でキャッチし、 で記述します\n
出力は vim にリダイレクトされます。

于 2013-02-15T16:55:53.120 に答える