My personal summary of vimtutor with the most useful commands.
- Open a file from the shell prompt
vim filename
- Save changes made to the text
:w filename
esq
to switch to command mode- Exit the editor and discard any changes
:q!
- Save file and exit
:wq
- Save file and exit in normal mode
zz
- Save file and exit in normal mode
- Moving the cursor:
h
(left),j
(down),k
(up),l
(right) - Move the curser a word forward
w
- Move the curser to the end of a word
e
- Move to the start of a line
0
- Move to the end of a line
$
- Combine commands: move to the end of the next line
j$
- Combine commands: move to the end of the next line
- Move to the bottom of the file
G
- Move to the start of the file
gg
- Move to a specific line by typing
number + G
ctrl + g
shows the location in the file
- New horizontal window
:split filename
or:sp filename
- New vertical window
:vsplit filename
or:vs filename
- Navigate to the below/right window
ctrl+w
followed byw
- Navigate to the above/left window
ctrl+w
followed byW
- Switch towards the respective direction
ctrl+w
followed byhjkl
ort
for top/first orb
bottom/last window
- Open another file in a separate buffer
:e filename
- Jump to the next buffer
:bn
- Jump to the previous buffer
:bp
- Jump to the first buffer
:bf
- Jump to the last buffer
:bl
- Show all open buffers
buffers
- Delete single character: move curser to character
x
- Delete a word: move curser to word
dw
- Delete from the current character to the end of the word
de
- Delete the end of a line
d$
- Delete a whole line
dd
- Join current line and the next line (remove newline)
J
- Switch to insert mode (before the cursor)
i
- Append: switch to insert mode after the cursor
a
- Append: switch to insert mode at the end of a line
A
(it does not matter at which position the cursor is) - Open up a line below the cursor and switch to insert mode
o
- Open up a line above the cursor and switch to insert mode
O
- Replace a character at the curser
r
- Switch to replace mode
R
(every typed character deletes an existing character) - Change until the end of a word
ce
- Delete the whole line and switch to insert mode
cc
- Yank (copy) one word
yw
- Yank a whole line
yy
- Put a previously copied or deleted text after the curser
p
- Undo the last command
u
- Undo all changes to a line
U
- Redo (undo the undo's)
ctrl + r
- Search with
/
followed by the phrase to search for- Jump to the next occursion of the phrase
n
- Jump to the previous occursion of the phrase
N
- Jump to the next occursion of the phrase
- Search in backward direction
?
- Find a matching parenthesis by placing the curser on any (, [, or { and type
%
- Substitute new for the first old in the current line
:s/old/new
- Substitute all 'old's on a line
:s/old/new/g
- Substitute phrases between two line #'s type
:#,#s/old/new/g
- Substitute all occurrences in the file type
:%s/old/new/g
- To ask for confirmation each time add c
:%s/old/new/gc
- Set option "xxx"
:set xxx
- Ignore upper/lower case
:set ic
or:set ignorecase
- Show partial matches for a search phrase
:set is
or:set incsearch
- Highlight all matching phrases
:set hls
or:set hlsearch
- Ignore upper/lower case
- Switch an option off
:set noic
, etc.
- Select text
v
- Yank (copy) the highlighted text
y
- Delete selection
:d
- Write selected text to file
:w filename
- Yank (copy) the highlighted text
- Indent current line or selection
>>
- Unindent current line or selection
<<
- Put a disk file below cursor position
:r filename
- Combine with external commands, e.g.
:r !ls
- Execute an external command with
:!
followed by the command
- Help for a command
:help cmd
- Press
ctrl + d
to see possible completions andtab
to use one completion
operator [number] motion
operator
is what to do, such as d for delete[number]
is an optional count to repeat the motionmotion
moves over the text to operate onw
(word)$
(end of line)
For a user specific configuration of Vim create a .vimrc file in the HOME directory of the user. You can create a .vimrc file with:
touch ~/.vimrc
Open the .vimrc file with:
vim ~/.vimrc
To activate changes to the .vimrc file you have to reload the .vimrc file to make the changes take place:
:source ~/.vimrc
I am using vim-plug a minimalist Vim plugin manager (Github).
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
- Add the plugins to the
.vimrc
file.
" List of plugins managed by vim-plug (will be downloaded to specified dir)
call plug#begin(has('nvim') ? stdpath('data') . '/plugged' : '~/.vim/plugged')
Plug 'vim-airline/vim-airline'
Plug 'preservim/nerdtree'
Plug 'sonph/onehalf', { 'rtp': 'vim' }
call plug#end()
- Reload your Vim configuration file (
:source ~/.vimrc
) or restart Vim, and run:PlugInstall
to install the plugins.
Remove the line from the .vimrc
file. Quit and restart Vim, and run :PlugClean
. Confirm the prompt to delete the directory with type Y
.
Ctags is a programming tool that generates an index (or tag) file of names found in source and header files. The vim plugin tagbar requires ctags. With ctags it is possible to jump to functions with ctrl + ]
.
Install with homebrew:
brew install ctags
MacOS has a ctags version installed. Afterwards, set the alias to the new version:
alias ctags="`brew --prefix`/bin/ctags"
alias ctags >> ~/.zshrc
The second line will add ctags=/usr/local/bin/ctags
to the .zshrc file.
Add set tags=tags
to the .vimrc
file:
set tags=tags
Neovim is a newer version of vim with some advantages. It can be installed with:
brew install neovim
To start neovim instead of vim we add echo "alias vim='nvim'" >> ~/.zshrc
to the Z shell .rc file and reload the file with source ~/.zshrc
.
In contrast to ~/.vimrc
, neovim organizes configuration under ~/.config/nvim/init.vim