Vim command screenshot

2016/12/01


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:

      <item>@color/Green</item>
      <item>@color/Black</item>
      <item>@color/White</item>

      The steps to do this, relatively quickly are:

      • Step 1:
        :%s/.*=//g

        Resulting:

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

      • Step 2:
        :%s/>.*//g

        Resulting:

        “Green”
        “Black”
        “White”

      • Step 3:
        :%s/“//g

        Resulting:

        Green
        Black
        White

      • Step 4:
        :%s/.*/<item>@color\/&<\/item>/g

        Resulting:

        <item>@color/Green</item>
        <item>@color/Black</item>
        <item>@color/White</item>

  • Solved the problem.
    You can also see the solution of the problem in video: https://youtu.be/5LAnylEZRQc

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

About Author

I'm passionate about simply, casual, minimal and experimentals games.