Your SlideShare is downloading. ×
08 qlx02vedit
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

08 qlx02vedit

36
views

Published on

Published in: Education, Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
36
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Unit 8Editing Files
  • 2. ObjectivesAfter completing this unit, you should be able to:• Determine the type of file using file• Edit text files with vi• Discuss other text file editors such as kedit• Discuss the ways non-text files can be edited
  • 3. Editing Files• Use file command to determine the content of a file$ file /etc/passwd /etc/passwd: ASCII text$ file /usr/bin/passwd/USR/BIN/PASSWD: ELF 32-BIT LSB EXECUTABLE•To edit text files, use an editor• Non-text files can only be changed using the applicationthat created them, or with a "hex editor"• But most configuration files under Linux are text files!
  • 4. The "vi" text editor• Default editor in all UNIX operating systems• Usually the only editor available in emergencies• Relatively hard to learn, but really powerful•As a Linux user, you should be able to use vi for basicediting tasks• BUT ITS OK IF YOU PREFER ANOTHER EDITOR FOR DAILYWORK•vi in Linux is usually vim (vi improved): •Syntaxhighlighting• Arrow keys, Del, BS work in insert mode• Multi level undo• Mouse support
  • 5. vi Modes•vi knows three modes of operation• Command mode (for simple, one-letter commands)• EDIT MODE (INSERT TEXT)• ex mode (for complicated commands)• Can easily change between modes
  • 6. Starting vi$ vi myfile.txt0,0-1All
  • 7. Cursor Movement in Command Modeh<left-arrow>Ak<up-arrow> 1GJ<down-arrow> Gl■><RIGHT-ARROW> $
  • 8. Editing Text in Command Mode•To delete a single character under cursor x•To delete a single character left of cursor X•To replace a single character r• Undo the last change u•To repeat last command .•To join two lines together Jcommand mode
  • 9. Switching to Edit Mode•To insert text at begin of line I•To insert text before cursor i•To append text after cursor a•To append text at end of line A•To go back to command mode <ESC>IIAACommand mode Edit modeESC
  • 10. Adding Text in Edit ModeThis file contains some lines.Line 2.The last line is line 5.Keystroke "i" switches vi to edit mode. New characters canbe inserted at the current position of the cursorINSERT 3,8All
  • 11. Exiting the Edit ModeThis file contains some lines.Line 2.And this for example is line 3.Line 4 follows line 3.The last line is line 5.~ ~ 3,8 AllKeystroke "ESC" LEAVES THE EDIT MODE.
  • 12. Searching for Patterns•To search for a pattern (in command mode): /<PATTERN> •Torepeat the previous search: nThis file contains some lines.Line 2.And that for example is line 3.Line 4 follows line 3.The last line is line 5.~ ~/line
  • 13. Replacing Patterns•Advanced search and replace can be done in ex mode: •Toreplace old with new: :1,$s /old/new/gThis file contains some lines.Line 2.And that for example is line 3.Line 4 follows line 3.The last line is line 5.~ ~: 1,$s/this/that/g
  • 14. Cut, Copy and Paste•To cut a whole line into buffer: dd•To copy a whole line into buffer: yy•To cut a word from the current cursorposition to its end: dw•To paste contents of buffers here: p•To cut, copy multiple lines, proceed command bynumber: 3dd, 8yy
  • 15. Cut and PasteThis file contains some lines.Line 2.Ar.d that for example is line 3.Line 4 follows line 3.The last line is line 5.cut line 3 by pressing "dd"This file contains some lines.Line 2.§NE 4 FOLLOWS LINE 3. E LAST LINEIS LINE 5. AND THAT FOR EXAMPLE ISLINE 3..AND INSERT IT after line 4 by pressing "p"
  • 16. Copy and PasteThis file contains some lines.Line 2.Ar.d that for example is line 3.Line 4 follows line 3.The last line is line 5.copy line 3 by pressing "yy"This file contains some lines.Line 2.And that for example is line 3§NE 4 FOLLOWS LINE 3. E LAST LINEIS LINE 5. AND THAT FOR EXAMPLE ISLINE 3..AND INSERT IT after line 5 by pressing "p"
  • 17. vi Options• Options entered in ex mode change the behavior of the vieditor: :set all:set autoindent/noautoindent:set number/nonumber :setlist/nolist :setshowmode/noshowmode :settabstop=x:set ignorecase/noignorecase:set wrapmargin=x :settx/notx:set hlsearch/nohlsearch:syntax on/off•To make this options available to all vi sessions, put it into a.EXRC or .VIMRC file in your HOME-directory
  • 18. Exiting vi•To save and exit in command mode ZZ•To save in ex mode :w•To forcefully save file in ex mode :w!•To quit without saving in ex mode :q•To forcefully exit in ex mode :q! •To save and exit inex mode (recommended) :wq•To save and exit in ex mode, shorter :x
  • 19. vi Cheat SheetCommand mode Edit modeTo edit modedd yy p Jdelete char, word search, repeatx X dw / Nex mode save&exit undo, redo: _ ZZ u .Ex modesearch and replace <ESC> or <Enter>:%s /old/new/gchange settings:setsave, exit:w :w! :q :q!:wq :xCan now typetext. Note: in vimarrow keys, Del,Backspace willwork!<ESC>
  • 20. Other Editors•A typical Linux distribution comes with a large number ofeditors. Examples:•Text mode editors pico (really simple) • Original viemacs (even more powerful and complicated than vi)• Graphical mode editorskedit, kwrite gedit• Hex editors allow you to change non-text files if you knowthe internal structurekhexedit
  • 21. CheckpointT/F 1. You need to learn vi because vi is the best editor forany job.2. What does the file command do?a. It looks at the extension to determine the type of file.b. It looks at the first few characters of the file andcompares this to a database of known file types.c. It asks the kernel for information about the file.d. It makes a wild guess.3. What is a hex editor?
  • 22. Unit Summary•The most common editor on any UNIX is vi•vi has three modes of operation: command mode, editmode and ex mode•vi makes a copy of the file you are editing in an editbuffer. The contents are not changed until you save thechanges•A typical Linux distribution comes with a lot of othereditors as well