Vim Wiki

  • go back

  • vimwiki doc a personal wiki for Vim

  • marks
    • special marks

      Vim has some special marks which it sets automatically. Here are some of the most useful:

      Mark Description
      `. jump to position where last change occurred in current buffer
      `" jump to position where last exited current buffer
      `0 jump to position in last file edited (when exited Vim)
      `1 like `0 but the previous file (also `2 etc)
      '' jump back (to line in current buffer where jumped from)
      ` ` jump back (to position in current buffer where jumped from)
      `[ jump to beginning of previously changed or yanked text
      `] jump to end of previously changed or yanked text
      `< jump to beginning of last visual selection
      `> jump to end of last visual selection

    • save and store mark position

      You can save and restore the position of a mark in Vim with the following snippet:

      let save_a_mark = getpos("'a")
      call setpos("'a", save_a_mark)
      :help '<
      

      Just a heads-up: for Visual mode, the behavior of '< and '> changes depending on the mode. In visual line mode (V), the column of '< is zero, and the column of '> is a large number equal to v:maxcol.

      Here’s a quick rundown of the marks +:

      • '<: Points to the first line or character of the last selected Visual area in the current buffer. In block mode, it may also be the last character in the first line to define the block.
      • '>: Points to the last line or character of the last selected Visual area in the current buffer. In block mode, it may also be the first character of the last line to define the block. Note that ‘selection’ applies, so the position may be just after the Visual area.

    • helpful resources

      for more in-depth information, check out:



  • folding

    • shortcut
      • utilities
        • <ctrl-k>i to set fold method
        • g<Ctrl-G> to see word count
      • creating fold
        • manual method
          • it is the simplest, and is the default.
          • it method uses markers in the text to define the start and end of each fold.
            • command description
              zf create a fold
              zd delete a fold
              zE delete all folds
              zR open all folds
              zM close all folds
      • cheat sheet
        • main commands
          • command description
            zR decreases the foldlevel to zero – all folds will be open
            zM closes all open folds
        • fold commands
          • command description
            za toggle fold at the cursor
            zA toggle all folds recursively at the cursor
            zo opens a fold at the cursor
            zO opens all folds at the cursor
            zc close fold at the cursor
            zC close all folds at the cursor
            zm increases the foldlevel by one
            zM closes all open folds
            zr decreases the foldlevel by one
            zR decreases the foldlevel to zero – all folds will be open
        • jump commands
          • command description
            zj moves the cursor to the next fold
            zk moves the cursor to the previous fold
            [z move to start of open fold
            ]z move to end of open fold
    • helpful resources

      for more in-depth information, check out:



  • text replacement and/or manipulation §

    • delete blank lines
      • :%s/^\s*$\n//gc
      • :g/^\s*$/d
    • extracting email addresses
      • for extracting multiple email addresses from a single line, separating them with commas:
        • :%s/[^<]*<\([^>]\+\)>[^<]*/\1, /gc
        • it looks for anything between < and > and captures it (that’s an email address!) - then, it replaces the entire matched section with just the captured email address followed by a comma and a space.
      • for extracting a single email address per line
        • :%s/.\+<\(.\+\)>.*/\1/gc
        • it captures the email address within the < and > and replaces the entire line with just the captured address.
    • the vim global command
    • advanced search and replace in Vim
  • advanced search and replace in Vim

    • the vim global command §

      The global command is one that can be very powerful for performing operations across a matched pattern. The general idea is to match some text in a buffer and then perform an operation on all matches. Example: :g/pattern/command +

      • Delete Matching Lines
        • :g/pattern/d
          • This command deletes all lines matching the pattern.
        • :g/error/d
          • Deletes all lines matching error
      • Delete Non-Matching Lines
        • Use almost the same command but instead use a g! instead of just g. This will invert the match and grab all lines that do NOT match.
          • :g!/pattern/d
            • This command deletes all lines not matching the pattern.
          • :g!/error/d
            • Deletes all lines not matching error.
      • Substitute in Matching Lines
        • Similar to how you would use %s, you can use g to find and replace a pattern.
          • :g/error/s/error/errorMessage/
            • Performs a substitution only on lines matching pattern.
            • This will replace only the first “error”, if you want to replace all then add g to the end:
              • :g/pattern/s/foo/bar/g
                • This command replaces all occurrences of “foo” with “bar” in lines matching the pattern.
              • :g/error/s/foo/bar/g
                • Replaces all occurrences of “foo” with “bar” in lines matching error.
      • Execute a Command on Matching Lines
        • To run a command you can still use the g command in the same structure that we have seen before and using the normal keyword:
          • g/pattern/normal command
            • Executes a normal-mode command on all matching lines.
              • :g/TODO/normal A // Comment
                • This example will append ` // Comment to lines containing TODO`.
      • Yank Matching Lines
        • :g/pattern/yank
          • Yanks all lines matching pattern into the default register.
        • g/error/y
          • Yank lines containing “error”.
      • Advanced Patterns
        • using regular expressions to narrow down your matches to a smaller subset:
          • :g/^foo/d
            • Match lines starting with “foo”
          • :g/bar$/d
            • Match lines ending with “bar”
          • :g/\d/d
            • Match lines ending with “bar”
      • Chaining Multiple Commands
        • To chain commands, use the | like this:
          • :g/pattern/command1 | command2
            • This will execute command1 and then command2.
          • :g/DEBUG/d | w
            • Delete lines containing “DEBUG” and then save the file.

    • replacing in multiple files

      Example Workflow:

      Here’s a concise example of the entire process:

      :grep foo **/*.js      " Search for 'foo' in JavaScript files
      :cdo s/foo/bar/gc      " Line-wise replace 'foo' with 'bar' (with confirmation)
      :cfdo %s/foo/bar/gc    " File-wise replace 'foo' with 'bar' (with confirmation)
      :cfdo up               " Save changes
      

      This guide provides a friendly walkthrough for performing powerful search and replace operations across multiple files in Vim. We’ll break down the process into smaller, manageable steps.

      1. populating the Quickfix list:

        The first step is to identify all occurrences of your search pattern. Several commands can achieve this by populating Vim’s “quickfix list.” Here are a few options:

        • :grep: A built-in command for searching. For example, to search for “foo” in all JavaScript files within the current directory and its subdirectories, use:

           :grep foo **/*.js
          
        • :vimgrep: Similar to :grep, but offers more advanced pattern matching capabilities.

        You can view the quickfix list with :cwindow.

      2. performing the replacement:

        Once the quickfix list is populated, you can perform the replacement using either :cdo (operate on each line in the quickfix list) or :cfdo (operate on each file containing matches).

        • :cdo (Line-wise Replacement):

           :cdo s/foo/bar/gc
          

          This replaces “foo” with “bar” on each line listed in the quickfix list, prompting for confirmation (c) before each substitution.

        • :cfdo (File-wise Replacement):

           :cfdo %s/foo/bar/gc
          

          This replaces all instances of “foo” with “bar” within each file listed in the quickfix list, also prompting for confirmation (c) before each substitution. The % ensures the substitution applies to the entire file.

        • Skipping Confirmation: If you’re confident in your replacement, you can omit the c flag for automatic substitution. See :help :s_flags for more details on substitution flags.

      3. saving changes:

        After performing the replacements, save the changes to disk using :cfdo update (or its abbreviated form, :cfdo up):

        :cfdo update
        

        This command writes only the modified files to disk.

      • deleting lines with :global:

        You can also use the quickfix list to delete lines matching a pattern. For example, to delete all lines containing “my-grep-pattern” within the files in the quickfix list:

        :cfdo %g/my-grep-pattern/d
        
      helpful resources

      For more in-depth information, check out:


    • text manipulation in Vim