Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to search and replace only full word in Vim

📅 2015-Feb-12 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ replace, search, vim, word ⬩ 📚 Archive

By default, the search and replace in Vim replaces the search string even if it is a substring in a longer string. For example, :%s/foo/abracadabra/ will replace foo that is in foobar[10] or zoofoomoo(20). However, while working with source code we typically want to replace only those occurrences where the search string appears as a whole word.

To do this in Vim, enclose the search string as shown in:

:%s/\<foo\>/abracadabra/

Tried with: Vim 7.4 and Ubuntu 14.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧