Ở đây bạn đi:
:g/foo/t.|s//bar
Đang phân hủy:
:g/foo/ " start a global command applied on all lines matching 'foo'
t. " duplicate the current line (the cursor is now on the new line)
| " chain a new command
s//bar " substitute the last searched element with 'bar'
Bởi vì glệnh sẽ cập nhật mẫu tìm kiếm, vì vậy bạn có thể bỏ qua mẫu để thay thế trong lệnh thay thế. (ref : :h :g, tìm kiếm search pattern).
Phiên bản cũ hơn:
:g/foo/norm! yyp:s/foo/bar^M
Đang phân hủy:
:g start a global command
/foo/ apply only on lines having 'foo'
norm! execute a normal command
yyp duplicate the line
:s//bar replace foo with bar on the line (the duplicated one)
^M add enter to execute the substitution
Để chèn ^Mbáo chí Ctrl+vvà enter.
Lưu ý : Ban đầu tôi đã đưa ra phiên bản "cũ hơn", trước khi tôi biết về tlệnh này. Tôi sẽ bỏ nó nhưng tôi sẽ không khuyên bạn nên sử dụng nó. Cái đầu tiên sạch sẽ đơn giản hơn.