0

私は以下を実行しようとしています

%s/foo/\=substitute(getline('.'),'bar','','g')

この例に

this is a foo cool bar

返して欲しいのは

this is a bar cool bar

しかし、それは戻ってきています

this is a this is a foo cool  cool bar

つまり、substitute()関数で一致した正規表現だけでなく行全体が返されます

私は何かが足りないのですか?

split()関数とsedの実装を知っていますが、substitute()に入れたいです

4

1 に答える 1

1

なぜsubstitute()

なぜ単純ではないのです:s/foo/bar/か?

とにかく、両方の醜いコマンドが機能するはずです... :(

:%s/foo/\=substitute(submatch(0),".*","bar","g")/

:%s/.*/\=substitute(submatch(0),"foo","bar","g")/

関数のみを使用したい場合は、これも機能します。

:%call setline(line('.'),substitute(getline('.'),'foo','bar','g'))
于 2013-02-15T17:43:53.383 に答える