Chapter 4

4. Advanced Text-Fu

This is the Fourth chapter for learning Linux on Let’s Learn Linux.

Navigate text like a Linux spider monkey with vim and emacs.

Subsections of 4. Advanced Text-Fu

1. regex (Regular Expressions)

Lesson Content

Regular expressions are a powerful tool to do pattern based selection. It uses special notations similar to those we’ve encountered already such as the * wildcard.

We’ll go through a couple of the most common regular expressions, these are almost universal with any programming language.

Well use this phrase as our test string:

sally sells seashells 
by the seashore
  1. Beginning of a line with ^
^by
would match the line "by the seashore"
  1. End of a line with $
seashore$ 
would match the line "by the seashore"
  1. Matching any single character with .
b. 
would match "by" 
  1. Bracket notation with [] and ()

This can be a little tricky, brackets allow us to specify characters found within the bracket.

d[iou]g
would match: dig, dog, dug

The previous anchor tag ^ when used in a bracket means anything except the characters within the bracket.

d[^i]g
would match: dog and dug but not dig

Brackets can also use ranges to increase the amount of characters you want to use.

d[a-c]g
will match patterns like dag, dbg, and dcg

Be careful though as brackets are case sensitive:

d[A-C]g
will match dAg, dBg and dCg but not dag, dbg and dcg

And those are some basic regular expressions.

Exercise

Try to combine regular expressions with grep and search through some files.

grep [regular expression here] [file]

Quiz Question

# What regular expression would you use to match a single character? > - Regular Expression, or regex or regexp in short, is extremely and amazingly powerful in searching and manipulating text strings, particularly in processing text files. One line of regex can easily replace several dozen lines of programming codes. > - Regex is supported in all the scripting languages (such as Perl, Python, PHP, and JavaScript); as well as general purpose programming languages such as Java; and even word processors such as Word for searching texts. Getting started with regex may not be easy due to its geeky syntax, but it is certainly worth the investment of your time. 1. [ ] - 2. [ ] | 3. [ ] , 4. [x] .

2. Text Editors

Lesson Content

If you get a couple of diehard Linux users in a room and ask them what is the best text editor to use, you’ll hear a never ending banter about the godliness of either vim or emacs. Don’t even try to bring up using a GUI editor if you value your life.

Vim and emacs are popular text editors that are installed by default on most Linux distributions and they both have their pros and cons. If you want to get around your system like a ninja, you’ll need to pick up one of these text editors to use. They are essentially coding, word document processing and basically all in one editors.

Exercise

Take a little tour of vim and emacs:

Quiz Question

No questions move along!

3. Vim (Vi Improved)

Lesson Content

Vim stands for vi (Improved) just like its name it stands for an improved version of the vi text editor command.

It’s super lightweight, opening and editing a file with vim is quick and easy. It’s also almost always available, if you booted up a random Linux distribution, chances are vim is installed by default.

To fire up vim just type:

$ vim

Exercise

No exercises for this lesson.

Quiz Question

No questions move along!

4. Vim Search Patterns

Lesson Content

To search for an expression just type the / key and then your search result while you are in a vim session. Once you hit enter, you can press “n” to go forward or “N” to go backward in your search results.

My pretty file is very pretty.

/pretty

will find the pretty words in the text file.

The ? search command will search the text file backwards, so in the previous example, the last pretty would come up first.

My pretty file is very pretty.

?pretty

will find the pretty words in the text file.

Exercise

Play with the search key, open a text file in vim with: vim [textfile] and start searching!

Quiz Question

# What key is used to search in vim? > It is possible to find the current word the cursor is currently on. Open up a file in normal mode. Put the cursor on a word, then press * to find the next occurrence and # for the previous occurrence. On the bottom left corner, we can see the word that is being searched. 1. [ ] \? 2. [ ] \< 3. [ ] \\ 4. [x] \/

5. Vim Navigation

Lesson Content

Now you may notice, the mouse is nowhere is use here. To navigate a text document in vim, use the following keys:

- h or the left arrow - will move you left one character 
- j or the up arrow - will move you up one line 
- k or the down arrow - will move you down one line 
- l or the right arrow - will move you right one character 

Exercise

No exercises for this lesson.

Quiz Question

# What letter is used to move down? > To perform multi-position navigation use number with these commands. For instance to navigate cursor 10 line below from current line, execute following command $ ``` 10j ``` You can use numbers with remaining commands as well. 1. [ ] D 2. [ ] S 3. [ ] Down Arrow key 4. [x] k

6. Vim Appending Text

Lesson Content

Now you may have noticed if you tried to type something you wouldn’t be able to. That’s because you are in command mode. This can get pretty confusing especially if you just want to open a file and enter text. The command mode is used for when you enter commands like h,j,k.l etc. To insert text you’ll need to enter insert mode first.

i - insert text before the cursor
O - insert text on the previous line
o - insert text on the next line
a - append text after the cursor
A - append text at the end of the line

Notice how when you type any of these insertion modes, you’ll see that vim has entered insert mode at the bottom of the shell. To exit insert mode and go back to command mode, just hit the Esc key.

Exercise

Play around with entering and exiting insertion mode.

Quiz Question

# What key is use to insert text before the cursor? > - The append command will put the cursor after the current position, while the insert command will put the cursor before it. using the append command is like moving the cursor one character to the right, and using the insert command. > - Using the insert command is like moving the cursor one character to the left, and using the append command. > You choose which depending on where you want to start typing. 1. [ ] A 2. [ ] o 3. [ ] \< 4. [x] i

7. Vim Editing

Lesson Content

Now that we have a couple of lines written, let’s edit it a bit more and remove some cruft.

x - used to cut the selected text also used for deleting characters
dd - used to delete the current line
y - yank or copy whatever is selected
yy - yank or copy the current line
p - paste the copied text before the cursor

Exercise

I know this lesson added some oddballs, open up a text editor and play around with these.

Quiz Question

# What character is used to delete an entire line? > Enter nd and hit Enter where n in nd is the line number. For example, if you want to delete the fifth line, then you should use ``` 5d ``` 1. [ ] x 2. [ ] del 3. [ ] yy 4. [x] dd

8. Vim Saving and Exiting

Lesson Content

Now that you’ve done your editing it’s time to actually save and quit out of vim:

  • :w - writes or saves the file

  • :q - quit out of vim

  • :wq - write and then quit

  • :q! - quit out of vim without saving the file

  • ZZ - equivalent of :wq, but one character faster

  • u - undo your last action

  • Ctrl-r - redo your last action

You may not think ZZ is necessary, but you’ll eventually see that your fingers may tend to lean towards this rather than :wq.

Whew that was a lot of information to take about Vim. Now that you know some basic commands and navigation, you can start editing some text files. There are many more options you can use in vim to increase your ability to master this text editor, head on to Vim’s online guide to take a look.

Exercise

No exercises for this lesson.

Quiz Question

# How do you quit out of vim without saving? > Remember Esc : As mentioned above, pressing Esc is a key step in exiting Vim. If you are having trouble exiting insert mode or entering commands, try pressing Esc first to return to normal mode. Use ``` :q ``` 1. [ ] :d 2. [ ] :x 3. [ ] :q 4. [x] :q!

9. Emacs

Lesson Content

Emacs is for users who want an extremely powerful text editor, which may be an understatement because you essentially live in emacs. You can do all your code editing, file manipulation, etc all within emacs. It’s a bit slower to load up and the learning curve is a bit steeper than vim, but if you want a powerful editor that is extremely extensible, this is the one for you. When I say extensible, I literally mean you can write up scripts for emacs that extend its functionality.

To start emacs just use:

emacs

You should be greeted with the default welcome buffer.

Buffers in emacs is what your text resides in. So if you open up a file, a buffer is used to store that file’s content. You can have multiple buffers open at the same time and you can easily switch between buffers.

Exercise

No exercises for this lesson.

Quiz Question

No questions move along!

10. Emacs Manipulate Files

Lesson Content

In a lot (if not all) of Emacs documentation, you will see the syntax C-[letter]. This just means hit the Ctrl-letter, but for shorthand purposes, we’ll call Ctrl with C. If you see syntax such as M-[letter], that means use the Meta key, most commonly the Alt key.

Saving files

C-x C-s - Save a file
C-x C-w - Save file as
C-x s - Save all

The save file options will prompt you if you want to save each file.

Opening a file

C-x C-f

This will prompt you to type a filename to open. If you do not have a file that already exists, it will create a new file. You can load up a directory as well.

Exercise

Play around with opening files and saving files.

Quiz Question

# What command is used to open a file? > Use Ctrl-x f to open a file from within Emacs. Create a new file in the same way as opening a file by specifying the new filename. The new file will not be saved unless specified. Save a file that is currently open by entering the Ctrl-x Ctrl-s command. 1. [ ] C-x s 2. [ ] C-x C-w 3. [ ] C-x C-s 4. [x] C-x C-f

11. Emacs Buffer Navigation

Lesson Content

To move around buffers (or files you’re visiting) use the following commands:

Switch buffers

C-x b - switch buffer
C-x right arrow - right-cycle through buffer
C-x left arrow - left-cycle through buffer

Close the buffer

C-x k

Split the current buffer

C-x 2

This allows you see multiple buffers on one screen. To move between these buffers use: C-x o

Set a single buffer as the current screen

C-x 1

If you ever used a terminal multiplexer like screen and tmux, the buffer commands will feel very familiar.

Exercise

Play around with buffers.

Quiz Question

# How do you kill a buffer? > - The text you are editing in Emacs resides in an object called a buffer. Each time you visit a file, a buffer is used to hold the file’s text. Each time you invoke Dired, a buffer is used to hold the directory listing. If you send a message with C-x m, a buffer is used to hold the text of the message. When you ask for a command’s documentation, that appears in a buffer named *Help*. > - Buffers exist as long as they are in use, and are deleted (“killed”) when no longer needed, either by you or by Emacs (e.g., when you exit Emacs, see Exiting Emacs). 1. [ ] C-x d 2. [ ] C-x x 3. [ ] C-x s 4. [x] C-x k

12. Emacs Editing

Lesson Content

Text Navigation

C-up arrow : move up one paragraph
C-down arrow: move down one paragraph
C-left arrow: move one word left
C-right arrow: move one word right
M-> : move to the end of the buffer

With text navigation, your regular text buttons work as they should, home, end, page up, page down and the arrow keys, etc.

Cutting and Pasting

To cut (kill) or paste (yank) in Emacs you’ll need to be able to select text first. To select text, move your cursor to where you want to cut or paste and hit

C-space key

then you can use the navigation keys to select the text you want. Now you can do the cut and paste like so:

C-w : cut
C-y : yank

Exercise

Play around with text navigation.

Quiz Question

# How do you move to the end of the buffer? > - The emacs editing mode is entered when you enable either the emacs or gmacs option. The only difference between these two modes is the way each handles the Ctrl-T edit command. > - To edit, move the cursor to the point needing correction and insert or delete characters or words, as needed. All of the editing commands are control characters or escape sequences. 1. [ ] K-> 2. [ ] E-> 3. [ ] N-> 4. [x] M->

13. Emacs Exiting and Help

Lesson Content

To close out of emacs

C-x C-c

If you have any open buffers, it will ask you to save it before closing out of emacs.

Confused?

C-h C-h : help menu

Undo

C-x u

As you can see Emacs has more moving parts, so the learning curve is a little steeper. In exchange though, you get a very powerful text editor.

Exercise

Visit the Emacs site to learn about more commands. Emacs

Quiz Question

# How do you access the help menu? > Try ```C-h C-h``` ( 'help-for-help' ) to access Emacs help. You can also use the 'Help' menu. The EmacsManual is available through 'Help' → 'Info' → 'Emacs' . The command names associated with the individual help keys can sometimes help you remember the keys themselves. 1. [ ] C-h 2. [ ] C-help 3. [ ] help 4. [x] C-h C-h