DEV Community

Cover image for Mastering VIM: A Complete Beginner's Guide to the Command-Line Editor
Gunjan Mishra
Gunjan Mishra

Posted on

Mastering VIM: A Complete Beginner's Guide to the Command-Line Editor

1. Introduction to VIM

1.1 What is VIM?

Remember the last time you had to quickly edit code on a remote server? That's where VIM shines! While VS Code needs a graphical interface, VIM lets you edit code right in your terminal, making it perfect for cloud servers and remote development.

Let me share Anaya's story. She was a full-stack developer working on a client's e-commerce website. During a crucial product launch, her client Ashish called in panic: "The payment gateway is down! Can you fix it immediately?"

The issue was on the production server, and Anaya needed to quickly edit a configuration file. She tried logging into the server's control panel, downloading the file, opening it in VS Code, editing it, and uploading it back - but the server was restricting file uploads for security reasons.

Then she remembered VIM. She simply:

  1. Connected to the server via SSH
  2. Opened the config file with VIM
  3. Made the fix in seconds
  4. Saved and tested

The site was back up in minutes, and Ashish was impressed with the quick resolution.

Ananya working on vim

Why developers like Anaya choose VIM:

  • Perfect for quick server fixes (no downloading/uploading needed)
  • Works smoothly even with slow internet connections
  • Great for pair programming through shared terminal sessions
  • Makes you look like a coding wizard πŸ˜‰

Remember: Tools like VS Code are great for development, but VIM is your best friend when you need to make quick fixes on servers or work in terminal-only environments.

1.2 Installing VIM

Setting up VIM is easier than you might think! Let's break it down:

First, check if VIM is already installed (it often is!):

vim --version
Enter fullscreen mode Exit fullscreen mode

If not, here's how to get it:

For Ubuntu/Linux Users:

sudo apt install vim
Enter fullscreen mode Exit fullscreen mode

For Mac Users:

# Using Homebrew
brew install vim
Enter fullscreen mode Exit fullscreen mode

For Windows Users:

  1. Visit https://www.vim.org/download.php
  2. Download the latest version
  3. Run the installer

Quick Test:

  1. Open terminal
  2. Type vim test.txt
  3. Press i to start typing
  4. Type "Hello VIM!"
  5. Press Esc, then type :wq to save and quit
  6. Type cat test.txt to see your saved code in terminal

Congratulations! You've just made your first edit in VIM! πŸŽ‰

hello vim screenshot

Pro Tip: Start small! Begin by using VIM for simple config file edits on servers, then gradually expand to larger tasks as you get comfortable.

Note: Want to know the real power of VIM? Wait until you need to edit files on 10 different servers - that's when you'll fall in love with it!

2. Understanding VIM's Modes

2.1 The Concept of Modes

VIM works differently from typical text editors. Instead of always being ready to type text, VIM uses modes. What makes a mode? It's a state where your keyboard keys behave in specific ways. Pressing 'x' in Insert Mode types the letter 'x', but in Normal Mode, it deletes a character. This might seem strange at first, but it makes editing much more efficient.

The Two Main Modes You Need to Know:

  • Normal Mode: This is your default state. Here, your keyboard becomes a command center for navigation and text manipulation. Every key can perform a special action.
  • Insert Mode: This works like a regular text editor. When you need to type or edit text, this is where you'll be.

Switching Between Modes is Simple:

  • Press Esc anytime to get to Normal Mode. Don't worry about pressing it too much - it's always safe!
  • Press Esc + i when you want to type text (enters Insert Mode)

2.2 Normal Mode: Your Command Center

Normal Mode is where VIM's power lies. Think of it as your control room - a place where you can move around your text, make changes, select text, and execute commands. Almost everything you do in VIM starts here.

Moving Around in Normal Mode:


h - move left one character
j - move down one line
k - move up one line
l - move right one character
w - jump forward to the start of the next word
b - jump backward to the start of the previous word
0 - jump to the start of the current line
$ - jump to the end of the current line

Enter fullscreen mode Exit fullscreen mode

Just press : and type your command:


:w     - save your current file
:q     - quit VIM (will warn if you haven't saved)
:q!    - force quit VIM (will no warn even if you haven't saved)
:wq    - save and quit in one command
:help  - when you need to learn about a command

Enter fullscreen mode Exit fullscreen mode

Everyday Normal Mode Actions:


dd     - delete the current line
yy     - copy the current line
u      - undo your last action
Ctrl+r - redo what you just undid

Enter fullscreen mode Exit fullscreen mode

2.3 Insert Mode

Insert Mode is straightforward - it's where you actually type your text, just like any regular editor. The cursor blinks, and your keys type letters instead of executing commands.

Quick Tips for Success:

  1. Make Normal Mode your home - return there when you're done typing
  2. Don't hold down the Esc key - one press is enough
  3. Watch the bottom left of your screen - it shows which mode you're in
  4. Practice the basic movements (hjkl) until they feel natural
  5. Start with these basics and add more commands as you get comfortable

Remember: The power of VIM comes from Normal Mode. While Insert Mode is for typing text, Normal Mode is where you'll do most of your editing and navigation.

3. Essential VIM Commands:

Let's see a complete list of essential commands.
Remember: You need to press Esc first to use these commands, as they only work in Normal mode.

Essential VIM Commands List

Basic Workflow:

Esc         - return to Normal mode (use this before any command)
i           - enter Insert mode (start typing)
:w          - save file
:q          - quit
:wq or :x   - save and quit
:q!         - quit without saving

Enter fullscreen mode Exit fullscreen mode

Navigation:

Esc then:
h           - move left
j           - move down
k           - move up
l           - move right
w           - jump to next word start
b           - jump to previous word start
0           - jump to line start
$           - jump to line end
gg          - jump to file start
G           - jump to file end

Enter fullscreen mode Exit fullscreen mode

Text Operations:

Esc then:
x           - delete character under cursor
dd          - delete current line
yy          - copy current line
p           - paste below
u           - undo
Ctrl+r      - redo

Enter fullscreen mode Exit fullscreen mode

Search and Replace:

Esc then:
/text       - search for 'text'
n           - next search result
N           - previous search result
:%s/old/new/g - replace all 'old' with 'new'

Enter fullscreen mode Exit fullscreen mode

Quick Scrolling:

Esc then:
Ctrl+f      - page down
Ctrl+b      - page up

Enter fullscreen mode Exit fullscreen mode

Pro Tips:

  1. Keep hitting Esc whenever you're unsure - it's always safe!
  2. After any command, you can press i to start typing again
  3. If you're lost, press Esc and type :help
  4. Save often using Esc then :w

Remember: Always start with Esc to ensure you're in Normal mode before using any command. It's better to press Esc too many times than too few!

4. Customizing Your VIM Environment

What is .vimrc?

VIM starts with basic default settings, but you can customize it to match your preferences. This is where .vimrc comes in - it's a configuration file that VIM reads when it starts up. Any settings you put in this file will automatically apply every time you use VIM.

Finding Your .vimrc

Your .vimrc file lives in your home directory:

bash
Copy
# Linux/Mac
~/.vimrc

# Windows
C:\Users\YourUsername\_vimrc

Enter fullscreen mode Exit fullscreen mode

If the file doesn't exist, you can create it.

Creating Your First .vimrc

Let's make a simple .vimrc:

  1. Open terminal
  2. Type: vim ~/.vimrc
  3. Press i to start typing
  4. Add your settings (Copy and paste the settings given below)
  5. Press Esc then type :wq to save

Basic .vimrc Settings for Beginners

Here's a simple, beginner-friendly .vimrc file:

" Basic Settings
set number         " Show line numbers
syntax on          " Enable syntax highlighting
set autoindent     " Auto-indent new lines
set tabstop=4      " Tab width of 4 spaces
set softtabstop=4  " Tab width while editing
set mouse=a        " Enable mouse support

" Make VIM more user-friendly
set confirm        " Ask to save changes instead of failing
set wildmenu       " Better command completion
set showmatch      " Highlight matching brackets
set incsearch      " Show search matches while typing

" Visual Helpers
set cursorline     " Highlight current line
set ruler          " Show cursor position
set showcmd        " Show incomplete commands

Enter fullscreen mode Exit fullscreen mode

vimrc file screenshot

Pro Tips:

  1. Add settings one at a time
  2. Test each setting to see if you like it
  3. Comment your settings (use " for comments)
  4. Keep a backup of your .vimrc file

Remember: You can edit .vimrc anytime. Changes take effect when you restart VIM or type :source ~/.vimrc

5. Final Thoughts

Your VIM Journey

Starting with VIM is like learning to ride a bike - it might feel wobbly at first, but once you get the hang of it, you'll wonder how you managed without it.

VIM isn't just a text editor - it's an investment in your productivity. Take it one command at a time, and you'll be editing like a pro before you know it!

I hope this guide helps you get started with one of the most popular Command-Line Editor VIM !!
If you found this helpful, please consider sharing it on social media and following me for more tech content. If you have any feedback or suggestions, please share them in the comments. Your input will help me create better content going forward.

Happy VIMming! πŸš€

Top comments (0)