5. Text Editors

As a developer, you will edit text files on a regular basis. This can sometimes pose a challenge because Linux offers a variety of editors. In many cases, you should be able to pick your editor, but sometimes you might be forced to use one of the standard editors, such as the vi editor.

This chapter focuses on the editors that are available on Linux distributions. The primary focus is on the vi (or vim) editor because it has the advantage of always being available on whatever Linux distribution you are working on. I introduce some additional editors to help you determine which tool will work best for your situation.

The vi Editor

Consider the early days of Unix, the precursor to Linux: A developer would sit down at a keyboard, ready to edit a program that he is working on. He stares at the printer (yes, printer, not monitor) considering what commands to execute. Monitors were very rare in the early 1970s and even if a developer had one, it was primarily designed to display the output of executed code, not to interactively edit files.

Instead, the developer would use a simple command-based editor, such as the ed editor. With this editor a developer could perform operations such as list the contents of a file (print the file), modify specific characters of a file, or save the contents of a file. However, he accomplished all this in a way that might seem foreign today. The developer wouldn’t see what he was editing, but rather just assume the commands were successful (or print the file to verify).1

When monitors became more commonplace, the ed editor seemed like a clumsy way to edit a text file. In the mid-1970s a replacement editor named vi (short for visual) was introduced to Unix.2 It was a great improvement over the ed editor3 because you could actually see and move around in your document.

Why Learn vi?

You will soon discover that some editors are easier to use than vi. This discovery will result in your asking the question, “Why the heck should I bother learning the vi editor?” Several good reasons exist, even if you never plan on using the vi editor on a daily basis:

vi requires no GUI: Many of the editing tools that this chapter introduces require a graphical user interface. Normally that is not a problem, but on many servers, the GUI isn’t even installed because it tends to be a resource (CPU, memory, and hard drive space) hog. If you might be called upon to edit code on a server, then you might need to know a command-line editor like the vi editor.

vi is a very stable standard: You could take a cryogenically frozen developer from the 1970s who used the vi editor, unfreeze him today and he could edit files using a modern vi editor. Certainly, features have been added to the vi editor since the 1970s, but the core functionality of vi does not change, making it very easy for you to use throughout your career without having to “relearn” how newer versions work.

vi is always there: In every Linux distribution (and all Unix ones as well) the vi editor is there. After you know how to edit files with the vi editor, you can edit files on any Linux machine.4

Speed of use: Although you might not believe this initially, you can actually edit files quickly using the vi editor. Granted, this will take many years of practice, but because you never need to use the mouse, you never need to take your hands off your keyboard when editing a file. The commands are also very short, so you can edit files very quickly.

What Is vim?

The vim editor5 was released in 1991 as a clone of the vi editor. The vim editor has the same base functionality as the vi editor, but it has several additional features. Some of these features can be useful for software developers.

Your distribution might only have the vi editor. Many distributions have both the vi and vim editor. On some distributions, the command vi is actually a link to the vim editor.

An easy way to tell whether you are using the vi or vim editor is to try executing the vi command. If you are using vim, a message like the following appears: VIM – Vi IMproved. If you don’t get this message, then you are using a standard vi editor.6


Note:

Unless stated otherwise, the commands shown in this chapter work in both the vi and vim editors. Any command that only works in vim is denoted as such.


Essential vi Commands

To become an expert vi user can take a lot of practice, but to be able to effectively edit files requires the knowledge of a subset of the large amount of vi commands.

It helps to have a large file to edit. All Linux distributions should come with a /etc/services file that is typically thousands of lines long. You can start by first copying this file to your home directory and then edit the copy with the vi command:

[student@fedora ~]$ cp /etc/services.
[student@fedora ~]$ vi services

Entering Insert Mode

When you first start the vi editor, you are placed in the command mode. This mode allows you to perform commands such as moving around the screen, copying text, and deleting text.

While in the command mode, you can’t insert new text into your document because all the keyboard keys are assigned to command tasks. To insert new text, you must use the s command to move from command mode to insert mode. These commands include the following:

i—New text will appear before the cursor position.

a—New text will appear after the cursor position.

I—New text will appear at the beginning of the line.

A—New text will appear at the end of the line.

o—Opens a new line below the line that contains the cursor; new text will be placed on this line.

O—Opens a new line above the line that contains the cursor; new text will be placed on this line.

See Figure 5.1 for a visual description of how these vi commands work.

Figure 5.1 vi commands to enter insert mode

Note that when you are using the vim editor and enter the insert mode, the bottom part of the screen changes to indicate this. See the -- INSERT -- at the bottom of Figure 5.2.

Figure 5.2 The insert mode

If you are working in a standard vi editor, then -- INSERT -- does not appear at the bottom of the screen by default. To enable this feature in a standard vi editor, type the following command while in command mode:

:set showmode

When you want to return to command mode, just press the escape (Esc) key. This is normally denoted in documentation as <ESC>. If you return to the command mode, then the -- INSERT -- should disappear from the bottom of the screen.

Movement Commands

While in the command mode, you can move the cursor in your document by using a variety of keys. One of the common methods is to move the cursor one character to the left or right or one line up or down. You do this by using either the arrow keys on your keyboard or using the h, j, k, and l keys7 as shown in Figure 5.3.

Figure 5.3 vi movement commands

Many additional movement commands are available, including the following:

$ Move to the last column (character) on the current line

0 Move to the first column (character) on the current line

w Move to the beginning of the next word or punctuation mark

W Move to past the next space

b Move to the beginning of the previous word’s punctuation mark

B Move to the beginning of the previous word, ignoring punctuation

e Move to the end of next word or punctuation mark

E Move to the end of next word, ignoring punctuation

) Move forward one sentence

( Move back one sentence

} Move forward one paragraph

{ Move back one paragraph

H Move to the top of the screen

M Move to the middle of the screen

L Move to the bottom of the screen

[[ Move to the beginning of the document

]] Move to the end of the document

G Move to the end of the document (same as ]])

xG Move to line x (you can also use :x)

Note that these are just some of the movement commands. Here’s a suggestion: spend some time trying out these movement commands and then create a cheat sheet of the commands that you feel will be most useful to you. Make this a cheat sheet that you can add more commands to as you learn additional useful commands.8

Repeater Modifiers

In the previous section, you discovered that you can jump to a specific line in a document by typing a number followed by a G while you are in the command-mode. For example, the command 7G will take you to line number 7 in the document.

Placing a number before a command acts as a modifier. You can use modifiers on many different command-mode commands; for example:

3w Move forward three words

5i Insert something five times9

4( Move back three paragraphs

You can also use repeat modifiers on commands like deleting, copying, and pasting. Typically, if there is a command-mode command that you would logically want to execute multiple times, then repeat modifiers should work with that command.


Are You Trying These Commands?

I had you copy the /etc/services file to your home directory so you can try the commands out. Remember, if you get stuck in insert mode, just press the Esc key to return to command mode.

Don’t worry if you end up with a big mess in this file. It is just a practice file and you are about to learn how to correct mistakes.


Undoing

You can undo whatever change has been made to the document by typing the u character in command mode. In the standard vi editor, you can only undo a single action;10 in fact, the u command acts as an undo/redo key.

If you are using the vim editor, you can undo several actions. Just keep pressing the u character to undo older modifications. You can perform a redo that undoes the changes performed by the undo command by using the ^r (Ctrl+r) command.

Suppose you made a large number of changes since you opened the document and you want to discard them all. In this case, you probably want to close the document without saving and open it again. To close the document without saving changes, type the command :q!. You can find more on this command and other ways to quit the vi editor in the “Saving and Quitting” section later in this chapter.

Copying, Deleting, and Pasting

The following is a summary of commonly used copying commands. Keep in mind that you should execute these while in command mode:

yw—Copy word. This actually copies from the current character in a word until the end of the word (including punctuation) and the white space after the word. So, if your cursor was on the h in “this is fun,” the cw command would copy “his ” into memory.

yy—Copy current line.

y$—Copy from current character to end of the line.

yG—Copy current line to the end of the document.

You might be wondering “Why use the y character?” This is because the process of copying text into the memory buffer used to be called yanking.

The following is a summary of commonly used deleting commands. Keep in mind, you should execute these while in command mode:11

dw—Delete word; this command actually deletes from the current character in the word until the end of the word (including punctuation) and the white space after the word. So, if your cursor was on the h in “this is fun” the dw command would delete “his ”, resulting in “tis fun.”

dd—Delete current line.

d$—Delete from current character to end of the line.

dG—Delete current line to the end of the document.12

x—Delete the character the cursor is currently on (like a delete key).13

X—Delete the character before the character the cursor is currently on (like a backspace key).


Where Are the Cut Commands?

When you use a delete command, the text that was deleted is placed into the copy buffer. As a result, no need exists for a separate set of cut commands.


Pasting commands can be a bit trickier because how they work depends on what you are pasting. For example, suppose you had copied a word into the buffer. In this case, the following describes how the paste commands would work:

p—Pastes the buffer contents before the cursor

P—Pastes the buffer contents after the cursor

The behavior is a bit different if you copy an entire line (or multiple lines) into the buffer:

p—Pastes the buffer contents in the line above the cursor

P—Pastes the buffer contents in the line below the cursor

Finding Text

Finding text is a critical function for a software developer who is using the vi editor because error messages that appear when code is executed often include bits of code where the error occurs. You can search for text by using one of the following methods:

/ While in command mode, type the / key; this character appears in the bottom-left corner of the terminal window. Now type what you want to search for and then press the Enter key. The vi editor will search forward in the document for the text you asked it to search for.

? While in command mode, type the ? key; this character appears in the bottom-left corner of the terminal window. Now type what you want to search for and then press the Enter key. The vi editor will search backward in the document for the text you asked it to search for.

Suppose your search didn’t find the specific match you were looking for. You can use the n command to find the next match. The n command searches forward when your last search started with the / key and searches backward when your last search started with the ? key.14

What if you searched for “/one” and realize you will need to press the n key many times to find what you are looking for? After furiously typing n repeatedly, you realize you went past the match that you wanted. To reverse the current search, use the N character (capital N rather than lowercase). When you’re searching forward, N will reverse and search backward in the document. When you’re searching backward, N will reverse and search forward in the document.


Case Sensitive

As with just about everything you use in Linux, the search function is case sensitive. In other words, a search of /the will not match the following line:

The end is near


Searching and Replacing

To search for text and replace it with other text, use the following format:

:x,ys/pattern/replace/

The values of x and y represent which lines you want to perform the search on. For example, to search and replace on only the first ten lines of the document, use the following syntax:

:1,10s/I/we/

You can use the $ character to represent the last line in the document:

:300,$s/I/we/

So, to perform the substitution on the entire document, use the following:

:1,$s/I/we/

By default, only the first match on each line is replaced. Imagine if the line you are searching for and replacing looked like the following:

The dog ate the dog food from the dog bowl

If the command :s/dog/cat/ were executed on the previous line, then the result would be:

The cat ate the dog food from the dog bowl

To replace all occurrences on a line, add a g15 at the end of the search command:

:s/dog/cat/g

Searching and replacing is case sensitive. Imagine if the line you are searching and replacing looked like the following:

The Dog ate the dog food from the dog bowl

If the command :s/dog/cat/ were executed on the previous line, then the result would be:

The Dog ate the cat food from the dog bowl

The result matched the second dog because the first one had a capital D character. To perform a case-insensitive search and replace, add an i at the end of the search command:

:s/dog/cat/i

Saving and Quitting

In the previous section, you typed a : character to perform a search and replace operation. Complex commands are performed in the last line mode, also called the ex mode in honor of the ex editor. The : character takes you to the bottom of the screen where the command appears as you type it.

Another operation you can do in this mode is save and quit your document:

:wq


Note

You must save before quitting, so you can’t execute the command :qw because that will attempt to quit and then save.


You might also want to just save, but continue working:

:w

You can also save to a different document, but there is a little gotcha to this operation that you will discover. Suppose you want to save changes made to your services file into a file called myservices. Execute the following:

:s myservices

The changes will be placed into this file. However, any further changes will be saved by default into the original services file. Most modern editors “switch” the default save document to whatever the last saved document was, but vi doesn’t do this. To see your current document, type ^G (Ctrl+g).

So, if you want to edit the new file, you should quit the vi editor and open the new file.

If you make changes to a file and then try to quit without saving (:q), you receive an error message like the following:

E37: No write since last change (add ! to override)

To force quit (quit without saving changes), execute the following command:

:q!

Expanding Your vi Knowledge

Although the discussion has covered many vi commands, it really just scratches the surface. The vi editor is a very powerful tool with hundreds of commands. In addition, it provides some very advanced features, such as syntax highlighting, the capability to create macros, features for editing multiple files at the same time, and much more.

The vim editor has some very useful built-in documentation, but you must have a specific software package installed on Red Hat–based distributions16 in order to be able to access this documentation. Chapter 6 covers installing software in greater detail. For now, just make sure you are logged in as the root user and execute the following command: yum install vim-enhanced

If this package is installed, you can execute the command :help while in the vim editor to see a help document. See Figure 5.4 for an example.

Figure 5.4 vim help output

Use your arrow keys (or h, j, k, and l) to scroll through the document. About 20 lines down, you will start to see some subtopics, as shown in Figure 5.5.

Figure 5.5 vim help topics

Each of these topics, such as quickref and usr_01.txt, are separate help topics. To view them, first exit from the current help document by typing the :q command. Then type a command like the following, replacing topic with the full name of the topic you want to view:

:help topic

For example, to see help on “Using syntax highlighting,” type the following command:

:help usr_06.txt


vimtutor

A great tool to help you learn the vi editor is the vimtutor command. This command takes you into vi and provides a useful guide to learning the vim editor.


Additional Editors

A large number of editors are available that you can use in Linux. The focus of this section is to make you aware of these editors, not to teach you how to use them.


Note

It is likely that not all of these editors will be installed on your distribution. You might need to install additional software packages to have access to them.


Emacs

Like the vi editor, the Emacs editor was developed in the mid-1970s.17 Linux users who like Emacs will praise how easy it is to use and how customizable it is. If you launch Emacs (just run the emacs command) while in a GUI-based terminal, it should open a GUI-based version of the program as shown in Figure 5.6. As you can see, the GUI-based version has menus in addition to the commands that you can execute via the keyboard.

Figure 5.6 GUI-based Emacs

If you execute the Emacs editor in a command line–only environment, the editor will look like Figure 5.7.

Figure 5.7 Text-based Emacs


GUI vim?

If you install the vim-X11 software package, you get access to a GUI-based version of the vim editor. Just execute gvim or vim -g on Red Hat–based distributions or vim-gtk on Debian-based distributions.


gedit and kwrite

These editors are fairly standard GUI-based editors (gedit comes with GNOME and kwrite comes with KDE). If you are used to Notepad on Microsoft Windows, then you will find these editors fairly easy to use (although somewhat limited).

The gedit editor typically is installed on distributions that use the GNOME desktop by default. The kwrite (or KATE) editor typically is installed on distributions that use the KDE desktop by default. However, you can easily install gedit on a system that uses KDE desktop or install kwrite on a GNOME desktop system.

nano and joe

The vi and emacs editors are extremely powerful. In some cases, you might find that you want to just use a simple edit in a command-line environment. The gedit and kwrite editors only work in GUI-based environments. The nano editor is also typically installed by default on most Linux distros.

The nano and joe editors provide a simple interface for editing text files. They are command line–only editors, so no GUI is required. See Figure 5.8 for an example of the nano editor.

Figure 5.8 The nano editor

lime and bluefish

The lime and bluefish editors take the process of editing a text file to the next level by providing tools and features that are designed to help a developer create code. These tools provide features like syntax highlighting, insertion of code (by clicking a button), and automatic formatting (like automatically indenting code).

If you are going to make a career of coding on Linux, you should start to explore these tools (or many of the other similar editing tools). See Figure 5.9 for an example of the bluefish editor.

Figure 5.9 The bluefish editor


Linux Humor

Try this one in your shell:

bo@mintos:~ > cd /

bo@mintos:/ > touch me

touch: cannot touch 'me': Permission denied


Summary

The focus of this chapter was editors, primarily the vi or vim editor. We covered the basics of editing files in vi/vim, including how to start the editor, the three modes of operation, how to add and delete text, and some more advanced features. You were also introduced to some additional editors that are available in Linux. At this point, using these editors comes down to practice, which you should get plenty of when you start writing code on Linux.

1 On some modern Linux distributions, the ed editor still exists. Try typing the command ed in a terminal window. If the editor does exist on your system, you will be placed in the ed editor environment (essentially a blank prompt). In reality, the only ed command you want to know is q because this quits the ed editor.

2 By the way, vi is pronounced vee-eye, not a single syllable vi.

3 Technically, it was an improvement of the ex editor, which was an improvement of the em editor, which as an improvement of the ed editor. Use this Unix trivia to entertain your friends at your next party. On second thought, maybe just keep this nugget to yourself.

4 Although removing the vi editor is possible, I’ve never heard of any distribution that has done so.

5 vim = a contraction of Vi IMproved

6 To exit vi/vim, type :q and then press the Enter key.

7 Why use the h, j, k, and l keys when you can use the arrow keys? When the vi editor was developed, most keyboards didn’t have arrow keys. Even today, rack-mounted servers are sometimes connected to dump terminals that have keyboards that lack arrow keys.

8 Or visit the “Vi lovers” page (http://thomer.com/vi/vi.html) and download one of the reference cards under the “Vi pages/manuals/tutorials” section. Personally, I prefer to make my own because the commands that someone else finds useful, I might not find very helpful.

9 Be careful here. I once accidently typed the 8 key twice before pressing the i key to enter insert mode. After typing pages of text, I pressed the Esc key and received 88 times the text that I typed. Read on to learn how I fixed this problem quickly.

10 Actually, if you don’t move to another line, you can undo all changes on the current line by typing the u character while in command mode. However, I think you will find that you’ll rarely use this feature because the moment you move to a new line, these undos are lost.

11 Notice how similar these deleting commands are to the copying commands. Not only did this allow me to avoid a lot of extra typing in this book (call me lazy, or just an effective developer who “reuses” good “code”), but it highlights that most of the copying commands are just like deleting commands, which should make the process of learning them easier.

12 In a previous footnote, I mentioned that I once accidently inserted several pages of text 88 times. To fix this, I went to the first line in the second “set” and typed the dG command. This deleted all but the first copy of what I had typed with minimal effort.

13 If you are using the vim editor, you can use the delete key to delete the character you are currently on. However, the backspace key works like a back arrow key.

14 Remember the regular expressions that were covered in the section on the grep command in Chapter 4? If not, go back and read that section again! Regular expressions show up in many tools in Linux, including the vi editor. For example, if you search for ^The, the vi editor will only match lines that begin with The, rather than The anywhere on the line.

15 You can think of g standing for get them all. It actually stands for global.

16 The help feature comes with vim by default on Debian-based distributions.

17 Emacs versus vi/vim has been referred to as a “religious war” between the users of these editors. It is a war that I’ve always avoided; be aware that there are some in the Linux community who take this war very seriously.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.117.231