AICollection Help

In-depth VIM

The Ultimate Guide to Vim: From Beginner to Advanced

Vim is not just an editor—it’s a powerful tool that can be customized into a complete development environment. Whether you’re just starting out or you’ve been using Vim for years, this guide provides examples, commands, and advanced topics to help you get the most out of your workflow.

Table of Contents

  1. Introduction

  2. Getting Started: The Basics

  3. Intermediate Features and Techniques

  4. Advanced Vim Topics

  5. Customization and Scripting

  6. Tips, Tricks, and Best Practices

  7. Conclusion

Introduction

Vim (Vi IMproved) is an enhanced version of the classic Unix editor, vi. It is known for its efficiency, extensive functionality, and steep learning curve. This guide is structured to help you progress from the basics of Vim to advanced techniques that can turn you into a power user.

Why Learn Vim?

  • Efficiency: Once you master Vim’s commands, editing becomes extremely fast.

  • Customizability: Vim can be tailored to your specific needs with plugins and custom configurations.

  • Ubiquity: Vim is available on almost every Unix-based system, making it a useful skill across different environments.

  • Community: A large and active community supports Vim, offering numerous plugins, tutorials, and resources.

Getting Started: The Basics

Opening and Exiting Vim

  • Open a file:

    vim filename.txt
  • Exit commands:

    • :w – Save the file

    • :q – Quit Vim

    • :wq or :x – Save and quit

    • :q! – Quit without saving

Modes in Vim

Vim has several modes, with the two most important being:

  • Normal mode: The default mode for navigation and commands.

  • Insert mode: For entering text. Press i to enter Insert mode and Esc to return to Normal mode.

Basic Navigation

  • Movement commands:

    • h – move left

    • j – move down

    • k – move up

    • l – move right

  • Word-wise navigation:

    • w – jump to the start of the next word

    • b – jump back to the beginning of the word

Editing Basics

  • Insert text:

    • i – insert before the cursor

    • a – insert after the cursor

  • Deleting text:

    • x – delete the character under the cursor

    • dw – delete a word

    • dd – delete an entire line

  • Undo and redo:

    • u – undo

    • Ctrl-r – redo

Searching

  • Basic search:

    • /pattern – search forward for a pattern

    • ?pattern – search backward

    • n – repeat the search in the same direction

    • N – repeat in the opposite direction

Intermediate Features and Techniques

Working with Multiple Files

  • Buffers:
    Vim opens each file in a buffer. You can navigate between buffers using:

    :ls " List all open buffers :b <number> " Switch to buffer with specified number
  • Windows and splits:
    Use splits to view multiple files simultaneously.

    • :split filename.txt – horizontal split

    • :vsplit filename.txt – vertical split

    • Use Ctrl-w followed by arrow keys to navigate between splits.

Text Objects and Motions

  • Change inside a word:

    • ciw – change inner word

  • Change inside quotes:

    • ci" – change text inside double quotes

  • Visual mode:

    • v – start visual mode (character-wise)

    • V – start visual line mode

    • Ctrl-v – start visual block mode

Macros and Repeating Actions

  • Recording a macro:

    • Start recording: q{register} (e.g., qa to record in register “a”)

    • Perform actions

    • End recording: q

  • Playing back a macro:

    • @a – execute macro in register “a”

  • Repeat the last macro:

    • @@ – repeat the last executed macro

Advanced Searching and Replacing

  • Substitute command:

    • :s/old/new/g – substitute “old” with “new” in the current line

    • :%s/old/new/g – substitute in the entire file

    • Use c after the command (e.g., :%s/old/new/gc) to confirm each substitution

Advanced Vim Topics

Customizing Vim with .vimrc

Your ~/.vimrc file is where you can store your configuration. Here are some useful settings:

" Enable syntax highlighting syntax on " Set line numbers set number " Enable incremental search set incsearch " Use system clipboard set clipboard=unnamedplus " Map leader key let mapleader = ","

Vim Plugins and Plugin Managers

  • Popular Plugin Managers:

  • Example Plugin Setup using vim-plug:

    call plug#begin('~/.vim/plugged') " File explorer Plug 'scrooloose/nerdtree' " Status line enhancement Plug 'vim-airline/vim-airline' " Fuzzy finder Plug 'junegunn/fzf.vim' call plug#end()

Vim Scripting

Vim script allows you to extend Vim’s functionality. Here’s a simple script example that creates a custom command:

" Define a custom command to open a new split with a terminal command! TerminalSplit split | terminal

Using Vim as an IDE

  • Language-specific plugins:


    Vim supports many languages through plugins like ALE for linting and YouCompleteMe for code completion.

  • Integrated debugging:


    Plugins such as vimspector allow debugging within Vim.

  • Version control integration:


    Use plugins like fugitive.vim to interact with Git directly from Vim.

Customization and Scripting

Creating Custom Key Mappings

Key mappings allow you to speed up common tasks. Add these to your .vimrc:

" Remap jj to escape insert mode inoremap jj <Esc> " Save file quickly nnoremap <Leader>w :w<CR> " Close window nnoremap <Leader>q :q<CR>

Automating Tasks with Vim Scripts

You can write Vim scripts to automate repetitive tasks. For instance, to auto-format your code on save:

" Auto-format using an external formatter (e.g., prettier for JavaScript) autocmd BufWritePre *.js :%!prettier --stdin-filepath %

Advanced Regular Expressions

Vim uses its own flavor of regular expressions. Some examples include:

  • Matching word boundaries:
    Use \< and \> to denote the start and end of a word.

    :%s/\<foo\>/bar/g
  • Using capture groups:
    Replace patterns with captured groups:

    :%s/\(foo\)\(bar\)/\2\1/g

Performance Tweaks

  • Lazy-loading plugins:


    Some plugin managers allow you to load plugins only when needed, reducing startup time.

  • Minimal configurations:


    Start with a minimal .vimrc and add customizations gradually to keep Vim snappy.

Tips, Tricks, and Best Practices

  • Practice Regularly: Vim’s commands become second nature with daily practice.

  • Learn to use :help:


    Vim’s built-in help system is extensive. Try :help <command> for detailed explanations.

  • Backup your configuration:


    Keep your .vimrc and plugin list in version control.

  • Experiment:


    Try different plugins and configurations to discover what best fits your workflow.

  • Join the Community:


    Engage with communities like Reddit’s r/vim or the Vim mailing list for tips and support.

Conclusion

Vim is a robust editor that caters to all levels of users. Whether you are just learning the basics or diving into advanced scripting and customization, there is always something new to discover. With consistent practice and exploration, you can transform Vim into a highly personalized and efficient environment tailored to your development needs.

Happy Vimming!

Last modified: 08 March 2025