| 
 | 1 | +# Editing, Finding and Searching Text Files  | 
 | 2 | +## Table of Contents:  | 
 | 3 | + | 
 | 4 | +## Intro:  | 
 | 5 | +- This document will tackle three basic topics:  | 
 | 6 | +	- Editing text files with non-graphical text editors, specifically **`vi`** and its fancier sibling **`vim`**.  | 
 | 7 | +	- Finding files in a system or in specific directories based on some criteria such as filename, size, ownership, modification date, etc.   | 
 | 8 | +	- Searching files for certain patterns, words, etc.   | 
 | 9 | + | 
 | 10 | +## Editing Text Files with `vi` and `vim`:  | 
 | 11 | +- ***I've spend some time with `vim` elsewhere, so I might go light on some topic, or I might go deep on this and just tackle my `vim` knowledge for good!!***  | 
 | 12 | + | 
 | 13 | +### Using `vi`:  | 
 | 14 | +- When you first enter `vi` or `vim`, you can't probably do anything apart from navigating an existing document you've opened with the command, the way you'd navigate a `man` page. By the way **`vim`** stands for **`vim` improved**. `vi` doesn't have cool stuff like syntax coloring and visual highlighting. The latter seems indispensable to me!  | 
 | 15 | +- What sets `vi` from graphical text editors most of us noobs are used to such as a *Sublime Text* and *VS Code* is that it has two operating modes: **command mode** and **input mode**. In graphical text editors these two modes are mashed together so you can navigate the document as insert text into the file all at the same time. We will learn about these operating modes.   | 
 | 16 | +- When you first open vim, you are by default in the command mode. In this mode, clicking on a key such as `v` doesn't print a v into the screen but highlights something on the screen. Commands are case sensitive also. To input text into the document, you need to issue some command that tell the program to allow you to insert text into the file.  | 
 | 17 | + | 
 | 18 | +#### Adding Text:  | 
 | 19 | +- Learn all these and don't restrict yourself to `i`. For many months, I entered insert mode, move one character at a time to the end of the line, then pressed Enter to add a new line!! This is extremely stupid considering that it can be done with just pressing `o`.  | 
 | 20 | + | 
 | 21 | +| Command | Action |  | 
 | 22 | +| --- | --- |  | 
 | 23 | +| **`i`** | Inserts text that starts to the left of the cursor. |  | 
 | 24 | +| **`I`** | Inserts at the beginning of the current line. |  | 
 | 25 | +| **`a`** | Add text that starts to the right of the cursor. |  | 
 | 26 | +| **`A`** | Append text at the end of the end of current line. |  | 
 | 27 | +| **`o`** | Open below. Opens a line below the current line and puts you in insert mode. |  | 
 | 28 | +| **`O`** | Open above. Opens a line above the current line and puts you in insert mode. |  | 
 | 29 | + | 
 | 30 | +- When done with inserting text, press the Esc key. It's great to map the caps lock and turn it into an Esc key.  | 
 | 31 | +- By the way, to get out of the _ex mode_ (the one you get into from command mode by typing `:` to do things like `:q`, etc.), you press Esc twice.  | 
 | 32 | + | 
 | 33 | +#### Moving around in Text:  | 
 | 34 | +- Some of these are really really important. Others are great if you can remember them.   | 
 | 35 | + | 
 | 36 | +| Command | Action |  | 
 | 37 | +| --- | --- |  | 
 | 38 | +| **`Arrow keys`** | Move cursor left, right, up, down one character at a time. |  | 
 | 39 | +| **`h`** | Move left one char at a time. |  | 
 | 40 | +| **`l`** | Move right one char at a time. |  | 
 | 41 | +| **`j`** | Move down one char at a time. |  | 
 | 42 | +| **`k`** | Move up one char at a time. |  | 
 | 43 | +| **Spacebar** | Move forward one char at a time. |  | 
 | 44 | +| **`Backspace`** | Move back one char at a time. |  | 
 | 45 | +| **`w`** | Move to the beginning of the next word (delimited by spaces, tabs and punctuation). |  | 
 | 46 | +| **`W`** | Move to the beginning of the next word (delimited by spaces and tabs only). |  | 
 | 47 | +| **`b`** | Move to the beginning of the previous word (delimited by spaces, tabs and punctuation). |  | 
 | 48 | +| **`B`** | Move to the beginning of the previous word (delimited by spaces and tabs only). | |  | 
 | 49 | +| **`0`** (zero) | Move to the beginning of the current line |  | 
 | 50 | +| **`^`** | Move to the beginning of the current line (Less convenient than `0`). |  | 
 | 51 | +| **`$`** | Move to the end of the current line |  | 
 | 52 | +| **`H`** | Moves cursor to upper left corner of the screen (not necessarily top of file). |  | 
 | 53 | +| **`M`** | Moves cursor to the first character of the middle line in the screen. |  | 
 | 54 | +| **`L`** | Moves cursor to the lower left corner of the screen. |  | 
 | 55 | + | 
 | 56 | +#### Deleting, Copying and Changing Text:  | 
 | 57 | +- **`x`**, **`y`**, **`d`**, and **`c`** along with their capitalized counterparts can be used to delete, copy and edit characters, words or lines or larger chunks of text when used either by themselves or in combination with other keys. The following table shows how they are used in general:  | 
 | 58 | + | 
 | 59 | +| Command | Action |  | 
 | 60 | +| --- | --- |  | 
 | 61 | +| **`x`** | Deletes character under cursor. |  | 
 | 62 | +| **`X`** | Deletes character before the cursor. |  | 
 | 63 | +| **`d<?>`** | Deletes some text. |  | 
 | 64 | +| **`c<?>`** | Changes some text. Basically it erases some text and p |  | 
 | 65 | +| **`y<?>`** | Yanks (copies) some text. |  | 
 | 66 | + | 
 | 67 | +- The commands above (those with the placeholder **`<?>`**)can be combined with movement commands to have more control over what you wanna edit, delete or yank as the following table show:  | 
 | 68 | + | 
 | 69 | +| Command | Action |  | 
 | 70 | +| --- | --- |  | 
 | 71 | +| **`dw`** | Delete word after the cursor |  | 
 | 72 | +| **`db`** | Delete word after the cursor |  | 
 | 73 | +| **`dd`** | Delete current line |  | 
 | 74 | +| **`d$`** | Delete characters from cursor to end of line. |  | 
 | 75 | +| **`d0`** | Delete characters from cursor to the beginning of line. |  | 
 | 76 | +| **`c$`** | Change (erase) characters from current to end of line and put you in insert mode. |  | 
 | 77 | +| **`c0`** | Change (erase) characters from current to end of line and put you in insert mode. |  | 
 | 78 | +| **`cl`** | Erase current character and go into insert mode. |  | 
 | 79 | +| **`cc`** | Erase current line and go into insert mode. |  | 
 | 80 | +| **`yy`** | Yank the current line into the buffer*??!?!!* |  | 
 | 81 | +| **`y)`** | Copy the current sentence to the right of the cursor into the buffer. |  | 
 | 82 | +| **`y}`** | Copy the current paragraph to the right of the cursor into the buffer. |  | 
 | 83 | + | 
 | 84 | + | 
 | 85 | +#### Movement and Editing a Number of Times with Using Numbers:  | 
 | 86 | +- You can combine the commands we've seen so far (for movement, editing, copying and deleting) with numbers to do the given action for a number of words, lines or characters, etc. The following table provides some examples:  | 
 | 87 | + | 
 | 88 | +| Example | Action |  | 
 | 89 | +| --- | --- |  | 
 | 90 | +| **`3dd`** | Delete next 3 lines, starting with the current one. |  | 
 | 91 | +| **`3dw`** | Delete next 3 words. |  | 
 | 92 | +| **`5cl`** | Erase next five characters and enter insert mode. |  | 
 | 93 | +| **`12j`** | Move down 12 lines. |  | 
 | 94 | +| **`5cw`** | Erase 5 next words and put you in insert mode. |  | 
 | 95 | +| **`4y)`** | Copy next 4 sentences cursor. |  | 
 | 96 | + | 
 | 97 | +#### Pasting (Putting) Text:  | 
 | 98 | +- When you copy text into the buffer (by deleting, copying it or changing it using any of these commands or their variations `c`, `d`, `x`, `y`). You can paste (put) this text into the file with one of the following commands:  | 
 | 99 | + | 
 | 100 | +| Command | Action |  | 
 | 101 | +| --- | --- |  | 
 | 102 | +| **`P`** | Put text to the left of cursor if it were chars and words; above current line if it were a line. |  | 
 | 103 | +| **`p`** | Put text to the right of cursor if it were chars and words; below current line if it were a line. |  | 
 | 104 | + | 
 | 105 | +#### Repeating Commands:  | 
 | 106 | +- The dot command (**`.`**) allows you to repeat the last command in somehow an intelligent way. For example, if you search and then change the word `class` to `function` using `cw`, you can go to another occurrence of the word `class` using the command **`n`**, place cursor at its beginning, press the dot command and the word would be changed to `function`. You can keep doing this to the following occurrences of the world `class` by alternating `n` and `.` ***AMAZING!!!!!!***  | 
 | 107 | + | 
 | 108 | +#### Exiting `vi`:  | 
 | 109 | + | 
 | 110 | +| Command | Action |  | 
 | 111 | +| --- | --- |  | 
 | 112 | +| **`ZZ`** | Save changes and exit. |  | 
 | 113 | +| **`:w`** | Save changes without exiting. |  | 
 | 114 | +| **`:wq`** | Save changes and exit, same as `ZZ`. |  | 
 | 115 | +| **`:q`** | Exit (possible when no changes have been made). |  | 
 | 116 | +| **`:q!`** | Exit without saving changes. |  | 
 | 117 | + | 
 | 118 | +#### Other Useful Commands and Tips:  | 
 | 119 | + | 
 | 120 | +| Command | Action |  | 
 | 121 | +| --- | --- |  | 
 | 122 | +| **`u`** | Undo last change. |  | 
 | 123 | +| **`CTRL+R`** | Redo last undone change. |  | 
 | 124 | +| **`:!command`** | Allows you run a shell command while you are editing something in `vim`. *AMAZINGGG!!!*, maybe list current directory files or see date or `sed` something!!! To go back to `vim`, press Enter. You can also launch a shell from this command, then when done, exit that shell to come back. Save your work, though, before going into a new shell, because you might forget to come back to `vim`. |  | 
 | 125 | +| **`CTRL+G`** | Prints the name of the current file and current you're editing at the bottom of the screen. It also prints the number of lines in the file, percentage of where you are in line, and column where cursor is. This is helpful after breaks and whatnot! |  | 
 | 126 | + | 
 | 127 | +### Skipping around in the File:  | 
 | 128 | +- The following pages allow you to move more efficiently in large files that cover more than what can be displayed at once on the screen:  | 
 | 129 | + | 
 | 130 | +| Command | Action |  | 
 | 131 | +| --- | --- |  | 
 | 132 | +| **`CTRL+f`** | Move ahead one page at time. |  | 
 | 133 | +| **`CTRL+b`** | Move backward one page at a time. |  | 
 | 134 | +| **`CTRL+d`** | Move ahead half a page at a time. |  | 
 | 135 | +| **`CTRL+u`** | Move back half a page at a time. |  | 
 | 136 | +| **`G`** | Go to the last line of the file. |  | 
 | 137 | +| **`1G`** | Go to first line of the file. |  | 
 | 138 | +| **`gg`** | Go to first line of the file. |  | 
 | 139 | +| **`35G`** | Go to line number 35. |  | 
 | 140 | + | 
 | 141 | +### Searching for Text:  | 
 | 142 | +- ***This is the part I had very little knowledge about***.   | 
 | 143 | +- to Search a file, you'd use either:  | 
 | 144 | + | 
 | 145 | +| Command | Action |  | 
 | 146 | +| --- | --- |  | 
 | 147 | +| **`/`** | Search forward relative to the cursor current position. |  | 
 | 148 | +| **`?`** | Search backward relative to the current cursor position. |  | 
 | 149 | + | 
 | 150 | +- Examples include:  | 
 | 151 | + | 
 | 152 | +| Example |  | 
 | 153 | +| --- | --- |  | 
 | 154 | +| **`/Ahmed`** |  | 
 | 155 | +| **`?Saqqa`** |  | 
 | 156 | +| **`/S*ra`** |  | 
 | 157 | +| **`?[Pp]resident`** |  | 
 | 158 | + | 
 | 159 | +- After typing the search term, press enter. To continue searching for other occurrences of the same pattern use:  | 
 | 160 | + | 
 | 161 | +| Command | Action |  | 
 | 162 | +| --- | --- |  | 
 | 163 | +| **`n`** | Keep searching in the same direction. |  | 
 | 164 | +| **`N`** | Continue searching but in the opposite direction. |  | 
 | 165 | + | 
 | 166 | + | 
 | 167 | +### Using "ex mode":  | 
 | 168 | +- `vi` is originally based on yet an older editor called `ex` which allowed you to search some pattern and edit it in one or more lines, but couldn't print more than one page! `vim` still retains vestiges of `ex`. When you type a column **`:`** followed by something like **`:q`**, you are basically in **ex mode**. The following examples show some of the more useful facets of the ex mode:  | 
 | 169 | + | 
 | 170 | +| Command | Action |  | 
 | 171 | +| --- | --- |  | 
 | 172 | +| **`:g/Action`** | Search for the word `Action` and prints all occurrences of lines where it is. Like `grep`. |  | 
 | 173 | +| **`:s/Action/action`** | substitute the first occurrence of `Action` with `action` in the current line. |  | 
 | 174 | +| **`:g/Action/s//action`** | Find all first occurrences of `Action` in each line and substitute them with `action` |  | 
 | 175 | +| **`:g/Action/s/action/g`** | Find all occurrences of `Action` and substitute them with `action` |  | 
 | 176 | +| **`:g/Action/s/action/gp`** | Find all occurrences of `Action` and substitute them with `action` and print all changes so you can check if something goes wrong, or modified things you didn't mean to change. |  | 
 | 177 | + | 
 | 178 | +### Learning more about `vi` and `vim`:  | 
 | 179 | +- To learn more about `vim` and `vi`, type **`vimtutor`** which steps you through a fun tutorial of vim and its great features.  | 
 | 180 | + | 
 | 181 | + | 
 | 182 | + | 
 | 183 | + | 
 | 184 | +## Finding Files:  | 
 | 185 | +### Using `locate` to Find Files by Name:  | 
 | 186 | +### Searching for Files with `find`:  | 
 | 187 | +#### Finding Files by Name:  | 
 | 188 | +#### Finding Files by Size:  | 
 | 189 | +#### Finding Files by User:  | 
 | 190 | +#### Finding Files by Permission:  | 
 | 191 | +#### Finding Files by Date and Time:  | 
 | 192 | +#### Using `not` and `or` when Finding Files:  | 
 | 193 | +#### Finding Files and Executing Commands:   | 
 | 194 | + | 
 | 195 | +### Searching in Files with `grep`:  | 
0 commit comments