04. Text Editing using vi (Part 2)

Topics:
  • Substitute text
  • Copy and paste, cut and paste
  • Move existing text
  • Repeat, undo, and redo
  • Administrative commands



Commands to Substitute Text
  • The command to search and replace text is called the substitute command. It will search for a text string, and when it finds a match, will remove the text string and add the replacement text string.
  • You need to be in command mode to run the substitute command, and you stay in command mode after substitute runs.
  • Format:     :addresss/search_text/replacement_text/g        where address and /g are optional, but the colon :  and everything else is required
  • Example    :s/linux/LINUX       
    • substitute will find the first match of linux on the current line and replace it with LINUX
    • If there is no match, then nothing will change on the current line
    • If there are multiple linux on the current line, only the first one will get substituted
  • To substitute multiple instances of a text string on the same line, you need to use the option /g (for global)
    • Example     :s/linux/LINUX/g      will substitute all instances of linux on the  current line with LINUX
  • If there is no address field, the substitute command will work on the current line only.
  • The address field specifies which line(s) substitute will work with:
    • One line number :5s/linux/LINUX   substitute on line 5
    • Range of lines :2,8s/linux/LINUX   substitute from lines 2 to 8,   inclusive
    • Last line :$s/linux/LINUX       substitute on last line
    • Current line :.s/linux/LINUX   substitute on current line     (note the  .   used for current line) 
    • Entire file :%s/linux/LINUX      substitute on entire file
    • Range calculation   :.,.+2s/linux/LINUX  substitute from current line to 2 lines after current line
    •          :.-10,.+8s/linux/LINUX    substitute from 10 lines before current line to 8 lines after current line

Commands to Copy and Paste
  • Copy and paste takes 3 steps, just like with any other editor: copy the text, move the cursor to the appropriate location, paste the text at the cursor.
  • You need to be in command mode to run the copy and paste commands, and you stay in command mode after they run
  • To copy text:
    • Copy current word:   yw   (for yank word)
    • Copy current line:     yy
  • The copy command copies the text into a temporary buffer.  If there is already text in the buffer, it will be overwritten.
  • To move the cursor, use any of the move commands
  • To paste text:
    • Before the cursor:    P    (for put, uppercase)
    • After the cursor:       p     (lowercase)
  • The text copied into the buffer can be pasted over and over again, as long as the buffer is not overwritten

Commands to Cut and Paste
  • Cut and paste takes 3 steps, just like with any other editor: cut the text, move the cursor to the appropriate location, paste the text at the cursor.
  • You need to be in command mode to run the cut and paste commands, and you stay in command mode after they run
  • To cut text:
    • Cut current word:   dw   (same command to delete word)
    • Cut current line:     dd
  • The delete command copies the text into a temporary buffer.  If there is already text in the buffer, it will be overwritten.
  • To move the cursor, use any of the move commands
  • To paste text:
    • Before the cursor:    P    (for put, uppercase)
    • After the cursor:       p     (lowercase)  
  • The text in the buffer can be pasted over and over again, as long as the temporary buffer is not overwritten

Commands to Move Existing Text
  • You need to be in command mode for these commands and you stay in command mode after they run
  • Command to join 2 lines of text:   J  (uppercase)                                 J will take the line below the current line and append it to the current line
  • Command to insert text from another file    :r  filename
    • Note the :  in front, r is for read
    • This command will copy the file filename, and insert the copied text after the current line

Commands to Repeat An Action
  • You need to be in command mode to run these commands and you stay in command mode after they run
  • To repeat the previous command:    .   (a period or dot)                     This command will not repeat a move, search, or substitute command
  • To have vi run a command more than once:    nCommand where n is the number of time you want vi to run Command
  • Example:    
    • dd           delete the current line
    • 8dd         run delete line command 8 times, which results in deleting 8 lines, starting from the current line
    • j       move cursor down 1 line
    • 20j       move cursor down 20 lines

Commands to Undo and Redo
  • You need to be in command mode to run these commands and you stay in command mode after they run
    • To undo:   u
  • You can keep undoing until the last time you save the file
    • To redo:    control-r

Administrative Commands
  • To get the status of the current file, such as filename, how many lines of text, etc.       :f    (for file)
  • To temporarily bring up a shell     :sh
    • The shell prompt will appear and you can run any shell command. When done, type exit at the shell prompt and you will get back to the same vi screen from before
  • Line numbering:
    • To show line numbers for the lines in the text file    :set number
    • To show no line number    :set nonumber
    • Note that the line number shown is for the display window of vi only. No line number is added to the text file itself.
    • The default is no line number. Setting or not setting the line number applies only to the current vi session. 
    • To save the line numbering setting from session to session, use vi to create a text file named .exrc. Add in this file the line         set number and then save the file.  Line numbering will take place in the next vi session

Swap File
  • When you use vi to open any text file, for example the file fileA, vi creates a temporary file with the name .fileA.swp. This called a swap file.
  • It is this swap file that you change if you make any change to fileA.
  • When you quit out of vi, vi copies .fileA.swp and overwrites the original fileA, and then deletes .fileA.swp
  • If you did not quit out of vi by using a quit command, then vi does not know to overwrite the original file and delete the swap file, so the swap file remains in your directory.
  • This means that when you use vi to open fileA the next time, vi will alert you that the swap file is a more recent version of fileA.  At this point, you have a choice to use the swap file or to use the original version. If you do not want to be alerted again about the swap file, make sure you delete it.

Comments