vim

Vim is a highly efficient and powerful text editor used for coding, writing, and general text manipulation. Below are some basic commands and usage tips for getting started with Vim:

Basic Usage

  1. Opening a File:
    To open a file in Vim, use the following command:

    vim filename
    
  2. Switching Between Modes:
    Vim operates in different modes, mainly:

    • Normal Mode: For navigation and manipulation. This is the default mode when you open Vim.
    • Insert Mode: For typing text. You can enter this mode by pressing i from normal mode.
    • Command-Line Mode: For saving, quitting, and other commands. Access it by pressing : from normal mode.

Normal Mode Commands

Insert Mode Commands

To enter Insert Mode from Normal Mode, press:

Once in Insert Mode, type your text as usual. To return to Normal Mode, press Esc.

Command-Line Mode Commands

To enter Command-Line Mode, press : from Normal Mode.

Visual Mode Commands

Visual Mode allows you to select text and perform actions on it.

Other Useful Commands




Advanced Navigation

  1. Move by Paragraphs:

    • Move to the beginning of the next paragraph: }
    • Move to the beginning of the previous paragraph: {
  2. Move by Search Results:

    • Jump to the next search result: n
    • Jump to the previous search result: N
    • Search for the word under the cursor: * (next occurrence), # (previous occurrence)
  3. Move to Specific Line:

    • Go to a specific line number: :n (replace n with the line number)
    • Go to a specific column on a line: n| (replace n with the column number)
  4. Faster Navigation Between Files:

    • Move to the first non-blank character on a line: ^
    • Move to the end of the current sentence: )
    • Move to the beginning of the current sentence: (
    • Jump to a matching bracket, parenthesis, or brace: %
    • Go to the start or end of the current block: [{ or ]}
  5. Jump to Specific Marks:

    • Set a mark at the current cursor position: m{a-z}
    • Jump to the mark: 'a or `a (lowercase for line, backtick for exact position)

Advanced Editing

  1. Advanced Text Manipulation:

    • Change a word: cw
    • Change until the next character: c{char}
    • Change until the end of the line: C
    • Delete a word backwards: dB
    • Delete everything until the end of the current sentence: d)
    • Replace text: r{char} (replace current character under cursor)
  2. Macro Recording:

    • Start recording a macro: q{register} (e.g., qa to record to register a)
    • Perform multiple commands while recording.
    • Stop recording: q
    • Play the macro: @{register} (e.g., @a to replay the macro recorded in register a)
    • Play the macro multiple times: 10@a (plays macro a 10 times)
  3. Text Object Motions:

    • Change inside a word: ciw
    • Change inside a paragraph: cip
    • Change inside quotes: ci"
    • Delete inside a tag: dit
    • Select a sentence: vas
  4. Multiple Cursor Editing (Using Plugins):

    • If you want to use multiple cursors, you can use a plugin like vim-multiple-cursors.
    • Add multiple cursors using Ctrl + n, and then modify text at each cursor.

Command-Line Mode Advanced Features

  1. Ex Commands for Text Substitution:

    • Replace all occurrences of a word: :%s/old/new/g
    • Replace all occurrences with confirmation: :%s/old/new/gc
    • Replace in a specific range of lines: :5,10s/old/new/g (replace between lines 5 and 10)
  2. Execute Shell Commands:

    • Run a shell command from within Vim: :!command
    • Replace the content of the file with the output of a shell command: :%!command
  3. Vim Buffers and Windows:

    • List open buffers: :ls
    • Switch between buffers: :b{buffer_number} or :bnext
    • Delete a buffer (close it): :bd
    • Open a new window: :new filename
    • Split window horizontally: :split
    • Split window vertically: :vsplit
    • Switch between windows: Ctrl + w {h,j,k,l}

Advanced Search and Replace

  1. Regular Expressions:

    • Vim supports regular expressions for more complex search and replace:
      • Search for a word that starts with "test": /\v^test
      • Replace all digits with X:%s/\d/X/g
      • Replace a specific line number range: :5,10s/old/new/g
  2. Search and Replace Across Multiple Files:

    • Search across all files in the current directory and replace: :args *.txt | argdo %s/old/new/g
    • Search recursively through all files in a directory: :args **/*.py | argdo %s/old/new/g

Managing Sessions and Projects

  1. Saving and Loading Sessions:

    • Save your session (buffers, windows, etc.): :mksession!
    • Load a session: :source Session.vim
  2. Project Management:

    • Use vim-projectionist or NERDTree plugins to manage your project files.
    • Quickly navigate between related files, such as opening tests with :TestFile.

Advanced Window and Tab Management

  1. Using Tabs in Vim:

    • Open a new tab: :tabnew
    • Switch between tabs: :tabnext:tabprev
    • Close the current tab: :tabclose
    • List open tabs: :tabs
  2. Window Management:

    • Create a new horizontal split: :split filename
    • Create a new vertical split: :vsplit filename
    • Switch between windows: Ctrl + w then h/j/k/l
    • Close a window: Ctrl + w then c
    • Resize a window: Ctrl + w then + or - (to resize up or down)

Advanced Vim Plugins

  1. Fuzzy Finder (fzf or CtrlP):

    • Use fzf or CtrlP for fast file navigation and searching in your project directory.
    • Ctrl + p opens the fuzzy finder for file navigation.
  2. Autocompletion (coc.nvim or YouCompleteMe):

    • Use coc.nvim for autocompletion powered by the Language Server Protocol (LSP).
    • Install plugins for linting, formatting, and autocompletion.
  3. Git Integration (vim-fugitive):

    • View Git status: :Gstatus
    • Stage changes: :Gadd
    • View commit history: :Glog
  4. Syntax Highlighting and File Detection (vim-polyglot):

    • Install vim-polyglot for support for multiple languages and enhanced syntax highlighting.

Customization with Vimrc

  1. Setting Up Key Mappings:

    • Remap keys in your .vimrc file:

      nnoremap <leader>f :Files<CR>  " Map to fuzzy file search
      vnoremap <leader>c "+y         " Copy selected text to system clipboard
      
  2. Plugins and Package Management:

    • Use vim-plug or Vundle to manage plugins. For example:

      call plug#begin('~/.vim/plugged')
      Plug 'junegunn/fzf'
      Plug 'tpope/vim-fugitive'
      call plug#end()
      
  3. Customizing the Status Line:

    • Customize the status line using vim-airline or lightline.vim to show helpful information such as the current file name, line number, and Git branch.
  4. Enable Clipboard Support:

    • For clipboard integration, ensure Vim is compiled with clipboard support. If not, install Vim with +clipboard.
    • You can yank to the system clipboard with: "+y and paste with "+p.