InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Vim tutorials

  • Character toggle case
  • Go back to last edited line
  • Move to matching bracket
  • Multiline search replace with confirmation
  • Open vim at previous location
  • Vim mini cheat sheet
  • Vim redo (undo of last undo)
  • show line numbers
 
  • Home
  • > Tutorials
  • > Vim

Vim (vi) – multiline search replace with confirmation

By admin on Dec 3, 2015

Vi (or Vim) is a very powerful and light weight editor which is available on any Linux or Mac. Even if you are working on production environment remotely, very likely vi will be available there. For casual simple editing also it works nicely.

Searching/replacing on multiple lines is a very common use case. Here are some approaches to do multiline search replace with confirmation in vi:

Vi – searching and replacing from current line till last line

  1. Once you hit escape the enter the following command for searching and replacing text from current line till last line:
    :.,$s/Line/Row/gc
    
    Dissecting above command
    String Purpose
    . Current line
    $ last line in file
    .,$ Lines range
    s To search replace
    Line String to be replaced
    Row Replacement string
    Flag g global in a line
    Flag c Confirm before replacing

    Here is a sample file opened in vi with above command used:
    vi-search-replace-with-confirmation-command

  2. After entering the above hit enter. If everything is correct, vi will stop at first match and ask for confirmation as shown below. Press y or n as desired.
    vi-search-replace-confirmation-prompt
  3. Once all replacements are done, you will see a summary as shown below:
    vi-search-replace-end-summary

Vi – more search replace scenarios

  1. Use 1,$ or % to search/replace from first line till last line.
  2. Use .,+9 to search/replace in 10 lines starting from current line
  3. Skip flag c to replace without confirmation

Suggested posts:

  1. Internal vs external css
  2. AngularJS hello world example
  3. Iterate over specific file extension in a dir in shell script
  4. Javascript/jQuery – disable right click
  5. How to delete files starting with dash/hyphen
  6. How to run a program as administrator on windows 10
  7. How to enable Chrome tabs on Android Lollipop
  8. Python find length of string or list using len()
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Linux/Unix Command Line, Mac, Tutorials, Ubuntu Linux, Vim
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress