0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

絵文字などで使うゼロ幅接合子を Vim と echo で置換する方法

Posted at

絵文字なんかで使われる「ゼロ幅接合子」U+200D を含む文字列を置換したりしたときに使ったことの覚書。

Vim で置換する

Vim で検索する場合は \%u200d で表現する。
Ex モードで置換する場合の例は以下のような感じ。

# 「[任意の絵文字👮]<200d>♂」な文字列を 「[任意の絵文字👮]<200d>♂️」に置換
:%s/\([^\x01-\x7e]*\)\%u200d♂/\1\%u200d♂️/gc

簡単な説明

\%u200d # Vim における U+200D の表現
[^\x01-\x7e] # 任意の 2 バイト文字

Vim だけじゃ駄目だった。

Vim を使って上記のように \%u200d に置換すると単に u200d という文字列になって「ゼロ幅結合子」にならなかったので、echo コマンドを使って sed で置換します。

sed -i "s/u200d/$(echo -ne '\u200d')/g" file.txt

参考資料

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?