TT Lab
Get started
Learn Learning paths Courses

vim and tmux

vim Is a Grammar, Not a List of Commands

Continue in TT Lab

In one line

vim's keys are not a list to memorize but a grammar you combine. Once you know the single formula {횟수}{동작}{범위}, you can build combinations you were never taught.

Why this was needed

Many people remember vim as "the editor you don't know how to quit". Yet a vi-family editor is already installed on almost every place without a GUI: a server you reached over SSH, the inside of a docker container, an embedded device. If you keep downloading a file to your machine and uploading it again just to change one line of a configuration file, the problem is not the tool. You just don't know the grammar.

vim has survived since it grew out of vi in 1976 for one reason: separating modes let every single letter key become a command. While other editors crammed commands into Ctrl combinations, vim acquired a vocabulary of one-letter words such as d, y, c, w and $, and as those words combined, its expressive power grew multiplicatively.

How it works

The core formula is just this one.

{횟수}{operator}{motion 또는 text object}

Read it as parts of speech and it has a verb + (preposition) + noun structure.

Combination How to read it What it does
3dd 3 times · delete · line Delete three lines
d3w delete · 3 words Delete three words
ci" change · inner · quote Replace only what is inside the quotes
da( delete · around · parenthesis Delete everything, parentheses included
yi{ yank · inner · brace Copy the contents of a block

Here i (inner) and a (around) play the role of prepositions. Once you know these two letters, tasks such as "replace only the string inside the quotes", "delete all the arguments of a function" or "select the contents of a tag" take just two keystrokes.

One important property: in vim, deleting is cutting. A line removed with dd goes into a register and can be pasted with p. That is where idioms such as ddp (swap with the line below) and yyp (duplicate a line) come from.

You only need to know four modes to get started: Normal (the default, for commands), Insert (for typing), Visual (for selecting) and Command-line (ex commands that start with :). When you lose your bearings, it is always Esc. For a beginner this is survival rule number 1.

What you see in the field

Changing one value in a configuration file. In /etc/nginx/nginx.conf, say you need to change the worker_connections value. Jump there with /worker_conn, replace just the word with ciw, and save with :wq. No mouse and no scrolling needed.

Cleaning up YAML with broken indentation. gg=G re-indents the whole file according to its syntax. It is safer than counting spaces one character at a time.

A configuration file only matters if vim actually reads it. If you write an option in .vimrc and make a typo, only that line is silently ignored. If you ask with a question mark, as in set number?, vim tells you the value currently in effect. That is why the first step of the lab checks this.

Don't forget .. It repeats your last edit exactly. After ciw새단어<Esc>, move to the next spot and a single . repeats the same edit. It is a tool to try before you reach for a macro.

The bare minimum to survive on an unfamiliar server

The vim on your own laptop is configured, but the vim on a server you log in to may have no configuration at all, or may be just vi. So if you learn the things that work without any configuration, you can work anywhere.

First, check what you are opening. :f tells you the name and state of the current file. And if you can open it read-only, do. Opening with view 파일 or vim -R 파일 (the file to open) means you can never edit and save by accident. Most of the time on a production server you only want to check a configuration anyway.

Make undo something you can trust. u undoes and Ctrl-r redoes. If you have undone several times and lost your bearings, :e! discards every change since the last save and rereads the file. Knowing that you can always return to the original file before you save lets you edit with peace of mind.

Don't panic when a save fails. Having a save rejected for lack of permission is common. You don't need to throw away your edits and reopen the file. You can save under another name and move it, or obtain permission and save in place. The worst thing is to leave with :q! and start over from scratch.

The habit of keeping the original is especially valuable on an unfamiliar server. If you make a dated copy before editing a configuration file, undoing a mistake takes a few seconds. vim itself has a setting that creates backup files, but on someone else's server you cannot expect that setting to be on, so doing it by hand is more reliable.

Finally, vim warns you when several people open the same file. That is the warning that a swap file exists: either someone else is editing right now, or an earlier editing session ended abnormally. If you habitually dismiss this warning you will overwrite someone else's work, so the right move is to first check who has it open.

What you will do in the next lab

You create a .vimrc, confirm that vim actually applies its settings, and then leave the results of basic edits such as deleting, duplicating and swapping lines in result files. In the last two steps you save the editing commands themselves to a file so the grader can apply them to the original again. Only when the procedure is reproducible, not just the result, have you truly learned it.