I used Vim every now and then when I needed to do a quick file editing in the terminal. I was reluctant to take the learning curve until very recently. My IDE of choice is struggling with a relatively large codebase. I found many of my colleagues flying through source code using Vim. This changed my position on the learning curve. Now, I the potential IDE legginess justifies the learning time. Besides, Vim is cool.
Moving between words and characters
Motions
1
2
3
4
5
6
** To move the cursor, press the h,j,k,l keys as indicated. **
^
k Hint: The h key is at the left and moves left.
< h l > The l key is at the right and moves right.
j The j key looks like a down arrow.
v
w
: Move to the next wordb
: Move to the previous worde
: Move to the end of current word0
: Move to the beginning of the line$
: Move to the end of the linef๐ธ
: Move on the first ๐ธ charactert๐ธ
: Move before the first ๐ธ characterF๐ธ
: Move back on the first ๐ธ characterT๐ธ
: Move back before the first ๐ธ character- Use
;
to repeat the lastf
,F
,t
orT
- Use
,
to inverse-repeat direction of the lastf
,F
,t
or `T
- Use
%
: Find the matching (,),],[,{ or }^+o
: Back - Move to previous courser position (i.e. After jumping to a search result)^+i
: Forward - Move to next courser position (i.e. after jumping to a search result)
Modes
As developers we tend to spend most of our time navigating through files and making smallish edits. Vim normal mode is the default mode the editor starts in. In that mode one canโt write code directly to the editor, but instead move around the file. This is in contrast to insert mode where we type in the text or code directly, which is the normal behaviour of most other editors. This is why Vim is called modal text editor.
It is a good practice to spend most of our time in normal mode and use insert mode only when absolutely necessary.
In addition there is visual mode, which one can activate using v
key. It highlights the motions that you are taking. You can consider this as a visual select mode as well. Use it sparingly because it is harder to automate selections this way compared to ones accomplished using motions in normal mode. While we are on the topic, using V
turn visual mode to be more of line selection tool.
visual block mode is a mode where one can select a column or a rectangular block of text. This could be useful for an example to add *
at the beginning of few lines and turn them into a list in markdown for example.
Moving around the file/page
H
: Move to the top of the window (high)M
: Move to the middle of the window (middle)L
: Move to the bottom of the window (low)zz
: Bring the cursor line to the middle of the windowzt
: Bring the cursor line to the top of the windowzb
: Bring the cursor line to the bottom of the window^+d
: Page Down^+u
: Page Upgg
: Move to the beginning of the fileG
: Move to the end of the file^+g
: Information on cursor location in fileโ+G
: Move to the line number โ
Undo and Redo
u
: Undo last commandU
: Undo all changes in the current line^+R
: Redo.
: Repeat last command in the current context
Selecting, Copying and Pasting Text
v
: Start visual selection (use motion operators to select text) then run a command against selected text. If you type :, you will get a prompt starting with โ<,โ> to reference the selection, which you can save to another file for an example if you likey
: Copy (yank) selected textp
: Put (paste) copied text
Search
/
: Search Forward?
: Search Backward\c
: add to the end of search to temporarily ignore case:set ic
: Ignore case for all search and replace:set noic
: unset ignore case:set hls
: Highlight search results:set nohls
: unset highlight search results:set is
: Incremental Search:Set nois
: unset incremental search
n
: Next instanceN
: Previous instance
Operations
x
: delete letter under courseri
: insertI
: insert in the beginning of the linea
: appendA
: append at the end of the lineo
: open line - insert line below and move to insert modeO
: open line above - insert line above and move to insert moder
: Replace the letter under courser (press r + the letter you want to add)R
: Overwriting insertd
: deletedd
: delete a line (cut)p
: put last deleted line (paste last deleted line)c
: change (combination of delete and insert)
Repeating a command twice, in most cases, applies the command to the current line
Objects
aw
: Around word including its trailing spacedaw
: Deletes the current word including its trailing space
iw
: Inner word, exactly the word without including any spacesdiw
: Deletes the exact word without any spaces around it
ip
: Inner paragraphit
: Inner tagi"
: Within quotesi(
: Within brackets
Operations and Motions in action
1
2
3
4
5
6
7
The format for a change command is:
operator [number] motion
where:
operator - is what to do, such as d for delete
[number] - is an optional count to repeat the motion
motion - moves over the text to operate on, such as w (word),
$ (to the end of line), etc.
dw
: delete wordd$
: delete until the end of the line3w
: Repeat move word motion three times3dw
: Repeat both the operation and motion three time3dd
: Delete three lines
Commands (press : first)
q!
: quit without savingw fileNem
: Save Aswq
: save (write) and exits/old/new
: Substitute first instance in lines/old/new/g
: Substitute all instances in lineโ,โs/old/new/g
: Substitute all instances between two line numbers โ and โ%s/old/new/g
: Substitute all instances in file%s/old/new/gc
: Prompt to Substitute all instances in file (will prompt in every instance)!ls
: Run bash command ls.r
: Read or Redirect into the current line, could be followed by a file name (it will be merged in) or a bash command.r !ls
: will read and redirect the output of the command where the courser is
Window management
- To get a new window, go to command mode
:
thennew
: New file in a new horizontal windowvnew
: New file in a new Vertical Windowsplit
orsp
: Edit file in a new Vertical Windowvsplit
orvsp
: Edit file in a new Vertical Windowtabnew
: New file in a new tabtabedit
: Edit file in a new tab
- To Manage windows start with
^+w
thenh
,j
,k
andl
(directions) to move between windowsH
,J
,K
andL
(directions) to move windows around-
or+
to resize the window vertically (lines)<
or>
to resize the window horizontally (columns)=
Restore windows to default configurations (roughly equal spaces)T
Break a window into a new tab
- Managing tabs
gt
: Move to next tabgT
: Move to previous tab
Jetbrains IDEs Vim Plugin and macOS key repeating
You can enable Vim mode in Jetbrains IDEs. Install the plugin and make sure to accept key repetition dialogue when the IDE restarts, otherwise when you keep your finger down on h
with the intent to continuously move left, macOS will instead show accented versions of the letter h
and in my experience the IDE just freezes and panics, or at least the Vim plugin does. If for some reason you want to disable the key repetition you can run the following command in the console.
1
defaults write -g ApplePressAndHoldEnabled -bool false
You can still find those accented characters if you want by using ^+โ+Space
keyboard shortcut which will show a pop-up to search for special characters and other goodies ๐.
For more information on enabling or disabling key repeating on macOS you can check How-To Geek.
Resources
- vimtutor: Run
vimtutor
in the terminal. - Upcase: Onramp To Vim
Comments powered by Disqus.