Presentation is loading. Please wait.

Presentation is loading. Please wait.

Vi/vim/gvim Training Judy Ghirardelli NWS/OST/MDL July 29, 2004 Last Update: September 15, 2004.

Similar presentations


Presentation on theme: "Vi/vim/gvim Training Judy Ghirardelli NWS/OST/MDL July 29, 2004 Last Update: September 15, 2004."— Presentation transcript:

1 Vi/vim/gvim Training Judy Ghirardelli NWS/OST/MDL July 29, 2004 Last Update: September 15, 2004

2 What is vi? Vim? Gvim? vi is a standard editor available on UNIX and LINUX platforms (and others) vi is a standard editor available on UNIX and LINUX platforms (and others) vim is “vi improved” and offers additional features above vi vim is “vi improved” and offers additional features above vi gvim is “graphical vi improved”, offers additional features above vi and vim, is GUI based, and is more user friendly than either vi or vim gvim is “graphical vi improved”, offers additional features above vi and vim, is GUI based, and is more user friendly than either vi or vim

3 Simple Vi tricks view view –opens the file for vi’ing in Read-Only mode. This is safer and should be used whenever you are merely looking at, and not planning on changing, a file. Moving around faster: Moving around faster: –:Nmove to the Nth line :0 move to the first line (go to the top of the file) :0 move to the first line (go to the top of the file) :50move to the 50th line :50move to the 50th line –Gmove to the bottom of the file –wmove to the next word beginning –b move to the previous word beginning –CTRL-fmoves forward a page –CTRL-bmoves backward a page

4 Simple Vi tricks Changing text Changing text –r replaces current character with the character you type after the “r”. Note this leaves you in command mode. –R Replaces from the current character onward. Note this leaves you in insert mode until you hit. –dwdeletes the current word –cwchanges the current word. It will put you in inset mode and show $ at the end of the current word. Type the replacement text, and hit when you are done. This will insert the relacement text between the beginning of the current word and the  $ . The size of the replacement text does not need to equal or be less than the size of the word that you replaced. –cw is particularly useful with find and repeat.

5 Simple Vi tricks uundos last command uundos last command Repeating commands: Repeating commands: –Place a number in front of a standard command: 5jmoves down 5 lines 5jmoves down 5 lines 100kmoves up 100 lines 100kmoves up 100 lines 45dddeletes 45 lines (the one you’re on and the next 44) 45dddeletes 45 lines (the one you’re on and the next 44) 3xdeletes 3 characters (the one you’re on and the next 2) 3xdeletes 3 characters (the one you’re on and the next 2)

6 Simple Vi tricks :r This imports the named file onto the line following the cursor. The file is inserted, so the original file below the cursor is moved to be following the inserted file. :r This imports the named file onto the line following the cursor. The file is inserted, so the original file below the cursor is moved to be following the inserted file. :! Escape to the shell and issue a command :! Escape to the shell and issue a command :r! Insert results from shell command into file :r! Insert results from shell command into file. (period)This will repeat the last issued command.. (period)This will repeat the last issued command. CTRL-gGives information about the file at the bottom of the screen. It will indicate the filename, what line number the cursor is on out of how many lines total in the file, and the percent of the way through the file that the cursor is. CTRL-gGives information about the file at the bottom of the screen. It will indicate the filename, what line number the cursor is on out of how many lines total in the file, and the percent of the way through the file that the cursor is.

7 Useful (but still simple) Vi tricks Replace ALL instances of with in the file. Replace ALL instances of with in the file. –:g/ /s// /g To replace ALL instances of with nothing, in effect removing all instances of old pattern, do: To replace ALL instances of with nothing, in effect removing all instances of old pattern, do: –:g/ /s///g Alternate syntax: Alternate syntax: –:%s/ / /g

8 Useful (but still simple) Vi tricks Filtering commands Filtering commands –Place cursor at text that contains UNIX command. Do !!sh to process the command and replace the command with the result. –Other Examples: 6!!sortsorts the next 6 lines starting at the cursor 6!!sortsorts the next 6 lines starting at the cursor !Gsortsorts all the lines from the current line to the end of the file !Gsortsorts all the lines from the current line to the end of the file !Gcut –c 55-93 cuts the lines from the current line to the end of the file !Gcut –c 55-93 cuts the lines from the current line to the end of the file !Gawk ‘{print $1}’returns the first field from the current line to the end of the file !Gawk ‘{print $1}’returns the first field from the current line to the end of the file

9 Not as simple, but VERY Useful Vi tricks Marking text: Marking text: –mjputs a marker of “j” at the place where the cursor is. You will not see the j. You could mark with any letter (e.g., ma, md, mq). This is used to mark the END of a block for copying or cutting and then pasting. –‘j(Single quote and then the marker letter). moves the cursor to the marker j. Could be useful for moving around a large document. –d’jdeletes from the cursor to the marker j. Puts contents into the anonymous buffer. –y’jcopies from the cursor to the marker j. Puts contents into the anonymous buffer. –pputs contents from the anonymous buffer after the line the cursor is on –Pputs contents from the anonymous buffer before the line the cursor is on

10 Not as simple, but VERY Useful Vi tricks Using Macros: Using Macros: –To set a single key to perform a number of keys in a certain sequence, you can use a macro in vi. Do: –:map lhs rhs where lhs = the name of the macro that you'll use to execute it. It could be v or something. Beware - this will let you overwrite current key values. I prefer to call my macros #n, where n = 0-9. #1 is the F1 key, etc. where lhs = the name of the macro that you'll use to execute it. It could be v or something. Beware - this will let you overwrite current key values. I prefer to call my macros #n, where n = 0-9. #1 is the F1 key, etc. Rhs = the vi commands you want to issue. Note you must “escape” the escape key and the enter key if they are part of your sequence of commands. Escape them by putting CTRL-v before you hit Esc or Enter. Rhs = the vi commands you want to issue. Note you must “escape” the escape key and the enter key if they are part of your sequence of commands. Escape them by putting CTRL-v before you hit Esc or Enter. If you called the lhs #1, then the F1 key will perform this macro. If you called the lhs #1, then the F1 key will perform this macro. Note – unsure if this works successfully in dtterms. Works with xterms, hpterms, and aixterms. Note – unsure if this works successfully in dtterms. Works with xterms, hpterms, and aixterms. –Con: you cannot do N to perform the macro N times. (But you CAN hold the macro key down to do the macro quickly repeatedly.)

11 Setting up vim and gvim Note – different versions of vim and gvim on tyr and IBM. Vim and gvim are NOT available on the hp’s. Note – different versions of vim and gvim on tyr and IBM. Vim and gvim are NOT available on the hp’s. Location: Location: –/usrx/local/vim.6.2/bin on snow and frost –/usr/bin/vim on tyr Add the location to your PATH in your.profile/.cshrc Add the location to your PATH in your.profile/.cshrc Place a copy of.vimrc and.gvimrc in your home directory. You can copy mine: Place a copy of.vimrc and.gvimrc in your home directory. You can copy mine: –/u/we24jg/.vimrc and /u/we24jg/.gvimrc (snow/frost) –/home/ghirarde/.vimrc and /home/ghirarde/.gvimrc (tyr) Or you can get a standard ‘example’ Or you can get a standard ‘example’ –On IBM, examples are in /usrx/local/vim.6.2/share/vim/vim62 gvimrc_example.vim vimrc_example.vim gvimrc_example.vim vimrc_example.vim –tyr? I believe you need BOTH the.gvimrc and.vimrc files to use gvim. I believe you need BOTH the.gvimrc and.vimrc files to use gvim.

12 Setting up vim and gvim On IBM On IBM –Once you place the.vimrc in your home directory, your.exrc will no longer automatically be sourced. You can source it in your.vimrc by adding :source ~/.exrc to your.vimrc, or you can add what you want from your.exrc to your.vimrc add :set dir=/nfstmp to your.vimrc add :set dir=/nfstmp to your.vimrc –Note that the setting of set ll=21000000 in the.exrc causes a problem for vim/gvim. Error detected while processing /u/we24jg/.exrc: line 4: E518: Unknown option: ll=21000000 Hit ENTER or type command to continue – Therefore I’d suggest adding what you want from your.exrc (except the ll setting) to your.vimrc file.

13 Setting up vim and gvim Sample.vimrc Sample.vimrc :hi Normal ctermfg=white ctermbg=darkgrey :syntax on :set ignorecase :set showmatch :set splitright :set ruler :set number :set hlsearch :set dir=/nfstmp

14 Setting up vim and gvim Sample.Gvimrc Sample.Gvimrc

15 Setting up vim and gvim On IBM On IBM –To allow the color coding of the text for codes, you need to issue vim/gvim from a terminal that supports color. The tyr xterms do, and the IBM aixterms do. The IBM xterms do not. –The IBM aixterm however will not restore your terminal colors after exiting from vim. –To deal with this, I’ve set my vim colors such that I like them for terminal colors also.

16 Examples of colors on tyr

17

18

19 Examples of colors on IBM

20

21

22 Setting up your colors Vim – in.vimrc Vim – in.vimrc –One way (this is what I do): :hi Normal ctermfg=white ctermbg=darkgrey :hi Normal ctermfg=white ctermbg=darkgrey :syntax on :syntax on –Or: :colorscheme :colorscheme :syntax on :syntax on

23 Setting up your colors In.gvimrc: In.gvimrc: –Manipulate color controls to your liking: “ Set nice colors " background for normal text is light grey " Text below the last line is darker grey " Cursor is green, Cyan when ":lmap" mappings are active " Constants are not underlined but have a slightly lighter background highlight Normal guibg=grey90 highlight Cursor guibg=Green guifg=NONE highlight lCursor guibg=Cyan guifg=NONE highlight NonText guibg=grey80 highlight Constant gui=NONE guibg=grey95 highlight Special gui=NONE guibg=grey95 On top menu bar: (Note this is only temporary for current session) On top menu bar: (Note this is only temporary for current session) –Edit  Color Scheme  pick one you like In.gvimrc: In.gvimrc: Comment out color controls (put “ in front on lines in yellow above) Comment out color controls (put “ in front on lines in yellow above) Add colors colorscheme below the color controls (e.g., colors elflord) (This is what I do) Add colors colorscheme below the color controls (e.g., colors elflord) (This is what I do)

24 Getting started To start vim: To start vim: –vim –vim filename –vim –R filename (read only) To start gvim: To start gvim: –gvim –gvim filename –gvim –R filename (read only)

25 Useful.vimrc settings :map lhs rhs :map lhs rhs –Sets your macros :hi Normal ctermfg=white ctermbg=darkgrey :hi Normal ctermfg=white ctermbg=darkgrey –Sets the highlighting such that the foreground is white, and the background is darkgrey (almost black) :colorscheme schemename :colorscheme schemename –Color scheme names found in /usrx/local/vim.6.2/share/vim/vim62/colors on IBM :syntax on :syntax on –Allows for color coding based on syntax. Color coding will work for codes such as scripts, FORTRAN, C, etc. See /usrx/local/vim.6.2/share/vim/vim62/syntax (IBM) for languages provided for. Some examples are: FORTRAN, c, crontab,gnuplot, html, java, javascript, make, mysql, php, perl, python, xhtml, many shell scripts. See /usrx/local/vim.6.2/share/vim/vim62/syntax (IBM) for languages provided for. Some examples are: FORTRAN, c, crontab,gnuplot, html, java, javascript, make, mysql, php, perl, python, xhtml, many shell scripts. –This should follow any color setting you do (like :hi ctermfg= or :set foreground=) :set ignorecase :set ignorecase –Ignore the case when searching for a pattern :set showmatch :set showmatch –When a bracket is inserted, briefly jump to the matching one.

26 Useful.vimrc settings :set ruler :set ruler –Shows at the bottom of the window the column and line of the cursor location, and the relative position in the file :set number :set number –Shows line numbers to the left of the lines. :set nonumber turns this off. :set hlsearch :set hlsearch –Highlights patterns you are searching for :set dir=/nfstmp :set dir=/nfstmp –Sets temporary directory (important on IBM) :set pdev=hp10428 :set pdev=hp10428 –Sets default printer :set shows your settings :set shows your settings

27 Using help In vim: In vim: –:help In gvim: In gvim: –:help or click on the ? on the menu bar Or :help Or :help –:help print –:help diff –:help record Maneuvering around in help: Maneuvering around in help: –Search works –Vim: Move cursor so it is within bars |topic|. CTRL-] takes you into the topic. CTRL-T or CTRL-O takes you back. Move cursor so it is within bars |topic|. CTRL-] takes you into the topic. CTRL-T or CTRL-O takes you back. –Gvim: As with vim, OR by using mouse (:set mouse=a must be set). Left doubleclick within bars |topic| to go to topic. CTRL-rightclick to return. As with vim, OR by using mouse (:set mouse=a must be set). Left doubleclick within bars |topic| to go to topic. CTRL-rightclick to return.

28 Using help Website: Website: –http://www.vim.org/ http://www.vim.org/ –Useful tips on first page –http://www.dbaclick.com/ftp/documents/lin ux/HOW-TO/Vim-HOWTO.pdf http://www.dbaclick.com/ftp/documents/lin ux/HOW-TO/Vim-HOWTO.pdfhttp://www.dbaclick.com/ftp/documents/lin ux/HOW-TO/Vim-HOWTO.pdf –Manual style reference – VERY useful

29 “ Vim/gvim – it’s not just pretty colors… ” - Marc Saccucci

30 Useful vim/gvim features Color syntax - examples Color syntax - examples –If you have hlsearch set, it might get annoying that your last searched for text STAYS highlighted until your next search. Add this to your.vimrc: :map #3 :let @/ = “ and hit F3 will clear your search field

31 Useful vim/gvim features Repositioning - in command mode: Repositioning - in command mode: ztredraw, current line at top of window ztredraw, current line at top of window zzredraw, current line at center of window zzredraw, current line at center of window zbredraw, current line at bottom of window zbredraw, current line at bottom of window Undo/redo Undo/redo –Virtually unlimited undo (u) and redo (CTRL- r) –Default number of undo/redos = 1000 (?)

32 Useful vim/gvim features Recording Recording –Records keystrokes, including corrections –Can do N number of playbacks (500@q) –Will always stop when an error is encountered –Works WITH macros (or without)

33 Useful vim/gvim features Recording Recording –Details q{a-z} (e.g., qq) starts recording into the q register q{a-z} (e.g., qq) starts recording into the q register q{A-Z} (e.q., qQ) appends to the q register q{A-Z} (e.q., qQ) appends to the q register q stops recording q stops recording @{a-z} replays the register (e.g., @q) @{a-z} replays the register (e.g., @q) @@ replays the LAST register replayed @@ replays the LAST register replayed 5000@q replays the q register 5000 times 5000@q replays the q register 5000 times Your registered keystrokes can be as complicated as you want! Your registered keystrokes can be as complicated as you want!

34 Useful vim/gvim features Recording – Simple Example Recording – Simple Example –Do this in a text file: qq (starts recording the q register) qq (starts recording the q register) 0 (go to the beginning of the line) 0 (go to the beginning of the line) dw (delete the first word) dw (delete the first word) j (go down one line to the next line) j (go down one line to the next line) q (stop recording to the q register) q (stop recording to the q register) @q (replay the q register, which will delete the first word of the current line and move you down one line) @q (replay the q register, which will delete the first word of the current line and move you down one line) 1000@q (will do this record to the next 1000 lines if you have that many in your file) 1000@q (will do this record to the next 1000 lines if you have that many in your file)

35 Useful vim/gvim features Differencing files Differencing files –gvimdiff file1 file2 (IBM) –vimdiff file1 file2 (IBM) –vim –d file1 file2 (tyr) –gvim –d file1 file2 (tyr) –Gvim File  Open  select file1 File  Open  select file1 File  Split Diff with  select file2 File  Split Diff with  select file2

36 Useful vim/gvim features Differencing files Differencing files –Shows differences line by line, in sync –“folds” lines that are not different, showing lines around those that are different for context –zi toggles the folding on and off –Clicking on the + in gvimdiff will expand the folded lines; clicking on the – will fold them again –]c jump to the next difference –[c jump to the previous difference

37 Useful vim/gvim features Merging files Merging files –Bring up the differences (gvimdiff, etc.) –Put your cursor on the difference to put or get to the other file To move it to the other file do: To move it to the other file do: –dp short for :diffput To move the other files lines to current file: To move the other files lines to current file: –:diffget dg was already taken –:diffget dg was already taken Can also specify a range of lines: Can also specify a range of lines: –:{range}diffput –:{range}diffget –Save file (perhaps with different name)

38 Useful vim/gvim features Visual blocking Visual blocking –In gvim only (I think) –Highlight area, hit CTRL-v; the block will be highlighted –Perform operations on the block –VERY useful for moving/deleting/copying columns, adding 3 spaces to every line in a block, changing the same columns of every line, etc.

39 Useful vim/gvim features Visual blocking operations: Visual blocking operations: –~Toggles the lower/upper case of letters –ddeletes the block – you can then “put” it –cc changes the block to –cc changes the block to –yyanks a block –!Runs the block through an external cmd (filter) –rr replaces all characters in block with –rr replaces all characters in block with –ss changes block to –ss changes block to –II Block insert – inserts text at beginning of block –AA Block append – appends text at end of block

40 Misc. Differencing directories Differencing directories –Use emacs


Download ppt "Vi/vim/gvim Training Judy Ghirardelli NWS/OST/MDL July 29, 2004 Last Update: September 15, 2004."

Similar presentations


Ads by Google