Most Important Vim Commands

Vim serves as a pliable and proficient text editor that facilitates text manipulation and code composition. Its customizable characteristics, shortcuts, and supplementary tools streamline the editing process. Vim employs separate modes for different tasks, ensuring effortless text traversal and adjustment. Moreover, its lightweight design and cross-platform functionality enhance its portability. Vim offers a substantial and comprehensive array of commands, empowering swift text manipulation and intricate editing procedures. Hence, it remains a favored preference among developers and those desiring a potent yet uncomplicated text editing involvement.

Here are some most important vim commands:

  1. Insert Mode: To enter insert mode and start typing, press i. Press Esc to exit insert mode and return to normal mode.
  2. Saving and Quitting: To save changes and quit Vim, use the command :wq or :x. To quit without saving changes, use :q!.
  3. Movement: Vim offers several movement commands:
    h, j, k, l for left, down, up, and right respectively.
    w to move forward by a word, b to move back by a word.
    0 to move to the beginning of a line, $ to move to the end of a line.
    gg to move to the beginning of the file, G to move to the end of the file.
    Ctrl+f to move forward by a page, Ctrl+b to move back by a page.
  4. Deletion and Yanking: In normal mode, you can delete characters, words, lines, or yank (copy) them without pasting.
    x to delete a single character, dw to delete a word, dd to delete a line.
    yy to yank a line, yw to yank a word.
    p to paste the yanked or deleted text after the cursor.
  5. Undo and Redo: To undo the last action, press u. To redo an undone action, press Ctrl+r.
  6. Search and Replace:
    /pattern to search forward for “pattern”, ?pattern to search backward.
    n to find the next occurrence, N to find the previous occurrence.
    :s/old/new/g to replace “old” with “new” globally on the current line.
    :%s/old/new/g to replace “old” with “new” globally in the entire file.
  7. Copy and Paste:
    In visual mode, use v to select text, then y to yank (copy) it.
    p to paste the yanked text after the cursor.
  8. Split Windows:
    :split to split the window horizontally, :vsplit to split vertically.
    Ctrl+w followed by an arrow key to switch between windows.
  9. Navigating Tabs:
    :tabnew to open a new tab, :tabnext or gt to move to the next tab.
    :tabprevious or gT to move to the previous tab, :tabclose to close the current tab.
  10. Command Mode: Press : in normal mode to enter command mode. Here you can execute various commands, such as saving, quitting, searching, and more.

These are just a few of the many Vim commands available. Vim has a steep learning curve, but once mastered, it can greatly enhance your text editing efficiency. Remember to use the :help command within Vim to access the comprehensive Vim documentation for more information on specific commands.

Here are some more Vim commands that can be useful:

  1. File Operations:
    :e <file> to open a specific file.
    :sp <file> to open a file in a new split window.
    :vs <file> to open a file in a new vertical split window.
    :w <file> to save the current buffer to a different file.
    :r <file> to insert the contents of a file into the current buffer.
  2. Buffers and Tabs:
    :ls to list all open buffers.
    :b <number> to switch to a specific buffer.
    :bd to close the current buffer.
    :tabnew to open a new tab.
    :tabnext or gt to switch to the next tab.
    :tabprevious or gT to switch to the previous tab.
    :tabmove <n> to move the current tab to position <n>.
    :tabclose to close the current tab.
    :tabonly to close all other tabs.
  3. Search and Navigation:
    * to search for the word under the cursor.
    # to search for the previous occurrence of the word under the cursor.
    gd to jump to the local declaration of the variable under the cursor.
    Ctrl+o to jump back to the previous location.
    Ctrl+i to jump forward to the next location.
  4. Marks and Bookmarks:
    m<char> to set a lowercase mark at the current cursor position.
    `char to jump to the line containing the mark.
    'char to jump to the beginning of the line containing the mark.
    :marks to list all the marks in the current buffer.
  5. Macros:
    q<char> to start recording a macro into the register <char>.
    q to stop recording the macro.
    @<char> to execute the macro stored in the register <char>.
    @@ to replay the last executed macro.
  6. Visual Mode Operations:
    v to start characterwise visual mode.
    V to start linewise visual mode.
    o to move to the other end of the visual selection.
    gv to reselect the previously selected text.
    Ctrl+v to start blockwise visual mode.
    Use movement commands to select a block of text.
    I to insert at the beginning of each line in the block.
    A to append at the end of each line in the block.
    r to replace the selected block with a single character.
    d to delete the selected block.
    y to yank (copy) the selected block.
  7. Formatting:
    gg=G to automatically format the entire file.
    =ap to format the current paragraph.
    gqap to format the current paragraph using the textwidth setting.
  8. Folding:
    zf#j to create a fold from the current line and down # lines.
    zf/string to create a fold from the current line to the next occurrence of “string”.
    zo to open a fold, zc to close a fold.
    za to toggle the state of a fold.
  9. Multiple Cursors:
    Ctrl+v to start blockwise visual mode.
    Select multiple lines using movement commands.
    I to insert at the beginning of each line, A to append at the end of each line.
    Esc to apply the changes to all lines simultaneously.
  10. Window Management:
    Ctrl+w + to increase the current window’s height.
    Ctrl+w to decrease the current window’s height.
    Ctrl+w > to increase the current window’s width.
    Ctrl+w < to decrease the current window’s width.
    Ctrl+w = to make all windows equal in size.
    Ctrl+w r to rotate windows in a clockwise direction.
  11. CommandLine Completion:
    Ctrl+d to show available commandline completions.
    Ctrl+n to complete the commandline with the next match.
    Ctrl+p to complete the commandline with the previous match.
  12. Spell Checking:
    :set spell to enable spell checking.
    ]s to jump to the next misspelled word.
    [s to jump to the previous misspelled word.
    z= to see spelling suggestions for a misspelled word.
  13. Text Objects:
    i" to select the text within double quotes.
    a" to select the text within double quotes along with the quotes.

Text objects in Vim are powerful and versatile commands that allow you to work with specific portions of text, such as words, sentences, paragraphs, or even entire blocks. Text objects provide a convenient way to perform operations on targeted sections of text without the need for precise manual cursor movement.

  1. Word Objects:
    iw selects the inner word, excluding surrounding whitespace.
    aw selects the entire word, including surrounding whitespace.
    Examples:
    diw deletes the inner word.
    ciw changes the inner word.
    viw visually selects the inner word.
  2. Sentence and Paragraph Objects:
    is selects the inner sentence.
    as selects a sentence, including trailing whitespace.
    ip selects the inner paragraph.
    ap selects a paragraph, including trailing whitespace.
    Examples:
    dis deletes the inner sentence.
    cis changes the inner sentence.
    vis visually selects the inner sentence.
  3. Tag Objects:
    it selects the inner HTML tag.
    at selects a complete HTML tag, including surrounding tags.
    Examples:
    dit deletes the inner HTML tag.
    cit changes the inner HTML tag.
    vit visually selects the inner HTML tag.
  4. Block Objects:
    ib selects the inner () block.
    ab selects a () block, including the parentheses.
    iB selects the inner {} block.
    aB selects a {} block, including the curly braces.
    Examples:
    dib deletes the inner () block.
    cib changes the inner () block.
    vib visually selects the inner () block.

These are just a few examples of text objects in Vim. Text objects can be combined with various editing commands, such as delete (d), change (c), yank (y), or visually select (v), to perform operations on the selected text objects.

The power and flexibility of text objects make editing tasks more efficient, allowing you to work with precise sections of text effortlessly. Experiment with different text objects and their combinations to enhance your editing workflow in Vim.

These additional commands should expand your Vim repertoire and help you become more efficient when editing text.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top