Operations and Administration By Ahmad AlRjoub [email protected] Chapter Four Creating and Editing Files with Text Editors Objectives

• Describe key features of GUI and command-line text editors available in Linux • Use the vim editor to create and edit text files

Linux Operations and Administration 2 Text Editors in Linux

– Program used to create and edit plain text files – Not same as word processor – Main purpose: to create a file to be used by another program, for example: • Hypertext Markup Language (HTML) for a Web browser • Source code that a compiler can process • Shell script – Text file containing a sequence of commands

Linux Operations and Administration 3 Text Editors in Linux (cont’d.)

• Two types of text editors: – Command-line editors – GUI editors

Linux Operations and Administration 4 GUI Text Editors in Linux

• Linux GUI text editors – Similar to Notepad in Windows • Advantage: – Select and edit text quickly with the mouse • Widely used GUI text editors: – KWrite – Gedit

Linux Operations and Administration 5 KWrite: A GUI Text Editor for KDE

• Also called programmer’s editor for the K Desktop Environment • To start: – Type kwrite at a command prompt • Opens a new empty file in Kwrite – Use Kickoff Application Launcher button • Advanced features: – to display text in different colors and fonts for programming languages – Bookmarks are markers placed on certain lines to help navigate through a text file Linux Operations and Administration 6 KWrite: A GUI Text Editor for KDE (cont’d.)

Figure 4-1 KDE’s KWrite text editor ©Cengage Learning 2013 Linux Operations and Administration 7 KWrite: A GUI Text Editor for KDE (cont’d.) • Activity 4-1: Exploring the KWrite Text Editor – Explore features of the KWrite text editor

Linux Operations and Administration 8 Gedit: A GUI Text Editor for GNOME

• GUI text editor included with GNOME • To start – Open a terminal window, type gedit, and press Enter • Syntax highlighting options – Plain text – Sources – Scripts – Others – Markup – Scientific

Linux Operations and Administration 9 Gedit: A GUI Text Editor for GNOME (cont’d.)

Figure 4-2 GNOME’s gedit text editor ©Cengage Learning 2013

Linux Operations and Administration 10 Gedit: A GUI Text Editor for GNOME (cont’d.) • Activity 4-2: Exploring the Gedit Text Editor – Learn features of the Gedit text editor

Linux Operations and Administration 11 Getting Started with the Vim Editor

command-line text editor – Included with most versions of and Linux – Learning curve – Knowing how to use it is crucial • vi has evolved into many different forms • vim – Stands for “vi improved” – vi command is now linked to the vim command

Linux Operations and Administration 12 Getting Started with the Vim Editor (cont’d.) • Modular editor – Runs in different operational modes – Command mode • Use key combinations as commands instead of typing text – Insert mode • Typed text is displayed onscreen – Extend mode • Used for more advanced commands, such as saving files, exiting vim, or searching and replacing text

Linux Operations and Administration 13 Starting Vim

• Type vim and press Enter • Type vim filename and press Enter – If the file doesn’t exist, vim creates an empty file with this filename • Figure 4-3 – Window displayed after you start vim – Tilde (~) • Represents blank or empty lines in the file

Linux Operations and Administration 14 Starting Vim (cont’d.)

Figure 4-3 The vim text editor ©Cengage Learning 2013

Linux Operations and Administration 15 Command Mode

• Command mode – Chosen automatically when starting vim • Table 4-1 – Advanced navigational commands in vim

Linux Operations and Administration 16 Table 4-1 Advanced vim navigational commands

Linux Operations and Administration 17 Table 4-1 Advanced vim navigational commands (continued )

Linux Operations and Administration 18 Command Mode (cont’d.)

• Activity 4-3: Using Vim in Command Mode – Use navigational commands in vim

Linux Operations and Administration 19 Insert Mode

• Must be in insert mode to type text • Change to insert mode with one of the commands in Table 4-2 • vim editor displays -- INSERT -- at the lower left – To indicate insert mode • Press the Esc key to change back to command mode

Linux Operations and Administration 20 Insert Mode (cont’d.)

Table 4-2 Commands for entering insert mode

Linux Operations and Administration 21 Insert Mode (cont’d.)

Figure 4-4 The vim editor in insert mode ©Cengage Learning 2013 Linux Operations and Administration 22 Modifying Text

• Modify text using the keyboard instead of the mouse • Table 4-3 – Commands for modifying text in vim – Must be in command mode • Activity 4-4: Using Vim in Insert Mode – Change to insert mode and modify text in vim

Linux Operations and Administration 23 Modifying Text (cont’d.)

Table 4-3 Commands for modifying text

Linux Operations and Administration 24 Deleting Text

• Table 4-4 – Lists commands for deleting text in vim

Linux Operations and Administration 25 Deleting Text (cont’d.)

Table 4-4 Commands for deleting text

Linux Operations and Administration 26 Cut, Yank, and Paste

• Cut text – Store text in a buffer • Paste text – Move from buffer to cursor location • Yank (copy) text – Keep in current location and copy to buffer • Table 4-5 – Commands used to yank and paste text in vim

Linux Operations and Administration 27 Cut, Yank, and Paste (cont’d.)

Table 4-5 Commands for yanking and pasting text

Linux Operations and Administration 28 Undo Commands

• Undo changes – Type u – Keep typing u until you have reached your oldest change • Activity 4-5: Modifying Text in Vim – Delete, cut, copy, and paste text in vim

Linux Operations and Administration 29 Extended Mode

• Offers advanced features – Save and quit – Search and replace – Customization options • Enter extended mode – Type the : symbol • Table 4-6 – Describes commands for saving files and exiting vim

Linux Operations and Administration 30 Extended Mode (cont’d.)

Table 4-6 Extended mode commands for saving and exiting

Linux Operations and Administration 31 Extended Mode (cont’d.)

Table 4-7 Extended mode commands to search for and replace text

Linux Operations and Administration 32 Useful Commands

• Table 4-8 – Commands for extended mode • Activity 4-6: Using Vim in Extended Mode – Use vim commands in extended mode

Linux Operations and Administration 33 Useful Commands (cont’d.)

Table 4-8 Additional extended mode commands

Linux Operations and Administration 34 Summary

• Linux GUI text editors – Easy to use – Comparable with Windows Notepad • vim text editor – Available with every major distribution of UNIX and Linux – Operates in several different modes • Insert mode • Command mode • Extended mode

Linux Operations and Administration 35 Summary (cont’d.)

– Command-line text editor – Use a variety of keys and key combinations in vim to perform operations

Linux Operations and Administration 36