vim is a command-line based text editor.
It is very useful for editing code and config files when you only have access to a command-line, e.g. when your are ssh’ed into a remote machine.
Select, Cut, Copy And Paste
i and then a delimiter such as
[ can be added after yank, delete, replace.
The following table shows some examples:
|Yank (copy) text inside braces|
|Yank text inside and including braces|
|Delete inside brackets.|
|Replace inside square brackets.|
Find And Replace
To find every occurrence of
foo and replace it with
bar we can use the substitute command (
% at the start forces vim to check all lines, not just the current one. The
g at the end stands for global, which force vim to replace all occurrences, not just the first.
The search pattern (which above is
foo) supports regex.
You can add a
c at the end to ask for confirmation on each replace:
$ have special functions. Place a backslash before each to instead search for that actual character (e.g.
\* will search for a
Advanced Find And Replace Using Backreferences
vim supports advanced find/replace operations which involve manipulating the input into the output using regex back references.
Jump To Matching Bracket, Brace, Parenthesis
When in normal mode, make sure the cursor is over one of the brackets, and press the
yy copies the current line, press
p to paste it below the current line, or
P to paste it above the current line.
dd deletes the current line. You can press
p to paste it.
Using Internal Registers
When performing copy/paste operations in the above manner, vim will not use the system clipboard, but rather it’s own internal clipboard. When no register is specified (e.g. the above example), vim will copy and paste using the unnamed register.
Copy/Paste To System Clipboard
Copying/pasting to the system clipboard can be a little tricky.
As long as your vim executable supports it, you can copy to the clipbaord using the
* register. Select text using visual mode (e.g.
l to select three characters), and then press:
You can find out if your vim executable supports copying to the clipboard with the command
vim --version (it prints out a large amount of info, so we use
grep to filter the output):
$ vim --version | grep clipboard -clipboard +iconv +path_extra -toolbar +eval +mouse_dec +startuptime -xterm_clipboard
-clipboard indicates that this feature was not includes. What you want to see is
vim has two different sets of movement keys. The first is the standard
← → ↑ ↓, arrow keys. The second is the more harder to learn
h (left) j (down) k (up) l (right) method.
The idea behind
h j k l is that they are closer to all the other vim commands, so you don’t have to move you hand/finger position to use them, resulting in more faster use. The downside is that they are not as intuitive to use, and do not work in insert mode (in insert mode they behave as the regular characters).
When writing code, a common need is to shift text left and right by a fixed indentation amount, either to make it readable in indentation agnostic languages such as C, or just to make it work in indentation sensitive languages such as Python!
vim allows you to use the
> keys to shift selected text left or right by the shiftwidth.
Unfortunately, the selection is deselected as soon as you press the
> keys. While you can still repeat the indentation with
., you cannot reverse the indentation with the opposite chevron key without re-selecting the text (of course, undo will still work). A shortcut for re-selecting the lines is
When in normal mode, you can press
<< to shift the current line.
When in insert mode, you can use
Ctrl-T to indent right and
Ctrl-D to indent left (unindent).
- January 2017 Updates
- Parsing Command-Line Arguments In Python
- December 2018 Updates
- January 2018 Updates
- August 2017 Updates