InfoHeap
Tech tutorials, tips, tools and more
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. Vim mini cheat sheet – handy commands reference
  2. Mac finder – change search default folder to current folder
  3. Python re (regex) search examples
  4. Perl command line – replace multi line comments
  5. Linux replace comma with newline
  6. Python re (regex) replace examples
  7. Mac – make spotlight search faster
  8. How to search files by name in Mac Finder
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

Follow InfoHeap

facebook
twitter
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | 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

Copyright © 2021 InfoHeap.

Powered by WordPress