User Tools

Site Tools


vim

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
vim [2011/06/28 14:48]
k2patel
vim [2020/08/10 02:35] (current)
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 "​z"​ twice)
 +        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 "​sometext"​
 +d. any command can take numeric argument before the name of "​object",​ i.e.:
 +        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 "​!",​ i.e.:
 +        !}fmt - reformat paragraph to 72 columns
 +f. command line (sometimes called "ex mode"​):​
 +        :
 +g. from the command line a "​set"​ command can be executed to 
 +   ​customize editing environment,​ i.e.:
 +        :set all - will show the state of all options
 +        :set number - will show on the screen numbers of all lines
 +        :set autoindent ​  // obvious
 +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 "​s/​^/​\/​\*/"​ -e "​s/​$/​\*\//"​
 +                (the command above wraps the block of text with
 +                 "​C"​ style comments - /* text */.  It can be done
 +                  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 "​somefile"​
 +        :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 "​somefile"​
 +        :q - quit without saving
 +        :q! - quit without saving if changes were made
 +k. text input commands (all require "​Esc"​ to terminate):
 +        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 "​overwrite"​
 +        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 "​Esc"​
 +</​code>​
 +
 +[[http://​www.infobound.com/​vi.html | Ref.]]
 ==== 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 %
 +</​code>​
 +
 +==== Quick tools ====
 +Comment line from current line.
 +<code bash>
 +:.,$s/^/#/g
 +</​code>​
 ==== Marking ==== ==== Marking ====
 <code vim> <code vim>
Line 153: Line 261:
 ^ - move to the beginning of the line            ^ - move to the beginning of the line           
 `m - move to the location of mark m `m - move to the location of mark m
-G - move to the end of the file                 +G - move to the end of the file 
 +gg - move to the beginning ​of the file                 
 ) - move forward 1 sentence ) - move forward 1 sentence
 H - move to the top of the display ​             ​ H - move to the top of the display ​             ​
Line 226: Line 335:
  
 === VIM Variable / Initialization (.vimrc) === === VIM Variable / Initialization (.vimrc) ===
 +OR
 +=== You can use .exrc ===
  
 == toggle syntax highlighting == == toggle syntax highlighting ==
Line 282: Line 393:
     * list: an ordered sequence of values delimited by square brackets, with implicit integer indices starting at zero. For example: ['​Cinema',​ '​Literature',​ 'World Domination',​ 101]     * 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':'?'​}     * dictionary: an unordered set of values delimited by braces, with explicit string keys. For example: {'​cell':​5551017346,​ '​home':​5558038728,​ '​work':'?'​}
 +
 +=== Sample .vimrc ===
 +<code bash>
 +set number
 +" ​      show line number
 +
 +set nocompatible ​
 +" ​      This setting prevents vim from emulating the original vi's bugs and limitations.
 +
 +set autoindent
 +set smartindent ​
 +" ​      The first setting tells vim to use "​autoindent" ​
 +" ​      (that is, use the current line's indent level to set the indent level of newlines). ​
 +" ​      The second makes vim attempt to intelligently guess the indent level of any new line based on the previous line, 
 +" ​      ​assuming the source file is in a C-like language. Combined, they are very useful in writing well-formatted source code.
 +
 +set tabstop=4
 +set shiftwidth=4 ​
 +" ​      I prefer 4-space tabs to 8-space tabs. The first setting sets up 4-space tabs,
 +" ​      and the second tells vi to use 4 spaces when text is indented (auto or with the manual indent adjustmenters.)
 +
 +set showmatch ​
 +" ​      This setting will cause the cursor to very briefly jump to a brace/​parenthese/​bracket'​s "​match" ​
 +" ​      ​whenever you type a closing or opening brace/​parenthese/​bracket. ​
 +" ​      ​I'​ve had almost no mismatched-punctuation errors since I started using this setting.
 +
 +set guioptions-=T ​
 +" ​      I find the toolbar in the GUI version of vim (gvim) to be somewhat useless visual clutter.
 +" ​      This option gets rid of the toolbar.
 +
 +set vb t_vb= 
 +" ​      This setting prevents vi from making its annoying beeps when a command doesn'​t work.
 +" ​      ​Instead,​ it briefly flashes the screen -- much less annoying.
 +
 +set ruler 
 +" ​      This setting ensures that each window contains a statusline that displays the current cursor position.
 +
 +set nohls 
 +" ​      By default, search matches are highlighted. I find this annoying most of the time. 
 +" ​      This option turns off search highlighting. You can always turn it back on with :set hls.
 +
 +set incsearch ​
 +" ​      With this nifty option, vim will search for text as you enter it. 
 +" ​      For instance, if you type /bob to search for bob, vi will go to the first "​b"​ after you type the "​b," ​
 +" ​      to the first "​bo"​ after you type the "​o,"​ and so on. 
 +" ​      It makes searching much faster, since if you pay attention you never have to enter 
 +" ​      more than the minimum number of characters to find your target location. ​
 +" ​      Make sure that you press Enter to accept the match after vim finds the location you want.
 +
 +set virtualedit=all ​
 +" ​      By default, vim doesn'​t let the cursor stray beyond the defined text. 
 +" ​      This setting allows the cursor to freely roam anywhere it likes in command mode. 
 +" ​      It feels weird at first but is quite useful.
 +
 +" ​      Type :help options within vim to get a complete list of options. ​
 +
 +highlight Comment ctermfg=green
 +" ​      ​Comments are shown in a brightgreen color which is visible clearly as against ​
 +" ​      a dark blue which is annoyingly hard to read to read, at least to me.
 +
 +
 +:set nu ts=4 sw=4 shiftround ignorecase smartcase
 +" let perl_fold=1
 +:​colorscheme elflord
 +" to set the title of the window where you are working
 +
 +:set title
 +:auto BufEnter * let &​titlestring = hostname() . ":"​ . expand("​%:​p"​)
 +:auto BufEnter * let &​titleold = hostname() . ":"​ . getcwd()
 +
 +:set statusline+=%f\
 +:set smartindent
 +:set expandtab
 +:set tabstop=4
 +
 +
 +:set ruler
 +:set nowrap
 +:set incsearch
 +
 +:au Syntax pl   ​source ~/​.vim/​test.vim
 +:au Syntax pm   ​source ~/​.vim/​test.vim
 +:au Syntax pod  source ~/​.vim/​test.vim
 +:au Syntax lib  source ~/​.vim/​test.vim
 +
 +":set foldmethod=indent
 +":set foldlevel=0
 +
 +</​code>​
 +
  
 ==== Basic Info ==== ==== Basic Info ====
vim.1309272505.txt.gz · Last modified: 2020/08/10 02:29 (external edit)