Skip to content

Instantly share code, notes, and snippets.

@sphingu
Last active December 27, 2021 15:19
Show Gist options
  • Save sphingu/5add27163528d7feda0075ff25d9be04 to your computer and use it in GitHub Desktop.
Save sphingu/5add27163528d7feda0075ff25d9be04 to your computer and use it in GitHub Desktop.
Mastering the vi and Vim Editors on the Linux, Unix, Mac, and Windows Operating Systems

Essential Navigation Commands

:q - quit :q! - force quit

:wq - write & quit

:23 - go to line No. 23

:$ - go to last line

:set ruler - set vim setting to show ruler

:set noruler - set vim setting to hide ruler

:set ruler! - set vim setting to toggle ruler

j - move down

k - move up

h - move left

l - move right

ctrl+f - move page down

ctrl+b - move page up

w - word forward

b - word backword

W - word forward with considering whitespace only

B - word backward with considering whitespace only

0 - go to start of the line

^ - go to first word of line

$ - go to last word of line

gg - go to first line

G - go to last line

29gg - go to 29th line

29G - go to 29th line

ctrl+G - display status line at bottom

@sphingu
Copy link
Author

sphingu commented Dec 19, 2021

Deleting text

[count][operator]{motion} - 3dw
x - delete character at cursor
X - delete character before cursor
d$ = D - delete all character starting from cursor position till end of line
d0 - delete all character start of line till cursor position
dw - delete word
dW - delete word (seperated by space)
db - delete word backward to cursor position
dB - delete word backward to cursor position (seperated by space)
2d3w = 6dw = d6w - delete six words
dd - delete line
2dd - delete two line
. - execute last executed command
:w - save changes

@sphingu
Copy link
Author

sphingu commented Dec 20, 2021

Help document command

:h = :help - display help document
:h gg - show help for given gg command
:h ctrl-f = :h ^f - show help for CTRL-F (carat & 'ctrl' work same way)

on help document
ctrl+] - jump to documentation of current word on cursor.
ctrl+o - jump to previous location in help doc.

ctrl+w+w - switch between opened window

@sphingu
Copy link
Author

sphingu commented Dec 26, 2021

Cut, Copy & Paste

  • Term cut => delete
  • Term copy => yank
  • Term paste => put

dd - cut line
p - paste/put bellow cursor position
P - paste/put before cursor position
x - cut single character

y - yank/copy
3yw - copy 3 word
4yy - copy 4 line

u - undo
ctrl+r - redo

Register Types

  • Unnamed => ""
  • Numbered => "0 "1 ... "9
  • Named

:reg - show register

"" => hold text from d,c,s,x and y operations
"0 => last text yanked(y)
"1 => last text deleted(d) or changed(c)
"_ => block-hole register

"0P => put most recent yanked(t) text
"_dd => store deleted line(by operation(dd)) to black-hole register
"byy => copy line to named register 'b'
"Byy => append line with existing content of named register 'b'
2"b2yy => copy two line twise
:reg z => show content of 'z' register
:reg 1b => show content of both '1' and 'b' register

@sphingu
Copy link
Author

sphingu commented Dec 27, 2021

Insert, Replace, Change & Joining

i - current cursor position
I - starting position of current line.
a - next character from current cursor position
A - end of line
o - line bellow
O - line bellow

80i => TEST => esc - will insert TEST 80 times (can use other insert mode enter operator)

R - turn on replace mode

cw - delete current word and turn on insert mode
"acw - delete current word and turn on insert mode & store deleted word in register 'a'
c$ - C - delete text till line end and turn on insert mode
cc - delete entire line and turn on insert mode

~ - make current character to upper case
g~w - make entire word to upper case
g~$ - make entire line to upper case
gUw - make entire word to upper case
gUU - make entire line to upper case
guw - make entire word to lowercase case
guu - make entire line to lowercase case

J - join line
gJ - join line without space for line ending with period case

@sphingu
Copy link
Author

sphingu commented Dec 27, 2021

Search, find, join, replace, Substritute

fb - search for 'b' character forward in current line
Fb - search for 'b' character backward in current line
; - repeat search same direction
, - repeat search opposite direction

tb - move to before first occurance of 'b' forward in current line (only if it is not before searched character)
;b - move to before first occurance of 'b' forward in current line (can repeat unlike tb command)
Tb - backward to tb command

dtw - delete till 'w' character found
dt[space] - delete till space found

/test - search for 'test' word in entire file forward
?test - search for 'test' word in entire file backward

* - search for current cursor word in entire file forward
# - search for current cursor word in entire file backward

n - for next search same direction
N - for next search opposite direction

d => /This - will delete text untill it find 'This' word
"ay => /z - will copy text till it find 'z' character

:set is? - check whether (insearch) is enable or not
:set is - enable (insearch)
:set hls? - check whether (hlsearch) is enable or not
:nohls - remove highlights

Substitute - :[range]s/old/new/[flags]

:1s/is/isnot/ - in first line, 'is' will replace by 'isnot'
:1,5s/for/FOR/g - in first five lines, 'for' will replace by 'FOR' for all occurance in line
:%s/for/FOR/g = :1,$s/for/FOR/g - same as above but for all lines
:/Test1/,/Test2/s/net/org/g - replace 'net' with 'org' for all occurance between 'Test1' and 'Test2'
:s#one/two#three/four# - used here '#' sign to ignore conflict with '/'. can use any character instad of '#' as seperator

:set nu - turn on line numbering

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment