vim,

10 Vim commands to start with

Jan 03, 2023 · 2 mins read · Post a comment

Vim (VI IMproved), as a common text editor based on Vi, since the early 90s cause headaches to every beginner and junior Ops engineer out there, whatever is a DevOps, SysOps, Cloud Administrator, etc. Here are few commands which I believe could use well as a starting point. I’ve been using them for good 5 years or so.

Prerequisites

  • Vim

Solution

  1. The three modes:
    • Command mode (ESC) or known as Normal mode is the default Vim mode. This is where you run commands.
    • Insert mode (:i) for inserting, updating text.
    • Visual mode (Shift + V) for selecting part of a text, so you could run a command against that part.
  2. Open a file.
    vim filename
    

    Note: If you are already in a Vim session, run :e <filename> to open new or already existing file relative to the current path.

  1. Exit Vim.
    :q
    :q! => quit without saving changes.
    :wq or :x => save changes and quit.
    

    Related post: The best way to exit Vim.

  1. Moving the cursor to the beginning / end of a line / file.
    0 => beginning of a line.
    $ => end of a line.
    gg => beginning of file.
    G  => end of file.
    
  1. Deleting stuff.
    x => delete a single character.
    dw => delete a word.
    D => delete to end of line from current position.
    dd => delete a single line.
    
  2. Copy and pasting.
    y => copy selected text.
    yy => copy current line.
    y$ => copy to end of line from current position.
    p => paste.
    
  3. Set / unset number lines.

  4. Search for a string.
    /somestringhere
    / => next occurance
    ? => previous occurance
    
  5. Replacing all occurrences of “devps” to “devops”.
    :%s/devps/devops/g
    
  • %: is the range that points to every line in the file.
  • /g: a flag that updates all occurrences on a line instead of replacing just the first one.
  1. Help?
    :help
    

Simple workflow

Step 1. Open new or an already existing file.

vim newfile

Step 2. Enter Insert mode. Press i.

Step 3. Add new text or modify the existing one.

Step 4. Exit Insert mode. Press the ESC button.

Step 5. Save the file and exit Vim.

:x or :wq

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.