How To Write In Vim

How To Write In Vim

Reading time1 min
#technology#productivity#software#vim#modalediting#texteditor

Mastering Efficiency: How to Write in Vim by Leveraging Its Modal Editing Power

Tired of wasting time on repetitive, mouse-driven editing? For engineers and writers who spend hours in source files, documentation, or configuration manifests, Vim's modal editing moves productivity into a different league. It’s not intuitive—at first—but it is efficient.


Modal Editing Demystified

Vim's architecture centers around distinct modes. No ambiguous "edit everything/command everything" mashup:

  • Normal mode: All movement and edits. No accidental text.
  • Insert mode: For raw text input.
  • Visual mode: Select/operate on text blocks.
  • Command-line mode: File, search, and substitute commands.

Mode switching is instant: i enters Insert, <Esc> returns to Normal. No context menus or hand-hopping—just fingers on home row.

iHello Kubernetes YAML<Esc>
0dw          " Deletes 'Hello' at start of line

Movement: The Real Differentiator

It’s never just typing—it’s moving. Stop arrow-keying through lines; Vim provides granular movement primitives:

  • h, j, k, l: Left, down, up, right (by character/line)
  • w / b: Forward/backward by word
  • 0 / $: Line start/end
  • gg / G: First/last line (useful for long logs: :e /var/log/syslog)

For block edits in a YAML or config:

Vjj>     # Visual Line, select next two lines, indent (critical when fixing block structure)

Known issue: Deeply indented YAML can confuse which block you're in. Toggle set number or set relativenumber for better orientation.


Insert with Intention

Default to Insert mode only when writing. Use a (append), A (append at end of line), o (open line below), O (open above):

# Adding missing config in a Helm values.yaml:
/nodeSelector      # Search for section
O    # Open new line above
esc  # Back to Normal once done

Major productivity boost: ci"—change inside quotes. Works in JSON, Markdown links, etc.

ci"This is the new string<Esc>

Undo/Redo: Not an Afterthought

Engineers rely on reliable changelogs. In Vim:

  • u: Undo single change.
  • <C-r>: Redo (opposite of undo).

Vim maintains a tree of undos per file session; can navigate (:undolist), though this is per-buffer, not persistent after quit/close (unless using plugins like undotree.vim).


Editing Words: Speed Up Refactoring

Practical with docs/code comments. The following eliminates backspace habits:

CommandFunctionTypical Use
cwChange word, ends at word edgeRefactor variable
ciwChange inner word (any cursor pos)Fix typo in comment
diwDelete inner wordRemove debug word inline
dwDelete to next word boundaryRemove placeholder

Ex:
The lattnce supports failover.
bciwlatency


Visual Mode: Bulk Text Transformation

Multiline configs or documentation may need realignment or prefixing. Visual line (V) selects whole lines.

# Example: prefix log lines for quick grep filtering
Vjj:I[DEBUG] <Esc>    # Three lines prefixed with [DEBUG]

Side note: Tabs vs. spaces—Vim respects expandtab and shiftwidth. Set in project .vimrc or explicitly per buffer (:set sw=2 et).


Macros: Automating Repetitive Edits

For structured but repetitive edits (e.g., converting error logs to Markdown bullet lists):

qaI- <Esc>jq   # Macro 'a': insert "- " and move to next line
10@a           # Apply 10 times from current cursor

Low overhead, but remember macros are lost after session unless explicitly saved (rarely needed—macros solve one-off tasks best).


Search and Replace: Global, Context-Aware

Vim's search/substitute via command-line is robust. Example: Renaming a deprecated field across an entire CRD manifest.

:%s/spec\.replicas/spec\.count/gc

The g is for multiple per line; c confirms each. For partial case matches, add I (case-insensitive). Missed one? Undo (u) and retry with a tighter pattern.


Advanced: Spell Check and Soft Wrapping

For Markdown/Asciidoc or code comments, enable spellcheck:

:set spell spelllang=en_us

Soft wraps for writing docs—install vim-pencil (tested on Vim 8+, Neovim 0.4+):

:PencilSoft   # Activates soft mode for current buffer

Non-Obvious Tip: Session State Recovery

Occasionally, a mid-session crash or lost SSH connection will drop your unsaved edits. Vim swaps will prompt on next open:

E325: ATTENTION
Found a swap file by the name ".index.md.swp"
    owned by: user  ... (process ID ...)
    file name: /project/docs/index.md

Recover with :recover, then :wq to save as normal. Don't ignore; swap files can prevent subtle data loss in multi-user systems.


Conclusion: It's Not About Speed—It's About Flow

Vim’s efficiency appears after the learning curve flattens. Keyboard-centric editing—jumping, transforming, and refactoring without leaving home row—translates into fewer interruptions, more accurate changes, and better focus. Not perfect: modal confusion can cause mistakes early on, and plugin/locale quirks may surface in heavy customizations or remote shells.

Engineers rarely go back after adaptation. Start small—commit to a week, avoid the mouse, lean on undo. Consider using a stripped .vimrc with only essential settings at first.


Questions about integration with LSPs or remote editing over SSH? Real-world obstacles are common—raise them and expect nuanced solutions.