February 12, 2017

vi shortcuts collection

❑ vi + file1: Place the cursor at the last line of the file1 when the file1 is opened.


<Command mode>

❑ i: Switch to the insert mode at the cursor.
❑ a: Switch to the insert mode to the right of the cursor.
❑ ~: Switch between lowercase-uppercase at the cursor.

❑ ^ (or 0): Move the cursor to the beginning of the line.
❑ $: Move the cursor to the end of the line.
❑ H: Move the cursor to the top of the file.
❑ M: Moves the cursor to the middle of the file.
❑ L: Move the cursor to the bottom of the file.
❑ (n)G: Move the cursor to the end line(or n line) of the file.
❑ Ctrl + d: Move the cursor down the half screen.
❑ Ctrl + u: Move the cursor up the half screen.

❑ x: Delete one character.
❑ dd: Delete one line.
❑ 3dd: Delete three lines from the cursor line.
❑ J: Concatenate the current line with the next line.

❑ yy: Copy current line to buffer.
❑ 3y: Copy three rows down including the current row to the buffer.
❑ p: Paste contents in the buffer.

❑ / str: Search str from the file.
❍ n: Move down to find next str.
❍ N: Move up to find next str.

❑ .: Redo the previous command.
❑ u: Cancel the previous command.
❑ Ctrl + r: Revoke canceled the previous command.
❑ Ctrl + g: Print information of the current file.



<Line mode>

❑ :s/str1/sub1: Change str1 of current line to sub1.
❑ :%s/str1/sub1/g: Change all str1 in file to sub1.

❑ :wq! ./result.txt: Force save to ./result.txt and exit vi editor.
❑ :r ./result.txt: Load the contents of ./result.txt to the current cursor location.

❑ :set nu: Print line number.
❑ :set nonu: Hide line number.
❑ :e [Path]: Open a file.
❑ :! [cmd]: Execute the shell command.
❑ :set shell=/bin/bash: Change the current shell as /bin/bash.
❑ :sh: Execute default shell.