2016-04-28 10:06:35 +03:00
---
category: tool
tool: vim
contributors:
- ["RadhikaG", "https://github.com/RadhikaG"]
2022-10-09 17:47:49 +03:00
- ["kaymmm", "https://github.com/kaymmm"]
2016-04-28 10:06:35 +03:00
filename: LearnVim.txt
---
2017-06-01 12:31:14 +03:00
[Vim ](http://www.vim.org )
2017-10-27 22:50:49 +03:00
(Vi IMproved) is a clone of the popular vi editor for Unix. It is a text
editor designed for speed and increased productivity, and is ubiquitous in most
unix-based systems. It has numerous keybindings for speedy navigation to
2016-04-28 10:06:35 +03:00
specific points in the file, and for fast editing.
2020-06-12 08:28:50 +03:00
`vimtutor` is a an excellent application that teaches you how to use `Vim` . It comes with the vim package during installation. You should be able to just run "vimtutor" on the command line to open this tutor. It will guide you through all the major features in `vim` .
2016-04-28 10:06:35 +03:00
## Basics of navigating Vim
```
2022-10-09 17:44:11 +03:00
vim < filename > # Open < filename > in vim
:help < topic > # Open up built-in help docs about < topic > if any exists
:q # Quit vim
:w # Save current file
:wq # Save file and quit vim
ZZ # Save file and quit vim
:q! # Quit vim without saving file
2022-12-10 18:05:34 +03:00
# ! *forces* :q to execute, hence quitting vim without saving
2022-10-09 17:44:11 +03:00
ZQ # Quit vim without saving file
2022-11-10 06:37:19 +03:00
:x # Save file(only when the file is modified) and quit vim
2022-10-09 17:44:11 +03:00
u # Undo
CTRL+R # Redo
h # Move left one character
j # Move down one line
k # Move up one line
l # Move right one character
Ctrl+B # Move back one full screen
Ctrl+F # Move forward one full screen
Ctrl+D # Move forward 1/2 a screen
Ctrl+U # Move back 1/2 a screen
2018-09-27 19:58:55 +03:00
2016-04-28 10:06:35 +03:00
# Moving within the line
2022-10-09 17:44:11 +03:00
0 # Move to beginning of line
$ # Move to end of line
^ # Move to first non-blank character in line
2016-04-28 10:06:35 +03:00
# Searching in the text
2022-10-09 17:44:11 +03:00
/word # Highlights all occurrences of word after cursor
?word # Highlights all occurrences of word before cursor
n # Moves cursor to next occurrence of word after search
N # Moves cursor to previous occurrence of word
2016-04-28 10:06:35 +03:00
2022-10-09 17:44:11 +03:00
:%s/foo/bar/g # Change 'foo' to 'bar' on every line in the file
:s/foo/bar/g # Change 'foo' to 'bar' on the current line
:%s/\n/\r/g # Replace new line characters with new line characters
:'< ,'>s/foo/bar/g # Change 'foo' to 'bar on every line in the current visual selection
2016-04-28 10:06:35 +03:00
# Jumping to characters
2022-10-09 17:44:11 +03:00
f< character > # Jump forward and land on < character >
t< character > # Jump forward and land right before < character >
2016-04-28 10:06:35 +03:00
2017-10-27 22:50:49 +03:00
# For example,
2022-10-09 17:44:11 +03:00
f< # Jump forward and land on <
t< # Jump forward and land right before <
2017-10-27 22:50:49 +03:00
2016-04-28 10:06:35 +03:00
# Moving by word
2022-10-09 17:44:11 +03:00
w # Move forward by one word
b # Move back by one word
e # Move to end of current word
2016-04-28 10:06:35 +03:00
# Other characters for moving around
2022-10-09 17:44:11 +03:00
gg # Go to the top of the file
G # Go to the bottom of the file
:NUM # Go to line number NUM (NUM is any number)
H # Move to the top of the screen
M # Move to the middle of the screen
L # Move to the bottom of the screen
2016-04-28 10:06:35 +03:00
```
2017-10-27 22:51:05 +03:00
## Help docs:
Vim has built in help documentation that can accessed with `:help <topic>` .
For example `:help navigation` will pull up documentation about how to navigate
your workspace!
`:help` can also be used without an option. This will bring up a default help dialog
that aims to make getting started with vim more approachable!
2016-04-28 10:06:35 +03:00
## Modes:
Vim is based on the concept on **modes** .
2018-12-21 23:48:10 +03:00
- Command Mode - vim starts up in this mode, used to navigate and write commands
- Insert Mode - used to make changes in your file
- Visual Mode - used to highlight text and do operations to them
- Ex Mode - used to drop down to the bottom with the ':' prompt to enter commands
2016-04-28 10:06:35 +03:00
```
2022-10-09 17:44:11 +03:00
i # Puts vim into insert mode, before the cursor position
a # Puts vim into insert mode, after the cursor position
v # Puts vim into visual mode
: # Puts vim into ex mode
< esc > # 'Escapes' from whichever mode you're in, into Command mode
2016-04-28 10:06:35 +03:00
# Copying and pasting text
2022-10-09 17:44:11 +03:00
# Operations use the vim register by default
# Think of it as vim's private clipboard
2016-04-28 10:06:35 +03:00
2022-10-09 17:44:11 +03:00
# Yank ~ copy text into vim register
y # Yank whatever is selected
yy # Yank the current line
2022-01-03 19:00:16 +03:00
2022-10-09 17:44:11 +03:00
# Delete ~ yank text and delete from file
d # Delete whatever is selected
dd # Delete the current line
2022-01-03 19:00:16 +03:00
2022-10-09 17:44:11 +03:00
p # Paste text in vim register after the current cursor position
P # Paste text in vim register before the current cursor position
2022-01-03 19:00:16 +03:00
2022-10-09 17:44:11 +03:00
x # Delete character under current cursor position
2016-04-28 10:06:35 +03:00
```
## The 'Grammar' of vim
2017-10-27 22:50:49 +03:00
Vim can be thought of as a set of commands in a
2016-04-28 10:06:35 +03:00
'Verb-Modifier-Noun' format, where:
2018-12-21 23:48:10 +03:00
- Verb - your action
- Modifier - how you're doing your action
- Noun - the object on which your action acts on
2016-04-28 10:06:35 +03:00
2016-08-26 11:05:48 +03:00
A few important examples of 'Verbs', 'Modifiers', and 'Nouns':
2016-04-28 10:06:35 +03:00
```
# 'Verbs'
2017-10-27 22:50:49 +03:00
2022-10-09 17:44:11 +03:00
d # Delete
c # Change
y # Yank (copy)
v # Visually select
2016-04-28 10:06:35 +03:00
# 'Modifiers'
2022-10-09 17:44:11 +03:00
i # Inside
a # Around
NUM # Number (NUM is any number)
f # Searches for something and lands on it
t # Searches for something and stops before it
/ # Finds a string from cursor onwards
? # Finds a string before cursor
2016-04-28 10:06:35 +03:00
# 'Nouns'
2022-10-09 17:44:11 +03:00
w # Word
s # Sentence
p # Paragraph
b # Block
2017-10-27 22:50:49 +03:00
2016-04-28 10:06:35 +03:00
# Sample 'sentences' or commands
2022-10-09 17:44:11 +03:00
d2w # Delete 2 words
cis # Change inside sentence
yip # Yank inside paragraph (copy the para you're in)
ct< # Change to open bracket
# Change the text from where you are to the next open bracket
d$ # Delete till end of line
2016-04-28 10:06:35 +03:00
```
## Some shortcuts and tricks
<!-- TODO: Add more! -->
```
2022-10-09 17:44:11 +03:00
> # Indent selection by one block
< # Dedent selection by one block
:earlier 15m # Reverts the document back to how it was 15 minutes ago
:later 15m # Reverse above command
ddp # Swap position of consecutive lines, dd then p
. # Repeat previous action
:w !sudo tee % # Save the current file as root
:set syntax=c # Set syntax highlighting to 'c'
:sort # Sort all lines
:sort! # Sort all lines in reverse
:sort u # Sort all lines and remove duplicates
~ # Toggle letter case of selected text
u # Selected text to lower case
U # Selected text to upper case
J # Join the current line with the next line
2017-11-14 21:14:17 +03:00
# Fold text
2022-10-09 17:44:11 +03:00
zf # Create fold from selected text
zd # Delete fold on the current line
zD # Recursively delete nested or visually selected folds
zE # Eliminate all folds in the window
zo # Open current fold
zO # Recursively open nested or visually selected folds
zc # Close current fold
zC # Recursively close nested or visually selected folds
zR # Open all folds
zM # Close all folds
za # Toggle open/close current fold
zA # Recursively toggle open/close nested fold
[z # Move to the start of the current fold
]z # Move to the end of the current fold
zj # Move to the start of the next fold
zk # Move to the end of the previous fold
2016-04-28 10:06:35 +03:00
```
## Macros
Macros are basically recordable actions.
When you start recording a macro, it records **every** action and command
you use, until you stop recording. On invoking a macro, it applies the exact
same sequence of actions and commands again on the text selection.
```
2022-10-09 17:44:11 +03:00
qa # Start recording a macro named 'a'
q # Stop recording
@a # Play back the macro
2016-04-28 10:06:35 +03:00
```
### Configuring ~/.vimrc
The .vimrc file can be used to configure Vim on startup.
Here's a sample ~/.vimrc file:
```
" Example ~/.vimrc
2017-10-27 22:50:49 +03:00
" 2015.10
2016-04-28 10:06:35 +03:00
" Required for vim to be iMproved
set nocompatible
" Determines filetype from name to allow intelligent auto-indenting, etc.
filetype indent plugin on
" Enable syntax highlighting
syntax on
" Better command-line completion
set wildmenu
" Use case insensitive search except when using capital letters
set ignorecase
set smartcase
" When opening a new line and no file-specific indenting is enabled,
" keep same indent as the line you're currently on
set autoindent
" Display line numbers on the left
set number
" Indentation options, change according to personal preference
" Number of visual spaces per TAB
set tabstop=4
" Number of spaces in TAB when editing
set softtabstop=4
" Number of spaces indented when reindent operations (>> and < < ) are used
set shiftwidth=4
" Convert TABs to spaces
set expandtab
" Enable intelligent tabbing and spacing for indentation and alignment
set smarttab
```
### References
[Vim | Home ](http://www.vim.org/index.php )
`$ vimtutor`
[A vim Tutorial and Primer ](https://danielmiessler.com/study/vim/ )
[What are the dark corners of Vim your mom never told you about? (Stack Overflow thread) ](http://stackoverflow.com/questions/726894/what-are-the-dark-corners-of-vim-your-mom-never-told-you-about )
[Arch Linux Wiki ](https://wiki.archlinux.org/index.php/Vim )