This shows you the differences between two versions of the page.
vim [2011/06/28 14:43] k2patel |
vim [2020/08/10 02:35] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== VIM ====== | ||
- | There's an old joke that Emacs would be a great operating system if only it had a decent text editor,\\ | ||
- | whereas vi would be a great text editor if only it had a decent operating system. | ||
- | |||
- | ==== File Cleaning ==== | ||
- | |||
- | === Remove Blank line from the file === | ||
- | |||
- | to remove blank lines please use following. | ||
- | <code bash> | ||
- | :g/^$/d | ||
- | </code> | ||
- | OR | ||
- | <code bash> | ||
- | :g/^ *$/d | ||
- | </code> | ||
- | |||
- | === Remove Tailing space as well space at beginning using VI === | ||
- | |||
- | To remove tailing space | ||
- | <code bash> | ||
- | :%s/\s\+$// | ||
- | </code> | ||
- | |||
- | To remove space at beginning | ||
- | <code bash> | ||
- | :%s/^\s\+// | ||
- | </code> | ||
- | |||
- | === Replace tab with space === | ||
- | |||
- | To replace tab with space you need to use | ||
- | <code bash> | ||
- | :%s/[CTRL-i]/ /g | ||
- | </code> | ||
- | |||
- | === Delete Line start with === | ||
- | |||
- | To delete lines start with some word e.g. Redirect | ||
- | <code bash> | ||
- | :g/^Redirect/d | ||
- | </code> | ||
- | |||
- | === Remove Dos / Windows carriage-return === | ||
- | |||
- | To remove Carriage return in vi use following | ||
- | <code bash> | ||
- | :%s/^M//g | ||
- | </code> | ||
- | NOTE : to create "^M" use "Ctrl+v Ctrl+M" Regular ^M does not help. | ||
- | |||
- | ==== Convert all character to lower OR upper case ==== | ||
- | NOTE : This will apply to whole file as we are using (%)\\ | ||
- | To convert upper case use following | ||
- | <code bash> | ||
- | :%s/\([a-z]\)/\u\1/g | ||
- | </code> | ||
- | To convert lower case use following | ||
- | <code bash> | ||
- | :%s/\([A-Z]\)/\l\1/g | ||
- | </code> | ||
- | |||
- | ==== Match Start of line Append End of line ==== | ||
- | |||
- | To Match Start of line and append that line use following\\ | ||
- | (search Append end of line) | ||
- | <code bash> | ||
- | %g/^DocumentRoot/s/$/\/www/ | ||
- | </code> | ||
- | |||
- | Which append "/www" if start of line is "DocumentRoot". | ||
- | |||
- | Also you can append after word by following. | ||
- | |||
- | <code bash> | ||
- | %s/word/&test/g | ||
- | </code> | ||
- | |||
- | So all word is get replaced by "wordtest". | ||
- | |||
- | |||
- | ==== 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 | ||
- | "ay'd - yank text into buffer a from cursor through line of mark d | ||
- | </code> | ||
- | |||
- | ==== 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 | ||
- | </code> | ||
- | |||
- | ==== Handling ==== | ||
- | <code vim> | ||
- | 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 | ||
- | ) - 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 | ||
- | </code> | ||
- | |||
- | ===== VIM Scripts ===== | ||
- | Introduction to vim scripts ":help vim-script-intro"\\ | ||
- | |||
- | == Running Vim scripts == | ||
- | There is many way to run vim script simple way is to run calling script from `source` | ||
- | <code bash> | ||
- | :source /home/yahoo/test.vim | ||
- | </code> | ||
- | You can also call command from script directly | ||
- | <code bash> | ||
- | :call MyBackupFunc(expand('%'), { 'all':1, 'save':'recent'}) | ||
- | </code> | ||
- | You can do mapping of the keyboard using the variables. | ||
- | <code bash> | ||
- | :nmap ;s :source /home/yahoo/test.vim<CR> | ||
- | </code> | ||
- | The key sequence `;s` will execute the specified script file\\ | ||
- | You can call fucntion using the stored mapping same way. | ||
- | <code bash> | ||
- | :nmap \b :call MyBackupFunc(expand('%'), { 'all': 1 })<CR> | ||
- | </code> | ||
- | To call the function you need to call enter squence `\b` | ||
- | |||
- | === syntax highlighting === | ||
- | VIM support syntax highlighting. to turn it on. | ||
- | <code bash> | ||
- | :syntax on | ||
- | </code> | ||
- | to turn it off | ||
- | <code bash> | ||
- | :syntax off | ||
- | </code> | ||
- | |||
- | === VIM Variable / Initialization (.vimrc) === | ||
- | |||
- | == toggle syntax highlighting == | ||
- | <code bash> | ||
- | function! ToggleSyntax() | ||
- | if exists("g:syntax_on") | ||
- | syntax off | ||
- | else | ||
- | syntax enable | ||
- | endif | ||
- | endfunction | ||
- | |||
- | nmap <silent> ;s :call ToggleSyntax()<CR> | ||
- | </code> | ||
- | `;s` sequence to flip syntax highlighting on or off every time typed when you're in Normal mode.\\ | ||
- | nmap stands for "normal-mode key mapping".\\ | ||
- | <silent> option causes the mapping not to echo any command it's executing. | ||
- | == Creating centered titles == | ||
- | <code bash> | ||
- | function! CapitalizeCenterAndMoveDown() | ||
- | s/\<./\u&/g "Built-in substitution capitalizes each word | ||
- | center "Built-in center command centers entire line | ||
- | +1 "Built-in relative motion (+1 line down) | ||
- | endfunction | ||
- | |||
- | nmap <silent> \C :call CapitalizeCenterAndMoveDown()<CR> | ||
- | </code> | ||
- | single backslash used for continuation marker | ||
- | <code bash> | ||
- | call SetName( | ||
- | \ first_name, | ||
- | \ middle_initial, | ||
- | \ family_name | ||
- | \ ) | ||
- | </code> | ||
- | Also you can use "|" to write two line on singe line. | ||
- | <code bash> | ||
- | echo "Starting..." | call Phase(1) | call Phase(2) | echo "Done" | ||
- | </code> | ||
- | You can comment using single quote, as double quote used to define a string.\\ | ||
- | also you can use "|" to escape the comment in some cases. | ||
- | <code bash> | ||
- | echo "> " |"Print generic prompt | ||
- | </code> | ||
- | === Defining variable === | ||
- | to define variable you need to use word `let` | ||
- | |||
- | <code bash> | ||
- | let name = "Damian" | ||
- | let height = 165 | ||
- | let interests = [ 'Cinema', 'Literature', 'World Domination', 101 ] | ||
- | let phone = { 'cell':5551017346, 'home':5558038728, 'work':'?' } | ||
- | </code> | ||
- | |||
- | * scalar: a single value, such as a string or a number. For example: "Damian" or 165 | ||
- | * list: an ordered sequence of values delimited by square brackets, with implicit integer indices starting at zero. For example: ['Cinema', 'Literature', 'World Domination', 101] | ||
- | * dictionary: an unordered set of values delimited by braces, with explicit string keys. For example: {'cell':5551017346, 'home':5558038728, 'work':'?'} | ||
- | |||
- | ==== Basic Info ==== | ||
- | == Variable scoping == | ||
- | <code text> | ||
- | g: varname The variable is global | ||
- | s: varname The variable is local to the current script file | ||
- | w: varname The variable is local to the current editor window | ||
- | t: varname The variable is local to the current editor tab | ||
- | b: varname The variable is local to the current editor buffer | ||
- | l: varname The variable is local to the current function | ||
- | a: varname The variable is a parameter of the current function | ||
- | v: varname The variable is one that Vim predefines | ||
- | </code> | ||
- | == pseudovariables == | ||
- | <code text> | ||
- | & varname A Vim option (local option if defined, otherwise global) | ||
- | &l: varname A local Vim option | ||
- | &g: varname A global Vim option | ||
- | @ varname A Vim register | ||
- | $ varname An environment variable | ||
- | </code> | ||