Author: Eiko

Tags: vim, tutorial, macro, vim motions

Time: 2024-09-10 17:31:17 - 2024-09-15 18:41:40 (UTC)

Vim Keybindings – Master the Magic Wand of Text Editing

At first glance, Vim might feel like an ancient, cryptic spellbook, but once you begin to unlock its power, you’ll discover it is no ordinary editor—it’s a tool for those who wield text like a true mage. The best way to learn is by casting the spells yourself. Don’t worry if it seems overwhelming; the true power of Vim reveals itself over time.

Here we cover all basic and advanced vim keybindings. We have not yet covered anything about its extremely powerful plugin system, but that’s a story for another day. For now, let’s focus on the core spells that will transform you into a text-wielding wizard.

Basics – Summon the Power

  • Enter Insert Mode: Use i or a to enter insert mode. This is the mode where you type your words into reality. i will insert text before the cursor, while a brings the words to life after the cursor.

    Example:
    You’re editing a line and want to fix a typo. Use i to start inserting the new word right before your mistake.

  • Escape the Insert Mode: Press ESC. It’s like casting “Exit to Command Mode”—where you can wield most of Vim’s true power.

  • Movement – Master the Dance: Instead of using those old arrow keys, try navigating with the keys h, j, k, l. These allow you to control the flow of your text with pure efficiency.

    • h - Move left
    • j - Move down
    • k - Move up
    • l - Move right

    Tip: Close your eyes (not looking at the keyboard, feel them on your right hand owo) and practice this—becoming one with your text.

    Pro Tip: You can bind J, K to 4j and 4k, also H L to 6h 6l. This will make editing and moving around much easier and happier! owo

    Super Pro Tip: enable relative line number with :set number relativenumber, and move to the line you want with something like 12j, 29k!

  • Jump through Words: Move in leaps and bounds using:

    • w - Jump to the start of the next word
    • b - Hop back to the beginning of the previous word
    • e - Soar to the end of the current word

    Add W, B, or E to move over larger structures like whitespace-separated blocks.

    Example:
    You’re crafting a long sentence and want to jump between words quickly—use w and e to speed across the sentence like lightning!

  • Save document, and quit When you need to save all your magic work, go back to normal mode and press :w or :w filename to save it on your computer!

    Press :q for quitting. You can stream line these two actions with command :wq owo!

Modifying Text – Shape Reality

  • Deleting like a Wizard: Deleting is more than just removing—it’s altering the very fabric of your text. Use x to delete the character under your cursor or X to strike the one before it. If you only need to change on letter, r is your friend, these commands will keep you in command mode, how convenient!

    Example:
    Made a typo? Just x or r that error away without switching modes.

  • Creating Space: When you need more room for new lines of code or text:

    • o opens a new line below your current one
    • O opens one above

    This is perfect for seamless additions.

  • Line Control: To fly to the start of a line, press ^. To zip to the end, press $.

    Another choice is 0, which will ignore all whitespace and indentations. That’s why in general ^ is preferred.

    Example:
    Editing a long line? You’ll zip to the start with ^ and dash to the end with $.

  • Undo and Redo Time:

    Feel like a time-traveling sorcerer with u (undo) and Ctrl + r (redo). You can also use the enhanced U to redo if your fingers get tired of the Ctrl combo (you need to bind it before using U, write :nnoremap U ^R in your vimrc file).

Advanced Spells

  • Macro Magic: You’re ready for the big leagues now—Macros allow you to automate your text manipulation like a true master.

    Example:
    Suppose you need to change a word pattern across multiple lines. Record a macro with q a, perform the action (like jumping, changing a word, and moving to the next line), then stop with q. Now, with @a, replay the macro and automate your edit. Want to feel like a true Vim wizard? Try 100@a to unleash your macro 100 times.

    Replay your last macro? Use @@.

    Let’s see a concrete example, suppose you have a lot of code that looks like

    function1 "heihei" (owo "kuku")
    func2 "aoaoao" (oao "kuku")
    function1 "wuwuwu" (o_o "kuku")
    func2 "heihei" (owo "kuku")
    function1 "aoaoao" (oao "kuku")
    func2 "wuwuwu" (o_o "kuku")

    and we want to change all occurences of kuku after the cute emoji to the same text just after the function application. You can do it this way:

    1. move cursor to the first line, press ^ to move to the start.
    2. press q a to record a macro to the register a (actually "a)
    3. press W to move to the string, then press yi" to copy the text inside the string.
    4. press f" then ; to move to the next string.
    5. press vi" to select the text inside the string, in this case, kuku
    6. press p to paste what you have copied over the selected text~
    7. press j and ^ to move to the start of the next line.
    8. press q, the record of a macro is complete now!
    9. unleash its power with 5@a (which will repeat your actions 5 times~)
  • Changing Inside Words (ciw): Change the word under your cursor like you’re rewriting history itself.
    ciw deletes the word and puts you in insert mode.

    Example:
    You’ve misnamed a variable—just place the cursor on it, type ciw, and correct it immediately. It’s like a perfect incantation for mid-word fixes.

Enhancements – Speed Beyond Human Limits

  • Fast Scrolling: Fly through your file with:

    • Ctrl + d to move down half a screen
    • Ctrl + u to soar back up half a screen
  • Find Anything: Use / followed by your search term and press Enter. You’ll immediately jump to the first occurrence. Press n to move to the next match and N to jump backward. It’s like casting a radar spell for text.

    Example:
    Looking for a particular function in your code? Just /def my_function and you’ll zoom straight to it.

A Final, Secret Technique

  • Visual Block Mode (Ctrl + v): This is where true text-wielders shine. You can select entire blocks of text and manipulate them simultaneously. Delete, insert, or replace in multiple lines at once—like a true sorcerer editing in parallel dimensions.

    Example:
    Imagine columns of text or numbers

    hello-1
    hello-2
    hello-3
    hello-4
    hello-5
    hello-6
    hello-7
    hello-8

    and you want to change all hello- into function=, instead of editing line by line, you can editing all at once—select them with Ctrl + v, then apply your command and watch the magic unfold!


The key to mastering Vim is patience. Every new spell you learn adds a layer to your text-wielding power. Don’t rush—focus on the basics first and slowly level up. Before you know it, you’ll be a Vim master, reshaping text with the flick of your fingers like an ancient text-sorcerer!

Just like linux, we learn it not because it’s easy, but because it’s worth it! Have fun casting your spells! 🧙‍♂️