Skip to content

Module 0.3: Vim for YAML

Hands-On Lab Available
Ubuntu intermediate 25 min
Launch Lab ↗

Opens in Killercoda in a new tab

Complexity: [QUICK] - Learn 10 commands, use them forever

Time to Complete: 20-30 minutes

Prerequisites: None (Vim is pre-installed on exam systems)


After this module, you will be able to:

  • Edit YAML files in vim with proper indentation (2 spaces, no tabs)
  • Fix common YAML errors (wrong indentation, missing fields) in under 30 seconds
  • Configure vim settings (.vimrc) that make YAML editing painless in the exam
  • Use the 10 essential vim commands that cover 95% of CKA editing tasks

The CKA exam requires editing YAML files. A lot. You’ll create manifests, fix broken configs, and modify existing resources—all in a terminal.

The exam environment defaults to nano (as of 2025), but you can switch to vim. Whether you use vim or nano, you need to be fast. This module covers vim because it’s more powerful once you know the basics.

If you prefer nano, that’s fine—it’s simpler. But vim skills transfer to production troubleshooting where nano might not be available.


The honest truth: Most CKA candidates spend 2 hours learning vim and then use i (insert), Esc, :wq (save and quit), and u (undo) for 90% of their editing. That’s fine. The 10 commands below cover the remaining 10% that saves you minutes per question — indentation fixes, block deletion, and search-replace.

You don’t need to be a vim expert. You need 10 commands.

Vim has modes. This confuses everyone at first.

The Gearshift Analogy

Vim modes are like a car’s gearshift. In Normal mode (drive), you’re navigating—moving around, not typing. In Insert mode (park), you’re stationary and typing. In Command mode (reverse), you’re doing special operations like saving. You wouldn’t try to park while driving. Vim forces you to “shift gears” deliberately. It feels weird at first, but it’s what makes vim so fast once you internalize it.

Pause and predict: If you are typing text and suddenly your backspace stops working or you start deleting lines by accident, what mode are you likely in, and how do you fix it?

ModeHow to EnterWhat It Does
NormalEscNavigate, delete, copy, paste
Inserti, a, oType text
Command:Save, quit, search

Rule: When confused, press Esc to return to Normal mode.

ENTERING INSERT MODE
i Insert before cursor
a Insert after cursor
o Open new line below and insert
O Open new line above and insert
NAVIGATION (Normal mode)
h Left
j Down
k Up
l Right
gg Go to first line
G Go to last line
0 Go to beginning of line
$ Go to end of line
w Jump forward one word
b Jump backward one word
EDITING (Normal mode)
x Delete character under cursor
dd Delete entire line
yy Copy (yank) entire line
p Paste below
P Paste above
u Undo
Ctrl+r Redo
SEARCH
/pattern Search forward
n Next match
N Previous match
SAVE AND QUIT
:w Save (write)
:q Quit
:wq Save and quit
:q! Quit without saving (discard changes)

Honestly? For the exam, you can survive with:

i → Start typing
Esc → Stop typing
:wq → Save and quit
dd → Delete line
u → Undo mistake

That’s 5 things. Master these, and you won’t fail because of vim.


YAML is whitespace-sensitive. A misconfigured vim will ruin your indentation.

Terminal window
cat << 'EOF' > ~/.vimrc
" Basic settings
set number " Show line numbers
set tabstop=2 " Tab = 2 spaces
set shiftwidth=2 " Indent = 2 spaces
set expandtab " Use spaces, not tabs
set autoindent " Maintain indentation
set smartindent " Smart indentation for code
set paste " Prevent auto-indent on paste (toggle with :set nopaste)
" YAML specific
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
" Visual helpers
set cursorline " Highlight current line
syntax on " Syntax highlighting
set hlsearch " Highlight search results
EOF
SettingWhy
tabstop=2Kubernetes YAML uses 2-space indentation
expandtabConverts tabs to spaces (tabs break YAML)
autoindentNew lines maintain indentation
numberLine numbers help with error messages
set pastePrevents auto-indent issues when pasting

Stop and think: Why might setting tabstop=2 not be enough on its own to prevent YAML parsing errors when you press the Tab key?

Gotcha: Tabs vs Spaces

YAML requires consistent indentation. A single tab character mixed with spaces will break your manifest. Always use spaces. The expandtab setting converts tabs to spaces automatically.

War Story: The Invisible Character

A senior engineer spent 45 minutes debugging why kubectl apply kept failing with a cryptic YAML parsing error. The manifest looked perfect. They eventually ran cat -A on the file and discovered invisible tab characters mixed with spaces—introduced when they copied code from a Confluence page. The lesson: never trust copy-paste. Always verify indentation, and configure your editor to show invisible characters or convert tabs automatically.


Terminal window
vim pod.yaml

Press i to enter Insert mode, then type:

apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx

Press Esc, then :wq to save and quit.

You need to add another container. Instead of typing from scratch:

  1. Navigate to the container block
  2. Position cursor on - name: nginx
  3. Press V (visual line mode)
  4. Press j twice to select 3 lines
  5. Press y to yank (copy)
  6. Navigate to where you want the new container
  7. Press p to paste

Selected the wrong indentation? Fix it:

  1. V to enter visual line mode
  2. Select lines with j/k
  3. > to indent right
  4. < to indent left

Or in Normal mode:

  • >> indent current line right
  • << indent current line left

Need to remove a whole section?

  1. Navigate to start of section
  2. Type 5dd to delete 5 lines
  3. Or d} to delete until next blank line

Wrong image name throughout file?

:%s/nginx:1.19/nginx:1.25/g
  • %s = substitute in whole file
  • /old/new/ = pattern
  • g = all occurrences (not just first)

When you copy YAML from documentation and paste into vim, auto-indent can mangle it.

Before pasting:

:set paste

Paste your content (usually Cmd+V or right-click).

After pasting:

:set nopaste

In the exam environment, you might paste directly into the terminal. The :set paste in your .vimrc helps, but be aware of it.

If vim paste is problematic:

Terminal window
cat << 'EOF' > pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
EOF

This avoids vim entirely for creating files.


Print this or memorize it:

╔════════════════════════════════════════════════════╗
║ VIM YAML QUICK REFERENCE ║
╠════════════════════════════════════════════════════╣
║ MODES ║
║ Esc → Normal mode (navigation) ║
║ i → Insert mode (typing) ║
║ : → Command mode (save/quit) ║
╠════════════════════════════════════════════════════╣
║ MOVEMENT ║
║ gg → Top of file ║
║ G → Bottom of file ║
║ 0 → Start of line ║
║ $ → End of line ║
║ /pattern → Search ║
╠════════════════════════════════════════════════════╣
║ EDITING ║
║ dd → Delete line ║
║ yy → Copy line ║
║ p → Paste below ║
║ u → Undo ║
║ >> → Indent right ║
║ << → Indent left ║
╠════════════════════════════════════════════════════╣
║ SAVE/QUIT ║
║ :w → Save ║
║ :q → Quit ║
║ :wq → Save and quit ║
║ :q! → Quit without saving ║
╠════════════════════════════════════════════════════╣
║ YAML SPECIFIC ║
║ :set paste → Before pasting ║
║ :set nopaste → After pasting ║
╚════════════════════════════════════════════════════╝

If vim feels like too much, use nano. It’s the exam default now.

Terminal window
nano pod.yaml

Nano shows shortcuts at the bottom:

  • Ctrl+O = Save (Write Out)
  • Ctrl+X = Exit
  • Ctrl+K = Cut line
  • Ctrl+U = Paste

For YAML, create ~/.nanorc:

Terminal window
cat << 'EOF' > ~/.nanorc
set tabsize 2
set tabstospaces
set autoindent
set linenumbers
EOF

Exam Tip

The exam environment (as of 2025) defaults to nano, but you can use vim if you prefer. Pick one and stick with it—don’t waste exam time debating editors.


  • Vim is on every Linux server. Learning vim pays off beyond the exam—you’ll use it for production troubleshooting, container debugging, and anywhere a GUI isn’t available.

  • The creator of vim (Bram Moolenaar) passed away in 2023. The project continues as an open-source community effort. Neovim is a popular modern fork.

  • vimtutor is built in. Run vimtutor in any terminal for an interactive vim tutorial. Takes about 30 minutes and teaches you more than this module.


MistakeProblemSolution
Stuck in insert modeCan’t navigatePress Esc
Pasted YAML is mangledAuto-indent:set paste before pasting
Tab characters in YAMLYAML syntax errorUse expandtab in .vimrc
Lost changesQuit without savingUse :wq not :q!
Wrong indentationYAML parsing fails>> and << to fix

  1. Scenario: You are in the middle of the CKA exam and need to quickly remove a broken volume mount block that spans exactly three lines from your deployment manifest. How do you achieve this efficiently in vim without repeatedly pressing the delete key?

    Answer You should position your cursor on the first line of the volume mount block, ensure you are in Normal mode (press `Esc`), and type `3dd`.

    Why: The d command in vim stands for delete, and typing it twice (dd) deletes the current line. By prefixing it with a number like 3, you instruct vim to repeat the action that many times, instantly removing the block. This is significantly faster than manually deleting characters or lines, saving precious seconds during the time-constrained exam.

  2. Scenario: You just found a perfect snippet of YAML in the Kubernetes documentation. You copy it and paste it directly into your terminal running vim, but the indentation instantly becomes a cascading mess of misaligned blocks. What caused this formatting disaster, and how do you correctly paste the snippet next time?

    Answer Vim's auto-indentation feature interpreted your pasted spaces as new indentation levels for each line, mangling the format. To prevent this, you must type `:set paste` before pasting your clipboard contents, and `:set nopaste` afterward.

    Why: The paste mode temporarily disables auto-indentation and other formatting automation. This ensures that vim accepts the characters exactly as they exist in your clipboard without trying to “helpfully” adjust the spacing, keeping the YAML structure intact. If you forget this step, vim tries to apply its own indentation rules on top of the already-indented text.

  3. Scenario: You have just finished adding a missing environment variable to a critical manifest. You need to apply these changes immediately, but you are currently in Insert mode typing the last quote. What exact sequence of keystrokes do you use to save your changes and exit the file?

    Answer You must first press `Esc`, followed by typing `:wq` and pressing `Enter`.

    Why: Vim requires you to exit Insert mode to execute file-level commands, which is why Esc is necessary to return to Normal mode. The colon : enters Command mode, w writes (saves) the file to disk, and q quits the editor. Combining them as :wq safely persists your critical changes and returns you to the terminal in one fluid motion, ready to run kubectl apply.

  4. Scenario: You create a quick Pod manifest and visually align all the fields perfectly. However, when you run kubectl apply, you get a cryptic error parsing your YAML. You realize you used the Tab key to align some fields. Why did this happen, and how should you configure vim to prevent it?

    Answer The Kubernetes YAML parser strictly expects spaces for indentation and fundamentally rejects or misinterprets tab characters, breaking the document structure. You should add `set expandtab` to your `~/.vimrc` file.

    Why: YAML relies on exact whitespace character counts to determine the hierarchy of objects, and a tab character does not equal a consistent number of spaces across different systems. The expandtab setting fixes this automatically by converting any Tab key press into the correct number of spaces (defined by tabstop), ensuring your manifests always parse correctly regardless of how you type. This completely eliminates a common source of invisible, time-wasting syntax errors.


Task: Configure vim and practice YAML editing.

Setup:

Terminal window
# Create .vimrc with YAML settings
cat << 'EOF' > ~/.vimrc
set number
set tabstop=2
set shiftwidth=2
set expandtab
set autoindent
syntax on
EOF

Practice Tasks:

  1. Create a pod manifest from scratch:

    Terminal window
    vim practice-pod.yaml
    # Type a complete Pod manifest
    # Save and exit
  2. Duplicate a container block:

    • Open the file
    • Copy the container section
    • Paste and modify the name
  3. Fix intentionally broken indentation:

    Terminal window
    cat << 'EOF' > broken.yaml
    apiVersion: v1
    kind: Pod
    metadata:
    name: test
    spec:
    containers:
    - name: nginx
    image: nginx
    EOF
    # Open in vim and fix indentation

Success Criteria:

  • Can create a valid Pod manifest in vim
  • Can copy and paste blocks within vim
  • Can fix indentation issues
  • Know how to save and quit

Verification:

Terminal window
# Validate your YAML
kubectl apply -f practice-pod.yaml --dry-run=client

Drill 1: Vim Speed Test (Target: 2 minutes)

Section titled “Drill 1: Vim Speed Test (Target: 2 minutes)”

Create this pod manifest from scratch in vim:

Terminal window
vim speed-test.yaml

Type this (don’t copy-paste):

apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80

Save and validate:

Terminal window
kubectl apply -f speed-test.yaml --dry-run=client
rm speed-test.yaml

Drill 2: Fix Broken YAML - Indentation (Target: 3 minutes)

Section titled “Drill 2: Fix Broken YAML - Indentation (Target: 3 minutes)”
Terminal window
# Create broken YAML
cat << 'EOF' > broken-indent.yaml
apiVersion: v1
kind: Pod
metadata:
name: broken-pod
labels:
app: test
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
EOF
# Open in vim and fix ALL indentation errors
vim broken-indent.yaml
# Validate your fix
kubectl apply -f broken-indent.yaml --dry-run=client
rm broken-indent.yaml
Fixed Version
apiVersion: v1
kind: Pod
metadata:
name: broken-pod
labels:
app: test
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

Drill 3: Fix Broken YAML - Mixed Tabs/Spaces (Target: 3 minutes)

Section titled “Drill 3: Fix Broken YAML - Mixed Tabs/Spaces (Target: 3 minutes)”
Terminal window
# Create YAML with hidden tab characters
printf 'apiVersion: v1\nkind: Pod\nmetadata:\n\tname: tab-pod\nspec:\n\tcontainers:\n\t- name: nginx\n\t image: nginx\n' > broken-tabs.yaml
# Look at it - seems fine visually
cat broken-tabs.yaml
# But kubectl fails!
kubectl apply -f broken-tabs.yaml --dry-run=client
# YOUR TASK: Open in vim and fix
vim broken-tabs.yaml
# Hint: In vim, use :%s/\t/ /g to replace tabs with spaces
kubectl apply -f broken-tabs.yaml --dry-run=client
rm broken-tabs.yaml

Drill 4: Copy and Modify Blocks (Target: 2 minutes)

Section titled “Drill 4: Copy and Modify Blocks (Target: 2 minutes)”
Terminal window
# Create a deployment with one container
cat << 'EOF' > multi-container.yaml
apiVersion: v1
kind: Pod
metadata:
name: multi
spec:
containers:
- name: app
image: nginx
ports:
- containerPort: 80
EOF
# YOUR TASK in vim:
# 1. Duplicate the container block
# 2. Change second container to: name: sidecar, image: busybox, remove ports
# Target: 2 minutes
vim multi-container.yaml
# Validate
kubectl apply -f multi-container.yaml --dry-run=client
rm multi-container.yaml
Expected Result
apiVersion: v1
kind: Pod
metadata:
name: multi
spec:
containers:
- name: app
image: nginx
ports:
- containerPort: 80
- name: sidecar
image: busybox

Drill 5: Search and Replace (Target: 1 minute)

Section titled “Drill 5: Search and Replace (Target: 1 minute)”
Terminal window
# Create file with wrong image version
cat << 'EOF' > version-fix.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.19
- name: log
image: fluentd:1.19
- name: cache
image: redis:1.19
EOF
# YOUR TASK: Change ALL "1.19" to "1.25" using vim search/replace
# Command: :%s/1.19/1.25/g
vim version-fix.yaml
# Verify all changed
grep "1.25" version-fix.yaml # Should show 3 lines
rm version-fix.yaml

Drill 6: Fix Broken YAML - Syntax Errors (Target: 5 minutes)

Section titled “Drill 6: Fix Broken YAML - Syntax Errors (Target: 5 minutes)”

This YAML has multiple errors. Find and fix all of them:

Terminal window
cat << 'EOF' > broken-syntax.yaml
apiVersion: v1
kind: Pod
metadata:
name: syntax-errors
labels:
app: test
environment: production # missing quotes on value with special chars
spec:
containers:
- name: app
image: nginx
env:
- name: DATABASE_URL
value: postgres://user:p@ssword@db:5432 # @ needs quoting
- name: DEBUG
value: true # boolean should be string
ports:
- containerPort: "80" # should be integer, not string
resources:
requests:
memory: 128Mi # missing quotes won't break, but...
cpu: 100 # should be 100m
EOF
vim broken-syntax.yaml
# Test
kubectl apply -f broken-syntax.yaml --dry-run=client
rm broken-syntax.yaml
Fixed Version
apiVersion: v1
kind: Pod
metadata:
name: syntax-errors
labels:
app: test
environment: production
spec:
containers:
- name: app
image: nginx
env:
- name: DATABASE_URL
value: "postgres://user:p@ssword@db:5432"
- name: DEBUG
value: "true"
ports:
- containerPort: 80
resources:
requests:
memory: "128Mi"
cpu: "100m"

If you prefer nano, do Drill 1 using nano instead:

Terminal window
nano speed-test.yaml
# Ctrl+O to save, Ctrl+X to exit
kubectl apply -f speed-test.yaml --dry-run=client
rm speed-test.yaml

Compare your time with vim. Use whichever is faster for you.


Module 0.4: kubernetes.io Navigation - Finding documentation fast during the exam.