DEV Community

Cover image for Vim Text Editor: 15 Must-Know Commands for Developers
Zane
Zane

Posted on • Edited on

3 1 1 1 1

Vim Text Editor: 15 Must-Know Commands for Developers

Vim's efficiency and flexibility make it a top choice for developers. Here are 15 advanced Vim commands, complete with examples, to elevate your coding experience:

1. Macros - Record and Execute

Automate repetitive tasks.

  • Record: q<letter>
  • Execute: @<letter>
   qa...q (record to 'a'), @a (execute 'a')
Enter fullscreen mode Exit fullscreen mode

2. Global Replace with RegEx

Complex pattern-based replacements.

   :%s/old/new/gc
Enter fullscreen mode Exit fullscreen mode

3. Tabs and Buffers

Efficient multiple file handling.

  • New tab: :tabnew <filename>
  • Switch tabs: gt, gT
   :tabnew file.txt, gt, :ls, :b2
Enter fullscreen mode Exit fullscreen mode

4. Window Splits

View files side-by-side.

  • Vertical: :vsp <filename>
  • Horizontal: :sp <filename>
   :vsp file.txt
Enter fullscreen mode Exit fullscreen mode

5. Folding - Code Organization

Hide/reveal code sections.

  • Create fold: zf#j
   zf3j, zo, zc
Enter fullscreen mode Exit fullscreen mode

6. :% - Command Range

Apply commands to the entire file.

   :%norm A;
Enter fullscreen mode Exit fullscreen mode

7. c - Change Command

Replace text efficiently.

   cw
Enter fullscreen mode Exit fullscreen mode

8. Visual Block Mode

Columnar text editing.

  • Enter: Ctrl+v
   Ctrl+v, I#<Esc>
Enter fullscreen mode Exit fullscreen mode

9. :g - Global Command

Execute on lines matching a pattern.

   :g/pattern/d
Enter fullscreen mode Exit fullscreen mode

10. Advanced Search and Navigation

Navigate through files.

  • Marks: m<letter>, 'a
   ma, 'a
Enter fullscreen mode Exit fullscreen mode

11. u and Ctrl+r - Undo/Redo

Manage changes.

   u, Ctrl+r
Enter fullscreen mode Exit fullscreen mode

12. :r !<command> - Insert Command Output

Insert external command output.

   :r !date
Enter fullscreen mode Exit fullscreen mode

13. :! - Execute External Command

Run commands without leaving Vim.

   :!ls
Enter fullscreen mode Exit fullscreen mode

14. :diffthis - Compare Files

Compare files in split view.

   :vsp file.txt, :diffthis
Enter fullscreen mode Exit fullscreen mode

15. :mksession - Save Session

Save your current Vim session.

   :mksession /path/to/session.vim
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs