Monday, October 18, 2010

vi and vim commands

Command mode ESC

                dd       delete
                u        undelete
                y        yank (copy to buffer)
                p/P      p before cursor/P after cursor

                Ctl-g    show current line number
                shft-G   end of file
              n shft-G   move to line n

               /stuff/   search
                  n   repeat in same direction
                  N   repeat in opposite direction
                  /return  repeat seach forward
                  ?return  repeat seach backward

               "dyy  Yank current line to buffer d
               "a7yy Yank next 7 lines to buffer a
                    or
               :1,7ya a  Yank [ya] lines 1,7 to buffer a
               :1,7ya b  Yank [ya] lines 1,7 to buffer b

               :5 pu b   Put [pu] buffer b after line 5

               "dP   Put the content of buffer d before cursor
               "ap   Put the contents of buffer a after cursor

               :1,4 w! file2  Write lines 1,4 to file2
               :1,3

               :set nu     Display line numbers
               :set nonum  Turns off display

               :set ic     Ignore Case

               :e  Edit a file in a new buffer

               :g//p   Print matching regular expression

            vim
               :split
               :split 
               :sp 
               :split new

                   ctl-w   To move between windows
                   ctl-w+
                   ctl-w-  To change size
                   ctl+wv  Split windows vertically
                   ctl-wq  Close window

               :only       To view only 1 window

            vim dictionary - put the following command in ~/.vimrc

                   set dictionary+=/usr/share/dict/words
                   set thesaurus+=/usr/share/dict/words
              
               Now, after you type a word  and to 
               go back in the listing 

                   butter

           Scripting - you can script vi commands using ex. For example
               suppose you want to replace all occurrences of "one" with "two", then
               exit the file if there are changes. You would put the following in a file call script

               Contents of script

                   %s/one/two/g|x

               If you want to run this on all files with the patten "example*"

                   for i in $(ls example*); do ex - $i 

No comments: