Getting to Black Belt in Vim – Part 6 – Black Belt

Welcome to our final chapter. We made it! Previously, at blue belt level, we covered text-objects. At black belt level, we’ll cover search/replace, macros and window commands. Finally, we’ll explore topics to take you beyond black belt.

Search & Replace

To replace everything on the current line, type:

:s/<search term>/<replace term>/g

where the <search term> is what we want to find, and <replace term> is the substitution.

In addition, if we want to search the whole file, we can specify a range of %, which is a shortcut for the whole file:

%s:/<search term>/<replace term>/g

The g at the end stands for global, which means all occurrences.

Macros

Macros can help you automate editing that you need to apply to a large area. Specifically, they record any keystrokes and motions you make. Then, you can repeat that whenever you need to. You can use pretty much everything you’ve learnt so far to record your macro.

Here are the sequence of commands:

  • Type q, followed any letter to start recording that macro.
  • Next, type the sequence of commands you’d like to automate.
  • Finally, press q again to stop recording.
  • To replay you macro, type @ followed by the letter you used to store the macro in.

For an example of how to use a macro, see this article.

Window Splits

Sometimes it’s useful look at different content at the same time. Here’s how to split windows:

  • <Ctrl> + w, then v for vertical split, or s for (horizontal) split
    • Once split, you can move between windows using h, j k or l:
      • <Ctrl> + w, h to move left.
      • <Ctrl> + w, l to move right.
      • <Ctrl> + w, j to move down.
      • <Ctrl> + w, k to move up.
    • <Ctrl> + w, c to close the window you’re in.
    • <Ctrl> + w, o to close all windows except the one you’re in.

Once you’ve split a window, you can use e: <filename> to edit that file.

Scrolling

  • zz – scrolls the screen so the cursor is in the centre.
  • zt – scroll so the cursor is at the top.
  • zb – scroll so the cursor ends up at the bottom.

Beyond Black Belt

At this point, you should be pretty quick at navigating your way around a file, making precision edits. However, we’ve only just scratched the surface of what Vim can do. Here are some topics you can explore to increase your productivity:

  • Marks
  • Tags
  • Folding
  • Files
  • Tabs
  • Buffers
  • Undo Tree
  • Vim Script
  • Keymapping

Getting Help in Vim

Getting help on a command is easy, type :help followed by the command you want to learn more about.

Published
Categorized as Vim Tagged

1 comment

Comments are closed.