Vim command screenshot

Vim, seach and replace. With regular expressions.


Vim, ohh, Vim. Text editor that is used from terminal, an improved version of the Vi text editor, present on all UNIX systems. It has a high learning curve, but when have a good control, it can increase productivity. In this post, I will explain some basic operations for "Find and Replace".

Vim has different working modes, the following are commented: "command", "insert" and “command-line”:

  • Command mode: Default mode or otherwise known as normal. You can always return to this mode by pressing two times the "Escape" key.

  • Insert mode: Used to edit the text. Accessed from the command mode by pressing the "i" key.

  • Command-line mode: Accessed from the command mode by pressing the ":" key and then the command. Mode where "Search and replace" actions will be executed:
    • Find the occurrences of the string "foo" and replace them with "bar", throughout the text:

    • :%s/foo/bar/g

    • Find the occurrences of the string "foo" and replace them with "bar", only in the current line:

    • :s/foo/bar/g

    • Regular expressions can also be used. For example, by solving the following problem:

      Assuming the following text is available:

      <color name=“Green”>#008000</color>
      <color name=“Black”>#000000</color>
      <color name=“White”>#FFFFFF</color>

      …(many colors)

      Want to transform to:


      The steps to do this, relatively quickly are:

      • Step 1:



      • Step 2:



      • Step 3:



      • Step 4:



  • Solved the problem.
    You can also see the solution of the problem in video:

Surely exists a regular expression that solve this with a single order, but it's a basic introduction and solved in a simple way. If you have the regular expression that solves it, I invite you to share it on our Twitter account @tocappcom

Solicitamos su permiso para obtener datos estadísticos de su navegación en esta web, en cumplimiento del artículo 22.2 de la vigente Ley 34/2002, de 11 de julio. Si continúa navegando consideramos que acepta el uso de cookies. Puede conseguir más información en nuestra Política de Cookies | OK