vim
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| vim [2011/06/28 14:37] – k2patel | vim [2020/08/10 02:35] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| whereas vi would be a great text editor if only it had a decent operating system. | whereas vi would be a great text editor if only it had a decent operating system. | ||
| + | ==== vi cheat-sheet ==== | ||
| + | |||
| + | <code text> | ||
| + | a. cursor movements (items below are sometimes called objects): | ||
| + | h - left one character | ||
| + | l - right one character | ||
| + | j - down one line | ||
| + | k - up one line | ||
| + | w - right one word | ||
| + | b - back one word | ||
| + | $ - to the end of line | ||
| + | 0 - to the beginning of the line | ||
| + | ) - right one sentence | ||
| + | ( - left one sentence | ||
| + | } - right one paragraph | ||
| + | { - left one paragraph | ||
| + | Ctrl-F - forward one page | ||
| + | Ctrl-B - back one page | ||
| + | G - go to (without arguments, go to end of file) | ||
| + | b. deleting: | ||
| + | d - delete | ||
| + | then add one of the cursor movement symbols to | ||
| + | show what should be deleted, i.e.: | ||
| + | d$ - delete to end of line | ||
| + | d0 - delete to the beginning of the line | ||
| + | d} - delete to the end of paragraph | ||
| + | dd - delete delete (delete the whole line) | ||
| + | x - delete character cursor is on | ||
| + | c. other basic commands: | ||
| + | r - replace one character | ||
| + | ZZ - save and exit (hold down shift and press " | ||
| + | y - yank (copy into temporary buffer) | ||
| + | then add cursor movement symbol to show what should be | ||
| + | copied, for example: y) - copy to the end of sentence | ||
| + | Y - yank line cursor is on | ||
| + | p - paste below cursor line (deleted or copied text) | ||
| + | P - paste above cursor line | ||
| + | u - undo last editing command | ||
| + | /sometext - search for " | ||
| + | d. any command can take numeric argument before the name of " | ||
| + | 5dd - delete 5 lines beginning with cursor line (or) d5d - same | ||
| + | 2dw - delete two words (or) d2w - delete two words | ||
| + | c3w - change 3 words | ||
| + | 3Ctrl-B - move up three pages | ||
| + | 1G - go to the first line | ||
| + | e. external commands can be performed on the selected text (in lines) | ||
| + | if command is started with " | ||
| + | !}fmt - reformat paragraph to 72 columns | ||
| + | f. command line (sometimes called "ex mode" | ||
| + | : | ||
| + | g. from the command line a " | ||
| + | | ||
| + | :set all - will show the state of all options | ||
| + | :set number - will show on the screen numbers of all lines | ||
| + | :set autoindent | ||
| + | h. from the command line operations can be performed on the range of lines, | ||
| + | i.e.: | ||
| + | :18,24 del - delete from line 18 to line 24 | ||
| + | :23,48 copy 17 - block from line 23 to 48 copy to line 17 | ||
| + | :2,17 move 92 - block from line 2 to 17 move to line 92 | ||
| + | i. from the command line any external UNIX command can be performed on | ||
| + | the range of lines if line range is superseded by " | ||
| + | :11,16! sed -e " | ||
| + | (the command above wraps the block of text with | ||
| + | " | ||
| + | easier, but this is an example) | ||
| + | :14,19! sort -r +3 | ||
| + | (sort the table in reverse order by fourth column) | ||
| + | j. file manipulation from the command line: | ||
| + | :r somefile - read in " | ||
| + | :x - save and exit (if file is "Read Only", this command will | ||
| + | exit without saving) | ||
| + | :wq - write and quit (same as above) | ||
| + | :w - write (save) if the file permissions allow it | ||
| + | :w! - save file even if it is read-only as long as we own it | ||
| + | :w somefile - save this file as " | ||
| + | :q - quit without saving | ||
| + | :q! - quit without saving if changes were made | ||
| + | k. text input commands (all require " | ||
| + | i - insert text before the character cursor is on | ||
| + | I - insert text at the beginning of the line | ||
| + | a - append (insert text after the character cursor is on) | ||
| + | A - append text to the end of the line | ||
| + | c - change (replace previous text with new one) | ||
| + | takes arguments just like the delete command - it is | ||
| + | a fast and powerful way of changing original text - | ||
| + | much more so than typical " | ||
| + | R - start overwriting text | ||
| + | o - start entering text at the beginning of the new line | ||
| + | below the cursor | ||
| + | O - start entering text at the beginning of the new line | ||
| + | above the cursor | ||
| + | l. if in doubt, press " | ||
| + | </ | ||
| + | |||
| + | [[http:// | ||
| ==== File Cleaning ==== | ==== File Cleaning ==== | ||
| Line 80: | Line 176: | ||
| + | ==== Saving file as root ==== | ||
| + | At some point you would like to save file as root. | ||
| + | |||
| + | <code bash> | ||
| + | :w !sudo tee % | ||
| + | </ | ||
| + | |||
| + | ==== Quick tools ==== | ||
| + | Comment line from current line. | ||
| + | <code bash> | ||
| + | :.,$s/^/#/g | ||
| + | </ | ||
| ==== Marking ==== | ==== Marking ==== | ||
| + | <code vim> | ||
| + | mk - record current location as mark k (redefines any previous mark k) | ||
| + | 'k - return to line of mark k | ||
| + | `k - return to mark k | ||
| + | d'k - delete to line of mark k | ||
| + | d`k - delete to mark k | ||
| + | c'k - change text to line of mark k | ||
| + | c`k - change text to mark k | ||
| + | " | ||
| + | </ | ||
| + | |||
| + | ==== Editing ==== | ||
| + | <code vim> | ||
| + | o - open a new line above cursor(*) | ||
| + | O - open a new line below cursor(*) | ||
| + | i - insert text ahead of cursor(*) | ||
| + | I - insert text at the beginning of the line(*) | ||
| + | a - append text after the cursor(*) | ||
| + | A - append text at the end of the line(*) | ||
| + | c$ - change to end of the line (*) | ||
| + | d$ - delete to end of the line | ||
| + | C - same as c$ (*) | ||
| + | D - same as d$ | ||
| + | cG - change to end of the file (*) | ||
| + | dG - delete to end of the file | ||
| + | c0 - change to beginning of file (*) | ||
| + | d0 - delete to beginning of file | ||
| + | cc - change line (*) | ||
| + | dd - delete line | ||
| + | c'm - change from cursor through mark m (*) | ||
| + | d'm - delete from cursor through mark m | ||
| + | 3cc - change 3 lines (*) | ||
| + | 3dd - delete 3 lines | ||
| + | 8cw - change next 8 words (*) | ||
| + | 8dw - delete next 8 words | ||
| + | R - overwrite current line, starting at cursor(*) | ||
| + | r - replace character at cursor | ||
| + | s - substitute for character at cursor (*) | ||
| + | 8s - substitute for next 8 characters (*) | ||
| + | S - substitute for entire line (*) | ||
| + | J - join two lines together | ||
| + | . - repeats previous edit command | ||
| + | xp - transpose two characters | ||
| + | easESC - add a plural, and go back to command mode | ||
| + | </ | ||
| + | |||
| + | ==== Handling ==== | ||
| + | <code txt> | ||
| + | h - move to the left | ||
| + | CTL-f - change display forward a page | ||
| + | j - `jump` down a line | ||
| + | CTL-b - change display back a page | ||
| + | k - move up a line | ||
| + | CTL-d - change display down half a page | ||
| + | l - move to the right | ||
| + | CTL-u - change display up half a page | ||
| + | - - same as k | ||
| + | CTL-y - shift display down on screen | ||
| + | + - same as j | ||
| + | CTL-e - shift display up on screen | ||
| + | e - move to the end of a word | ||
| + | z. - recenter display around cursor | ||
| + | w - move forward to the beginning of a word | ||
| + | z- - recenter display so cursor is at top | ||
| + | b - move backward to the beginning of a word | ||
| + | z+ - recenter display aso cursor is at bottom | ||
| + | $ - move to the end of the line | ||
| + | zCR - recenter display so cursor is at top | ||
| + | 0 - move to the beginning of the line | ||
| + | 'm - move to the beginning of the line of mark m | ||
| + | ^ - move to the beginning of the line | ||
| + | `m - move to the location of mark m | ||
| + | G - move to the end of the file | ||
| + | gg - move to the beginning of the file | ||
| + | ) - move forward 1 sentence | ||
| + | H - move to the top of the display | ||
| + | ( - move back 1 sentence | ||
| + | M - move to the middle of the display | ||
| + | } - move forward 1 paragraph | ||
| + | L - move to the bottom of the display | ||
| + | { - move back 1 paragraph | ||
| + | B - move back to previous blank space | ||
| + | 20| - go to 20th character in the line | ||
| + | E - move ahead to next blank space | ||
| + | CTL-L - clear and redraw | ||
| + | B - move back to previous blank space | ||
| + | CR - same as j | ||
| + | CTL-G - print current location in the file | ||
| + | :22 - move to line 22 | ||
| + | 6w - move forward 6 words | ||
| + | 6b - move backward 6 words | ||
| + | 8+ - move down 8 lines | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Play with Buffers ==== | ||
| + | <code txt> | ||
| + | yy - yank current line to unnamed buffer | ||
| + | "j8yy - yank 8 lines into buffer j | ||
| + | 19yy - yank next 19 lines to unnamed buffer | ||
| + | "J8YY - append the next 8 lines into buffer j | ||
| + | p - put unnamed buffer contents after cursor | ||
| + | p - recover previous edit | ||
| + | P - put unnamed buffer contents before cursor | ||
| + | "1p - recover 2nd previous edit | ||
| + | 19dd - delete next 19 lines, and put them in unnamed buffer | ||
| + | "7p - recover 8th previous edit | ||
| + | "bp - put the contents of buffer p into current file | ||
| + | :11,14 ya w - yank lines 11 through 14 into buffer w | ||
| + | :94 pu w - put contents of buffer w after line 94 | ||
| + | </ | ||
| ===== VIM Scripts ===== | ===== VIM Scripts ===== | ||
| Line 116: | Line 335: | ||
| === VIM Variable / Initialization (.vimrc) === | === VIM Variable / Initialization (.vimrc) === | ||
| + | OR | ||
| + | === You can use .exrc === | ||
| == toggle syntax highlighting == | == toggle syntax highlighting == | ||
| Line 172: | Line 393: | ||
| * list: an ordered sequence of values delimited by square brackets, with implicit integer indices starting at zero. For example: [' | * list: an ordered sequence of values delimited by square brackets, with implicit integer indices starting at zero. For example: [' | ||
| * dictionary: an unordered set of values delimited by braces, with explicit string keys. For example: {' | * dictionary: an unordered set of values delimited by braces, with explicit string keys. For example: {' | ||
| + | |||
| + | === Sample .vimrc === | ||
| + | <code bash> | ||
| + | set number | ||
| + | " | ||
| + | |||
| + | set nocompatible | ||
| + | " | ||
| + | |||
| + | set autoindent | ||
| + | set smartindent | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | |||
| + | set tabstop=4 | ||
| + | set shiftwidth=4 | ||
| + | " | ||
| + | " | ||
| + | |||
| + | set showmatch | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | |||
| + | set guioptions-=T | ||
| + | " | ||
| + | " | ||
| + | |||
| + | set vb t_vb= | ||
| + | " | ||
| + | " | ||
| + | |||
| + | set ruler | ||
| + | " | ||
| + | |||
| + | set nohls | ||
| + | " | ||
| + | " | ||
| + | |||
| + | set incsearch | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | |||
| + | set virtualedit=all | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | |||
| + | " | ||
| + | |||
| + | highlight Comment ctermfg=green | ||
| + | " | ||
| + | " | ||
| + | |||
| + | |||
| + | :set nu ts=4 sw=4 shiftround ignorecase smartcase | ||
| + | " let perl_fold=1 | ||
| + | : | ||
| + | " to set the title of the window where you are working | ||
| + | |||
| + | :set title | ||
| + | :auto BufEnter * let & | ||
| + | :auto BufEnter * let & | ||
| + | |||
| + | :set statusline+=%f\ | ||
| + | :set smartindent | ||
| + | :set expandtab | ||
| + | :set tabstop=4 | ||
| + | |||
| + | |||
| + | :set ruler | ||
| + | :set nowrap | ||
| + | :set incsearch | ||
| + | |||
| + | :au Syntax pl | ||
| + | :au Syntax pm | ||
| + | :au Syntax pod source ~/ | ||
| + | :au Syntax lib source ~/ | ||
| + | |||
| + | ":set foldmethod=indent | ||
| + | ":set foldlevel=0 | ||
| + | |||
| + | </ | ||
| + | |||
| ==== Basic Info ==== | ==== Basic Info ==== | ||
vim.1309271837.txt.gz · Last modified: 2020/08/10 02:29 (external edit)
