vim is the much-loved text editor when it comes to the command line or the terminal, but it has a learning curve when it comes to performing normal day-to-day operations like find and replace.
Let us take a look at various ways to perform find and replace operation using the:s substitution command,
Find and Replace 1st occurrence in a Line
:/s/sample/example
![Find and Replace first occurance Find and Replace first occurance](https://code2care.org/howto/perform-find-and-replace-using-vim-editor/images/Find and Replace first occurance.jpg)
![After replace After replace](https://code2care.org/howto/perform-find-and-replace-using-vim-editor/images/After replace.jpg)
Find and Replace all occurrences of in a Line
:/s/line/line1/s
![Before find and replace line all using vim Before find and replace line all using vim](https://code2care.org/howto/perform-find-and-replace-using-vim-editor/images/Before find and replace line all using vim.jpg)
![after-replace all in line using vim after-replace all in line using vim](https://code2care.org/howto/perform-find-and-replace-using-vim-editor/images/after-replace all in line using vim.jpg)
Find and Replace All occurrences of in a File
:%s/Say/Hello
![Before VIM find and Replace all in file Before VIM find and Replace all in file](https://code2care.org/howto/perform-find-and-replace-using-vim-editor/images/Before VIM find and Replace all in file.jpg)
![After VIM find and Replace all in file After VIM find and Replace all in file](https://code2care.org/howto/perform-find-and-replace-using-vim-editor/images/After VIM find and Replace all in file.jpg)
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to vi,
- How to add Colors to VIM (syntax highlighting)
- How to perform Find and Replace using vim Editor
- How to update VIM version on a Mac
- How to Select All Text in Vim/Vi editor
- Guide: Install Vim on Mac
- The Vim (vi) Mac Configuration File Details
- Fix :wq! Vi Error - E32: No file name (when write and quit)
- How to Copy all text to Clipboard in Vim
- How to Uninstall Vim on Mac?
More Posts:
- [fix] Chrome: no listeners for an event URLS_SAFE_CHECK__STATUS_UPDATED - Chrome
- How to Convert Timestamp to Date in Java 8 or Above - Java
- Get Current time in GMT/UTC using JavaScript - JavaScript
- Teams - You're offline. Messages you send while offline will be sent when you're back online - Teams
- How to Clear All Cells Output in Jupyter Notebook - Python
- How to Prevent Mac from sleeping - MacOS
- How to remove password from pdf file - HowTos
- [fix] RabbitMQ: AuthenticationFailureException ACCESS_REFUSED Login refused authentication mechanism PLAIN - 2022