Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Vi Editor in 100 minutes

A quick intoruction into the "vi" editor in 100 minutes.

  • Login to see the comments

Vi Editor in 100 minutes

  1. 1. 1 Viin 100 minutes. The Editor which no one knows how to use !
  2. 2. Auther: Ahmed Maklad 2 V.I.P ?  Vee eye
  3. 3. Auther: Ahmed Maklad 3 License Creative Commons You can use the material in this document for non-commercial purposes on the condition that you reference . you can’t modify the material without written approval from Ahmed Maklad ( first. This Document is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 3.0 Unsupported License.
  4. 4. Auther: Ahmed Maklad 4 How to use these slides ?  These Slides were designed to serve as a Tutorial in guided and unguided modes.  Each Slide attempts to answer the questions raised in the previous slide.  The slides could also be used as a Desktop Quick Reference. Please Remember to reference the Author and source of those slides : Ahmed Maklad
  5. 5. Auther: Ahmed Maklad 5 Disclaimer  All data and information provided on this site are for informational purposes only. Ahmed Makald, and sub-domains makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information in this document and will not be liable for any errors, omissions or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.
  6. 6. Auther: Ahmed Maklad 6 This session we discuss the following:  vi Modes  Syntax of vi commands  Examples of each command  Usability scenarios and how to use vi in real world Remember : you need to practice after wards on your own to memorize everything.
  7. 7. Auther: Ahmed Maklad 7 The Two Modes of VI  Command mode: The command mode allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes.  Insert mode :puts anything typed on the keyboard into the current file  VI starts in command mode.
  8. 8. Auther: Ahmed Maklad 8 Navigating the “modes” Command Mode Insert Mode ESC or control-[ a , A , i, o,O,r,C …etc ESC or control-[
  9. 9. Auther: Ahmed Maklad 9 Moving the cursor (command Mode) i j k h
  10. 10. Auther: Ahmed Maklad 10 Moving the cursor (command Mode)  Moving to end and beginning of a word WordWordWordWord eb
  11. 11. Auther: Ahmed Maklad 11 Moving the cursor (command Mode)  $ in Command mode moves to the end of current line.  0 or ^ moves to beginning of line  :$ moves to last line of file.  :0 moves to 1st line of file
  12. 12. Auther: Ahmed Maklad 12 Exiting vi  Before you learn how to fly you have to learn first how to land !  :q!  Quit/Exit WITHOUT saving any changes made.  :wq  Write then Exit/Quit.
  13. 13. Auther: Ahmed Maklad 13 1) The Insertion Mode: the append commands  a (lowercase) to append text AFTER the character under the cursor  A (Uppercase) appends to the end of the line.
  14. 14. Auther: Ahmed Maklad 14 the open (line) command  o to open a line BELOW the cursor and place you in insert mode.  To open up a line ABOVE the cursor, simply type a capital O , rather than a lowercase o.
  15. 15. Auther: Ahmed Maklad 15 the open (line) command Current line o O
  16. 16. Auther: Ahmed Maklad 16 To replace and change  cw To change part or all of a word.  C (shift-c) Change Rest of Line.  s Substitute chars.  S substitute lines (cc)  rx to replace One character under the cursor with new character x.  R overwrite mode, no insertion.
  17. 17. Auther: Ahmed Maklad 17 2) The Command Mode  The command mode commands are normally in this format: [count] command [where]  (Optional arguments are given in the brackets)
  18. 18. Auther: Ahmed Maklad 18 Command Mode (cont.) [count] command [where]  The count is entered as a number beginning with any character from 1 to 9. For example, the x command deletes a character under the cursor. If you type 23x while in command mode, it will delete 23 characters.  Some commands use an optional where parameter, where you can specify how many lines or how much of the document the command affects, the where parameter can also be any command that moves the cursor.
  19. 19. Auther: Ahmed Maklad 19 [count] command [where] d,r,c,s,y ..etc l line wword b back a word e end of word h one char back $ end of line ^ begin. of line … etc
  20. 20. Auther: Ahmed Maklad 20 text editing - deletion  While in Command Mode press x to delete the character under the cursor.
  21. 21. Auther: Ahmed Maklad 21 deletion commands  Type d$ to delete to the end of the line.  Type d^ to delete from beginning of line.  Type dw to delete to the end of a word.  Type db to delete a word backwards.  Type dL to delete all lines till end of file.
  22. 22. Auther: Ahmed Maklad 22 On commands and objects The format for the d delete command is as follows: [number] d object or d [number] object  number - is how many times to execute the command (optional, default=1).  d - is the command to delete.  object - is what the command will operate on (listed below). – w - from the cursor to the end of the word, including the space. – e - from the cursor to the end of the word, NOT including the space. – $ - from the cursor to the end of the line
  23. 23. Auther: Ahmed Maklad 23 an exception to 'command-object' dd to delete a whole line.  Due to the frequency of whole line deletion, the designers of VI decided it would be easier to simply type two d's in a row to delete a line. 2dd (remember number-command-object) to delete the two lines. D (shift-d) Deletes the rest of a line.
  24. 24. Auther: Ahmed Maklad 24 text editing - insertion  Press i and type in the necessary additions .
  25. 25. Auther: Ahmed Maklad 25 the undo command  u to undo the last command,  U to fix a whole line.  . (dot) repeats the last command you made
  26. 26. Auther: Ahmed Maklad 26 more changes using c (same as d) [number] c object or c [number] object  number - is how many times to execute the command (optional, default=1).  c - is the command to change.  object - is what the command will operate on (listed below). – w - from the cursor to the end of the word, including the space. – e - from the cursor to the end of the word, NOT including the space. – $ - from the cursor to the end of the line
  27. 27. Auther: Ahmed Maklad 27 Joining lines:  shift-J (capital J) Joins current line with next line. ------------------------------------------------------ Changing Case:  ~ changes the case of letter.  10~ changes case of 10 characters.
  28. 28. Auther: Ahmed Maklad 28 location and file status  CTRL-g to show your location in the file and the file status.  SHIFT-G to move to a line in the file.  Press shift-G to move you to the bottom of the file.  Type in the number of the line you want to go to then shift-G. (When you type in the numbers, they will NOT be displayed on the screen.)  :m places cursor at beginning of line m.  :set number to show the line numbers in vi.  :set nonumber to hide line numbers.
  29. 29. Auther: Ahmed Maklad 29 the searching for the needle  /pattern to search for the pattern downwards.  ?pattern to search for the pattern upwards.  n Find Next  N / shift-n Find Previous  To search for the same phrase again, simply type n . To search for the same phrase in the opposite direction, type Shift-N .  If you want to search for a phrase in the backwards direction, use the command ?pattern instead of /pattern.
  30. 30. Auther: Ahmed Maklad 30 More Search Tricks  /pat/+n nth line after pat  ?pat?-n nth line before pat  Simple search for one character in a line. – fx find next x – Fx find previous x – tx move to character following the next x – Tx move to character following the previous x – ; repeat last f, F, t, or T – , repeat inverse of last f, F, t, or T
  31. 31. Auther: Ahmed Maklad 31 Searching (Cont.) /pattern fx tx ?pattern Fx Tx Find Next : n Find Previous : N
  32. 32. Auther: Ahmed Maklad 32 How to describe a pattern : Special characters:  ^ Beginning of the line. (At the beginning of a search expression.)  . Matches a single character.  * Matches zero or more of the previous character.  $ End of the line (At the end of the search expression.)  [ – Starts a set of matching, or non-matching expressions... For example: /f[iae]t matches either of these: fit fat fet In this form, it matches anything except these: /a[^bcd] will not match any of these, but anything with an a and another letter: ab ac ad  < – Put in an expression escaped with the backslash to find the ending or beginning of a word. For example: /<the> should find only word the, but not words like these: there and other.  > – See the '<' character description above.
  33. 33. Auther: Ahmed Maklad 33 How to describe a pattern (Cont.): Special characters: This is a Text line. …………………. ^ $
  34. 34. Auther: Ahmed Maklad 34 Searching Examples (Cont.)  /[ab]cde  /[ab]*cde  /Hi  ~$  A[^aeoui]B  acde, bcde  acde, bcde, aaacde, bbbcde …etc  Hi  Empty line  A[anything but a vouel char]B  More comes in a next session for Regular Expression (RE).
  35. 35. Auther: Ahmed Maklad 35 a way to substitute patterns :s/old_pattern/new_replaced/  :s/old/new to substitute once 'new' for 'old' in the current line.  :s/old/new/g to substitute ALL 'new' for 'old' in the current line.  :%s/old/new/g to change every occurrence in the whole file.  :#,#s/old/new/g where #,# are the numbers of the two lines, To change every occurrence of a character string between two lines. (more on next slide)
  36. 36. Auther: Ahmed Maklad 36 More on :#,#s/old/new/g  .,$ current line to the end of file  5,. line 5 to the current line  1,$ line 1 to end of the file (entire file)  % all the lines in file (1,$)  .,.+5 current line to 5 lines down from current line (relative reference)  .-2,. 2 lines above current line to current line (relative reference)
  37. 37. Auther: Ahmed Maklad 37 More to do with searching  :g/pattern/d – Deletes every line with pattern.  :v/pattern/d – Deletes every line which doesn’t have the pattern.  :6& – Repeats last s/old/new command on line 6.  :10,16& – Repeats last s/old/new command on lines 10 to 16.
  38. 38. Auther: Ahmed Maklad 38 % matching parentheses () [] {} search %  use % to find a matching ),], or } – Place the cursor on any (, [, or { . – Now type the % character . – The cursor should be on the matching parenthesis or bracket . – Type % to move the cursor back to the first bracket (by matching).
  39. 39. Auther: Ahmed Maklad 39 More matching (helpful in code Navigation)  ]] next section/function  [[ previous section/function  ( beginning of sentence  ) end of sentence  { beginning of paragraph  } end of paragraph Nice for programmers !
  40. 40. Auther: Ahmed Maklad 40 writing files to disk  :w filename  :#,# w filename To save part of the file. where #,# are the two numbers (top,bottom) in your filename  :w >> filename – Append the contents of the buffer to the filename.  :w! Write the file even if read only.
  41. 41. Auther: Ahmed Maklad 41 retrieving and merging files :r filename To insert the contents of a file into the current cursor’s position.
  42. 42. Auther: Ahmed Maklad 42 Bookmarks / marking (command mode) You could mark a location with a letter and then when you want to move the cursor ‘ to that location you use the marker (the letter) to do so:  mx makes a mark called x(could be any other letter).  ma ,mb mc …. etc  ‘x goes to mark x  ‘a , ‘b ,’ac … etc
  43. 43. Auther: Ahmed Maklad 43 Marking (cont.)  You could use marks as a description for location in the file:  :‘a,’b w filename – writes a file with lines between markup a and b  :‘a,’bs/short/int/g – Replaces all short with int in the lines between marks a and b.
  44. 44. Auther: Ahmed Maklad 44 Buffers (vi clipboards)  VI has 36 buffers for storing pieces of text.  Those buffers have absolutely no relationship with the windows clipboard.  There are other buffers also for general purposes (delete and undo).  Any time a block of text is deleted or yanked from the file, it gets placed into the general purpose buffer  if it is specified. The buffer is specified using the “letter (quote) command, the letter has to be lower case [a-z] (i.e. 26 buffers).
  45. 45. Auther: Ahmed Maklad 45 Buffers  After typing "letter specifying the buffer to be used.  For example, the command: "mdd uses the buffer m, and the last two characters stand for delete current line.  vi saves the last 9 deleted buffers in cells numbering from 1-9. In order to recover the most recent delete use "1p or "1P command and so on. (i.e. 9 buffers)  9 (delete undo buffers) + 26 ([a-z] named buffers) = 36 buffers.
  46. 46. Auther: Ahmed Maklad 46 Pasting text  text can be pasted in with the p or P command.  "mp pastes the contents of buffer m after the current cursor position.
  47. 47. Auther: Ahmed Maklad 47 Cut  d^ – deletes from current cursor position to the beginning of the line.  d$ – deletes from current cursor position to the end of the line.  dw – deletes from current cursor position to the end of the word.  3dd – deletes three lines from current cursor position downwards.  .,$d – Deletes everything from current position till end of file.
  48. 48. Auther: Ahmed Maklad 48 Copy  y (yank) copies the text to the buffer.  yy copies the current line to buffer.  y^ – Yanks from current cursor position to the beginning of the line.  y$ – Yanks from current cursor position to the end of the line.  yw – yanks from current cursor position to the end of the word.  3yy – yanks three lines from current cursor position downwards.
  49. 49. Auther: Ahmed Maklad 49 Pasting p to put the last deletion after the cursor. 1. Move the cursor to the first line in the set below. 2. Type dd to delete the line and store it in VI's buffer. 3. Move the cursor to the line ABOVE where the deleted line should go. 4. While in Command Mode, type p to replace the line. 5. Repeat steps 2 through 4 to put all the lines in correct order. d) Can you learn too? b) Violets are blue, c) Intelligence is learned, a) Roses are red,
  50. 50. Auther: Ahmed Maklad 50 Pasting Cursor Position pP p (small) pasts buffer After Cursor Position P (Capital) pasts buffer Before Cursor Position
  51. 51. Auther: Ahmed Maklad 51 Pasting a whole Line: Cursor Position p P p (small) pasts buffer After Cursor Position P (Capital) pasts buffer Before Cursor Position
  52. 52. Auther: Ahmed Maklad 52 Another way to MOVE lines  :3m10 move line 3 to line 10. (cut’n’paste).  :3,10m20 move lines 3 to 10 to line 20
  53. 53. Auther: Ahmed Maklad 53 Yet another way to transfer lines.  :3t10 transfer line 3 to line 10. (copy’n’paste).  :3,10t20 transfer lines 3 to 10 to line 20  :+1,+4t10 1 line to 4 lines down the cursor’s position to be transferred to position 10 (relative positioning)
  54. 54. Auther: Ahmed Maklad 54 An easier way to use buffers markers to copy/cut and paste.  Got to beginning of the block you want to copy/delete.  Mark it with mletter  Go to the other end of the block and type y’letter.  Now you have yanked that block to the default buffer go to the desired location and paste the buffer with p or P.  The previous example uses the default buffer, but if you want to use named buffers: – "c2yy will yank 2 lines into the name buffer c. – “c3,10y will yank lines 3 to 10 to buffer c.
  55. 55. Auther: Ahmed Maklad 55 Copy’n’paste In Motion Block to be yanked Block to be yanked Block to be yanked ma y’a . . Insert Block below here Block to be yanked Block to be yanked Block to be yanked p
  56. 56. Auther: Ahmed Maklad 56 Cut’n’paste In Motion Block to be moved Block to be moved Block to be moved ma d’a . . Insert Block below here Block to be moved Block to be moved Block to be moved p
  57. 57. Auther: Ahmed Maklad 57 Copy’n’paste In a buffer other than default Block to be yanked Block to be yanked Block to be yanked ma y’a . . Insert Block below here Block to be yanked Block to be yanked Block to be yanked p “b “b It would have been a good practice to use the same buffer name as a the mark name
  58. 58. Auther: Ahmed Maklad 58 And yet another Copy’n’paste ! Using temporary files  :10,20 w temp_flname.txt – Writes the contents of lines 10 through 20 to a temporary file.  Move to the required position.  :r temp_flname.txt – Reads the contents of the temporary file to the cursor’s position.
  59. 59. Auther: Ahmed Maklad 59 execute a UNIX command from vi !  :!cmd – to execute the command.  :x,y!cmd – Execute a shell <cmd> [on lines x through y these lines will serve as input for <cmd> and will be replaced by its standard output.  :x,y!! arrgs – Repeat last shell command [and append <args>].  :r!cmd – Put the output of <cmd> onto a new line
  60. 60. Auther: Ahmed Maklad 60 More shell !  You shell out of vi, when you wish to execute more than one command. – :sh gives you a new shell, and when you finish with the shell, ending it by typing a ^D, the editor will clear the screen and continue.  :1,$!sort – This passes the file (between line 1 and last line) to the sort command and output (sorted) replaces the lines … nice for sorting file content, consider fmt, nroff ,cb , fold ,cut… etc
  61. 61. Auther: Ahmed Maklad 61 More to do with shell  :!spell % – The % passes the filename to the spell command so eventually it becomes :!spell filename . – This checks the spelling and displays a list of misspelled words at the bottom of screen.  :!spell % > %.sp – The misspelled words are in the file filename.sp .  :%!sort : (same as :.,$!sort ) – will pass the whole file to be sorted and replaced  :!sort % – Runs the command sort filename, display result and return to file without changing anything.
  62. 62. Auther: Ahmed Maklad 62 Fix text files from CR/LF windows characters  Sometimes when a text file is not properly transferred from windows to *NIX system, you see an extra line at the end of each line, which appears as “^M”.  To fix this you have to replace this CONTROL character in VI with nothing throughout the file, do the following:  :%s/[press CTRL-V][press CTRL-M]//g
  63. 63. Auther: Ahmed Maklad 63 Abbreviations  Another imported feature form ex to use abbreviations:  :ab Ex Example – While in Insert Mode every time you type Ex , immediately it converts into Example.  :una Ex removes the Abbreviation for Ex  :ab Lists all registered abbreviations.
  64. 64. Auther: Ahmed Maklad 64 mapping commands  You could also map commands in Command and Insert mode.  :map key command_sequence
  65. 65. Auther: Ahmed Maklad 65 Maintain your preferences 1. The $HOME/.exrc file is executed automatically every time vi starts. 2. Also the EXINIT environmental variable could be used to set the vi into certain behavior. – In .profile you could do the following: – export EXINIT – EXINIT='set ai nu wm=3|map g G'
  66. 66. Auther: Ahmed Maklad 66 Indention  :set sw=4 Sets the Indention to 4 spaces.  << Shifts the current line to the left by one shift width.  >> Shifts the current line to the right by one shift width.  4>> Indents 4 lines once to the left.  :set autoindent Very nice for our dear programmers.
  67. 67. Auther: Ahmed Maklad 67 set environment variable  :set ic Changes the environment so a search or substitute ignores case.  :set noic doesn’t ignore cases in searching.  Perform the following sequence on the file that contains multiple occurrences of Ignore, ignore and IGNORE. – /Ignore – n – :set ic – /ignore
  68. 68. Auther: Ahmed Maklad 68 Line Numbers  :set number  :set nonumber
  69. 69. Auther: Ahmed Maklad 69 Sources for Knowledge Exercises