How To Use Vim Editor In Linux

How To Use Vim Editor In Linux

Reading time1 min
#Linux#Coding#Productivity#Vim#VimEditor#LinuxEditing

Mastering Vim Editor in Linux: From Essential Commands to Custom Workflows

Think Vim is just an old-school editor? Think again. This guide treats Vim not as a relic, but as a powerful, customizable tool that can be tailored into a modern developer’s secret weapon for lightning-fast coding and system management.


If you’ve ever dabbled with Linux, chances are you’ve come across Vim. It’s often praised—and sometimes feared—as a “power-user” editor for text, config files, and source code alike. But mastering Vim isn’t about memorizing dozens of commands; it’s about embracing its architecture to speed up your workflow and customize it to your needs.

In this post, I’ll walk you through the essentials of using Vim in Linux, then dive into how to extend it with custom workflows. Whether you want to quickly edit configuration files or compose complex scripts, mastering Vim can transform your productivity.


Why Choose Vim?

Before diving in, let’s be clear: Vim stands for Vi Improved—an evolution of the classic Vi editor. It’s lightweight yet incredibly powerful. Unlike GUI editors, Vim is modal — the same set of keys behaves differently depending on whether you’re inserting text or navigating — making editing blazing fast once you internalize the model.

  • It starts in "Normal mode" where keys control navigation and commands.
  • Switch to "Insert mode" to input text.
  • Use "Visual mode" for selecting blocks of text.

Getting Started: Launching and Quitting Vim

Open your terminal and try:

vim filename.txt

If filename.txt doesn’t exist yet, Vim will create it when you save.

Quitting

  • Save and exit: :wq + Enter
  • Exit without saving: :q! + Enter
  • Just save (without exiting): :w + Enter

The Basics: Navigating Inside Vim

Navigation in Normal mode:

ActionCommandDescription
Move cursor lefthOne character left
Move cursor downjOne line down
Move cursor upkOne line up
Move cursor rightlOne character right
Beginning of line0 (zero)Jump to start of current line
End of line$Jump to end of current line
Page downCtrl + dScroll half page down
Page upCtrl + uScroll half page up

Tip: You can repeat motions by prefixing them with numbers (e.g., press 5j to move five lines down).


Editing Text

To enter Insert Mode (type text), use:

  • i: Insert before the cursor.
  • a: Insert after the cursor.
  • o: Open a new line below the current one.

To delete:

  • x: Delete character under cursor.
  • dd: Delete entire current line.
  • Pressing u: Undo last change.
  • Pressing Ctrl + r: Redo undone change.

Example sequence:

  1. Navigate with arrow keys or hjkl keys to where you want.
  2. Press i, type some text.
  3. Press Esc to return to normal mode.
  4. Save your changes with :w.

Searching Text Fast

To search forward for "config":

/config

Press Enter—cursor jumps to first instance. Then:

  • Press n to jump to next match,
  • Press N (shift+n) for previous match.

Searching backward uses question mark:

?function

Efficient Copying and Pasting (Yanking)

Common commands:

  • yy: Yank (copy) entire current line.
  • {number}yy: Yank multiple lines (e.g., 3yy)
  • p: Paste below the cursor position.
  • P: Paste above the cursor position.

Try this out by yanking some lines and pasting them elsewhere in the file.


Customizing Your Vim Workflow

Vim shines brightest when customized. Let’s explore how you can create your own environment.

1. The .vimrc File

You can control settings and keybindings by creating/modifying this file in your home directory (~/.vimrc). Example basic .vimrc:

" Enable syntax highlighting
syntax on

" Show line numbers
set number

" Enable mouse support in all modes
set mouse=a 

" Highlight search results
set hlsearch

" Ignore case except if uppercase is used in search term
set ignorecase
set smartcase

" Enable autoindentation for code editing
set autoindent 

" Set tabs as 4 spaces 
set tabstop=4 
set shiftwidth=4 
set expandtab 

This config will already make your editing experience sharper from day one.

2. Plugins with vim-plug

Extend Vim functionality via plugins—which add everything from Git integration, fuzzy file searching, file explorers, code completion, etc.

Installing vim-plug

Run these commands from terminal:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
   https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Then add plugin management block inside .vimrc, for example:

call plug#begin('~/.vim/plugged')

Plug 'preservim/nerdtree'        " File tree explorer plugin example
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } " Fuzzy finder 

call plug#end()

Inside Vim run:

:PlugInstall

Now try opening NerdTree with:

:NERDTreeToggle

3. Create Your Own Macros or Mappings

Map frequently used commands or snippets for quick insertion.

Example: Map F5 key to save file without quitting:

nnoremap <F5> :w<CR>

Add this into .vimrc. Now pressing F5 saves instantly from normal mode!


Real-Life Example Workflow Using Vim on Linux

Imagine editing system configs like /etc/nginx/nginx.conf. Here’s a quick example:

  1. Open config with sudo privileges:
    sudo vim /etc/nginx/nginx.conf
    
  2. Use search /server_name to locate domain declarations fast.
  3. Use visual block mode (Ctrl+v) if you want column editing multiple lines.
  4. Make edits; paste yanked blocks as needed (p).
  5. Save file with <F5> if mapped earlier or just use:
    :wq
    

Bonus tip: Combine shell power by opening multiple files quickly,

vim /etc/nginx/nginx.conf /var/log/nginx/error.log

Switch between open files with command like:

:bnext   " next buffer/file 
:bprev   " previous buffer/file 

Next Steps on Your Journey With Vim

Vim is as simple or complex as you make it—start slow by mastering navigation and editing essentials today.

Once comfortable, dive deeper into:

  • Writing custom functions/macros,
  • Integrating Git inside vim,
  • Leveraging language servers for code completion,
  • Exploring advanced plugins like CoC or ALE for linting and AI code assistance.

Remember: speed gains come naturally once muscle memory builds through repeated practice!


Summary

Vim is more than legacy software—it’s a gateway into lightning-fast effective Linux editing workflows critical for developers and sysadmins alike. Armed with these essentials plus your customized setup, you’ll speed through tasks once slow and tedious.

Got any favorite tips or plugins? Share them below!


Happy Vimming!