Some frequently used commands in Vim
File explorer
:Explore - opens the file explorer window.
:E - the same
Visual commands
> - shift right
< - shift left
y - yank (copy) marked text
d - delete marked text
~ - switch case
Cut and Paste
yy - yank (copy) a line
2yy - yank 2 lines
yw - yank word
y$ - yank to end of line
p - put (paste) the clipboard after cursor
P - put (paste) before cursor
dd - delete (cut) a line
dw - delete (cut) the current word
x - delete (cut) current character
Search/Replace
/pattern - search for pattern
?pattern - search backward for pattern
n - repeat search in same direction
N - repeat search in opposite direction
:%s/old/new/g - replace all old with new throughout file
:%s/old/new/gc - replace all old with new throughout file with confirmations
Identation settings
Create the file
vim ~/.vim/vimrc
Add the configuration:
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
Restart vim.
You can replace all the tabs with spaces in the entire file with
:%retab
Working with multiple files
:e filename - Edit a file in a new buffer
:bnext (or :bn) - go to next buffer
:bprev (of :bp) - go to previous buffer
:bd - delete a buffer (close a file)
:sp filename - Open a file in a new buffer and split window
ctrl+ws - Split windows
ctrl+ww - switch between windows
ctrl+wq - Quit a window
ctrl+wv - Split windows vertically
ctrl+wj - switch wo the bottom window
ctrl+wk - switch wo the top window
Paste without reformatting
Vim provides the ‘paste’ option to aid in pasting text unmodified from other applications. You can set it manually like:
:set paste
paste, then
:set nopaste
Upper/lower case transformation
~
- Toggle case of the character under the cursor, or all visually-selected characters.
3~
Toggle case of the next three characters.
g~3w
Toggle case of the next three words.
g~iw
Toggle case of the current word (inner word – cursor anywhere in word).
g~$
Toggle case of all characters to end of line.
g~~
Toggle case of the current line (same as V~).
The above uses ~
to toggle case. In each example, you can replace ~
with u
to convert to lowercase, or with U
to convert to uppercase. For example:
U
Uppercase the visually-selected text.
First press v
or V
then move to select text.
If you don’t select text, pressing U
will undo all changes to the current line.
gUU
Change the current line to uppercase (same as VU).
gUiw
Change current word to uppercase.
u
Lowercase the visually-selected text.
If you don’t select text, pressing u will undo the last change.
guu
Change the current line to lowercase (same as Vu).
Other versions:
My .vimrc (or ~/.vim/vimrc)
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab