My vim related articles
- Mac Vim Usage – Basics
- Mac Vim Usage – Advanced
- Mac Vim Usage – Customization
- Install vim on macOS High Sierra using source code
- System Version: macOS 10.13 (17A405)
- Kernel Version: Darwin 17.0.0
For how to install vim on Mac, you can refer to Install vim on macOS High Sierra using source code.
Check which vim is being used
~/Documents$ which vim /usr/bin/vim
Check vim version
~/Documents$ vim --version VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jul 26 2017 19:10:24) Included patches: 1-503, 505-642 Compiled by firstname.lastname@example.org
i Enter insert mode (before current cursor)
I Enter insert mode (from line beginning)
a Enter insert mode (append after cursor)
A Enter insert mode (append from line end)
x Delete character before cursor
X Delete character after cursor
dw Delete word
diw Delete current word
dd Delete line
J Joint line.
Save and quit
ESC to exit from insert mode
:q! Quit without saving
:w Save only
:wq Save and quit
w Move cursor forward by a word
b Move cursor backward by a word
0 Move cursor to line beginning
$ Move cursor to line end
gg Move cursor to file start
G Move cursor to file end
Copy and pase
To cut-and-paste or copy-and-paste inside Vim
- Position the cursor at the beginning of the text you want to cut/copy.
- Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block.
- Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement.
- Press d (delete) to cut, or y (yank) to copy.
- Move the cursor to the desired paste location.
- Press p to paste after the cursor, or P to paste before.
Visual selection (steps 1-3) can be performed using a mouse.
If you want to change the selected text, press c instead of d or y in step 4. In a visual selection, pressing c performs a change by deleting the selected text and entering insert mode so you can type the new text.
Some quick commands
yiw Copy a word
yy Copy a line
p Paste before current cursor position.
P Paste after current cursor position.
Undo and redo
u Undo last operation
Ctrl+r Redo the last undone operation.
Search and replace
E.g. we are going to search a keyword “arophix”
In normal mode, type
/ to enter your text to be searched. Type
<CR> to perform a search.
/arophix search keyword “arophix”
n search next occurrence of keyword “arophix”
N search previous occurrence of keyword “arophix”
:set ignorecase set search mode to be case insensitive
:set noignorecase set search mode to be case sensitive
E.g. we are going to replace “arophix” with “Arophix”
Firstly, set search mode be case sensitive.
Secondly, use the
:substitute command. In short, substitute command can be
:%s/arophix/Arophix/gc search keyword “arophix” in all lines (
%) and replace with “Arophix”, but ask for confirmation(
Open a directory
E.g. open /User/<myusername>/Download
Go back to previous editor
Ctrl + ^
Open current directory of the opening file
Vim help command