-- YurySmirnov - 07-Jul-2011

Vim tips and tricks

vi -R filename.ext - starts vi in read-only session

i - enters editor mode

Esc key - exits to normal (command) mode from any other

x - deletes this symbol

u - undoes last change

(Ctrl key + R) twice - in normal mode - redoes (note - R is CAPITAL!!)

ZZ (or :q) - exits Vim saving file

ZQ (or :q!) - exits Vim withOUT changes

dd - deletes a line

o - inserts a new line and moves all what was righter to that line

:help - guess what? smile

i!!! or 3i! - inserts !!! from normal mode; also for ex. 8x - deletes 8 characters

w - moves the cursor one word to the right; number before w is allowed (repetes w number times, e.g. 4w)

b - moves the cursor one word to the left

$ - moves the cursor to the end of the line; 5$ moves the cursor to the end of fifth line starting from one you are on; $ is the same as 1$

^ - moves the cursor to the first nonblank element of the line (as usual starting from the left)

Home key - moves the cursor to the first (any) symbol of the line

fx - searches this line only forward for x

Fy - searches this line only backward for y

k - moves the cursor one line up, same horizontal position

j - moves the cursor one line down, same horizontal position

h - moves the cursor one symbol to the left, same line

l - moves the cursor one symbol to the right, same line

7G - moves the cursor to the 7th line from file beginning

:set number - enables lines numbers at the left corner

:set nonumber - disables lines numbers at the left corner

Ctrl key + G - displays a status line that indicates where you are in the file.

Ctrl key + U - scrolls half a screen up

Ctrl key + D - scrolls half a screen down

dw - deletes a word; d3w - deletes three words

d$ - deletes from the cursor to the end of the line

. - repeats previous delete or change command

J - joins 2 lines; 3J joins 3 lines etc.

rh - replaces the symbol under the cursor with h; 5ra replace (one) symbol under the cursor with aaaaa

~ - changes the case of a symbol under the cursor

/abcd - searches for abcd in whole file and move the cursor there

/ - searches next abcd entry

n - does the same as previous one

nyy - copies n lines into buffer, e.g. 14yy copies 14 next lines into buffer

P (or just p) - pastes those lines out off buffer

:file - prints out current file name, lines quantity and current position in percents

ncc - "cuts" n lines into buffer, see nyy. To paste them out see P (or p ).

:%s/abcd/efgh/cg - replaces (s - substitute) all (starting from the first line line - %) instances of abcd to efgh , every time with confirmation (c), all instances (g) in every line. If the abcd is not specified, e.g. :%s//efgh/cg, your last search pattern will be used instead and replaced with efgh. It's great if you don't want to type the pattern: just put the word you want to be replaced under the cursor, and press *, then go ahead with something like :%s//efgh/cg.

:,$s/abcd/efgh/cg - same, but instead of searching from the first line, it starts searching from the line you are on

:split filename.ext - splits your screen into two, keeps the file you have had opened in the first one and opens (to write) file filename.ext in the second screen. To move between screens use Ctrl-Ww (holding Ctrl all the time, press first Shift+w then w without releasing Ctrl!!). To close one of them move to it with Ctrl-Ww and close (ZZ or ZQ).

vi -o file1.txt file2.txt - splits your screen horizontally and opens two windows, one with file1.txt, another with file2.txt

vi -O file1.txt file2.txt - splits your screen vertically and opens two windows, one with file1.txt, another with file2.txt

:exe 'normal '.(virtcol('$')/2).'|' - sets cursor to the middle of the line

:0 - go to the beginning of the file

:$ - go to the end of the file

ci" - edit text between quotes on this line

ci( - edit text between brackets on this line

:set filetype - find out which file type vim thinks it is. Possible values: zsh, python, vi, c, cpp, sh etc.

# - search backwards for the word the cursor is positioned on

* - search forward for the word the cursor is positioned on

ddkkp - swap current line with the previous one

Ctrl key + V - go to Visual Block mode

:%y - copy all (Ctrl-A + Ctrl-C in Win)

:set spell - turn the spellchecker on

]s - jump to the next "misspelled" word (only when spellchecker is on)

[s - jump to the previous "misspelled" word (only when spellchecker is on)

zg - tell vi that this "misspelled" word is actually spelled correctly (only when spellchecker is on)

ciw - to change the word the cursor is positioned on (i.e., to delete it, switch to editing mode and move the cursor to the beginning of the deleted word)

% - jump to the matching brace (any of the following: ({[]}))

d0 - deletes everything from the cursor current position till the beginning of the current line

:%s/ABCD//gn - counts the number of matches of ABCD in the current file

ggyG - copies the file content to the default register

Ctrl key + a - increments a digit under cursor

Ctrl key + x - decrements a digit under cursor

:set list - shows hidden characters (tabs, spaces etc.)

Ctrl key + r, then %, all in the editor mode - insert file name right here

In case of any malfunction with a filename.ext a .filename.ext.swp file will be saved with the "last change". To recover the text, open your original file with vi, press R when prompted, then :w new_filename.ext to save it as a new file. Now exit with ZZ. Your text is recovered in the new_filename.ext. Finally, delete the .filename.ext.swp file, and replace your original file with the the recovered one using something like mv new_filename.ext filename.ext.

Visual block mode:

use arrows to select a 1-symbol-wide rectangle with a text. Use Imystr (notice I is capital!), followed by the Esc key to insert mystr in front of your selection. Use Delete key to delete any rectangle. Use r6 to replace your 1-symbol-wide rectangle with 6's. Using something like r6 on a wider rectangle will replace all its content (all columns, all rows) with 6's. Use c to change the selection (i.e. to delete the selection and go into the insert mode), followed by the Esc key as in the Imystr example.

Find 10th occurrence of "pattern" in a file: :0 to go to the beginning of the file, then /pattern to find the first occurrence, finally 9n to find the 10th occurrence.

vimdiff tips and tricks

do - gets (obtains) changes from other window into the current window

dp - puts the changes from current window into the other window

]c - jumps to the next change

[c - jumps to the previous change

Ctrl+w+w - switches to the other split window

zc - folds (closes) a piece of code

zo - unfolds (opens) a piece of code

zR - unfolds all foldings

Edit | Attach | Watch | Print version | History: r34 < r33 < r32 < r31 < r30 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r34 - 2019-04-22 - unknown
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Sandbox All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback