~3 min read|
A place to store vim commands that aren’t muscle memory yet.
Doing a little consolidation as I’ve previously had to learn to undo and redo in vim.
u is Undo
. will repeat the last command (e.g. if you just pasted in a word and then use
., it will paste it again)
ctrl-r is Redo - the inverse of the
Imagine you yanked some text. Now, you want to replace other text with that. How might you do it?
One way is to learn the commands and just use those.
For example, if you want to replace text with what you’ve last yanked, you can do
Or, if you want to do last yanked or deleted text:
These are variations of a technique referred to as “stamping”.
This is nice to know, but wouldn’t it be nice if we remapped our bindings so that it was more ergonomic?
" Replace the visually selected text with previously yanked text
vnoremap p pgvy
There are also other strategies, laid out here.
ctrl-v - enter visual block mode
4j - highlight the same position in the next four rows
Esc to insert before the cursor or
Esc to insert after the cursor. In this case, I’m not replacing text, but inserting. In theory, you could also use
c2w to replace the first two words.
Note An important thing to know for first time users of the visual block editing is that when in Insert mode to make the change, vim will only render the changes on the first line. The changes will be applied to all lines in the visual block once Insert mode has been escaped (
visual-block mode and
v to enter
visual-block mode, type
: to enter a command - you should see
:'<,'>. It is automatically added by Vim and means “act on the selected area”
norm[al] enables executing normal mode commands on a range of lines
norm ^w2 i"<C-v
^ puts the cursor on the first char of the line.
w moves to the next word.
i" inserts a
" before the cursor.
<Esc is Vim’s way to input a control character in this context, here it’s
<Esc> used to exit insert mode.
ee moves to the end of the next word.
a" appends a ” after the cursor.
<CR> executes the command.
Cutting is done with the letter
d in NORMAL mode.
Copying (yanking) is done with
Pasting is done with
P which pastes before the cursor).
There is a lot of navigation involved in vim since that’s one of the ways that you can move so quickly around a file.
Two that I always struggle to remember are:
gg will take you to the top of the file,
shift+g will take you to the last line of the file.
shift+g is also how you navigate to a specific line number by preceding the command with the desired row. E.g., If you want to go to the 11th line in the file, the command would be
ESC 11 Shift+G.
Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!