# VIM Editor

## Resources

{% file src="/files/-MbG-8HKgMVAFdNKFU6g" %}
Mastering Vim Quickly From WTF to OMG
{% endfile %}

{% file src="/files/-MbG-TFsvhs4HREepFyo" %}
Vim 101 Hacks
{% endfile %}

To use Vi we must first understand the 3 modes in which this powerful program operates, in order to begin learning later about its text-editing procedures.\
Please note that modern Linux distro's ship with a variant of Vi called Vim (vi improved). For this narrative we use vi and vim interchangeably.

## Vim Philosophy

Vim is different to other text editors but 2 main features makes it so different:

* **Modal Editing** - Effectively editing text makes Vim more powerful than any other text editor. Want to jump to the top of the file **`gg`** or go to the bottom **`G`**. In normal text editors you type **gg** or **G** you will in fact type those letters into your file, that's why Vim has modes, and pressing **gg** or **G** in a different mode produces different results
* **Operators**- If you are moving your cursor using arrow keys, "page-up", "page-down", "end" and "home" keys - Vim has so much more to offer: **`d$`** if you want to delete text to the end of the line ($= end of line and d=delete). Here **'d' is the operator command**. These type of commands (operators) can delete, change or insert text, copy or format it etc

## Understanding Vi Modes

Vim has 12 modes in total but we will normally use between 4 and 5 in our daily use. Very important to understand these.

### Normal Node

When you start vim = normal mode. Mostly used for navigation and text manipulation. A good habit to adopt and keep in mind: whenever you're not typing - get back into normal mode

![](/files/-MbNeKBmf06fSOeCsC9i)

### Insert Mode

Inserts new text, but can also run some commands

### Command Mode

In this mode you can run Ex commands like **`:set number`**, enter search patterns like **`/word`** and enter filter commands. **After running the command Vim returns to normal mode.**

### **Visual Mode**

**For navigation and manipulation of text selections.** It's similar to Normal mode, but the movement commands extend a highlighted area. When you use a non-movement command, it’s executed for the highlighted area. By default, there is “– VISUAL –” shown at the bottom of the window.

## Working With Files&#x20;

Current file = current 'buffer'

### Opening files:

* ***From terminal***
  * **`vim /etc/passwd`**
* ***Inside Vim***
  * **`:e /etc/passwd`**

Copy a file or contents of a file to the current file using the **r**ead command or **r**\
&#x20;    **:r  file.txt**                                   Insert the file **file.txt below the cursor** in current buffer.\
&#x20;    **:0r file.txt**                                  Insert the file file.txt **before the first line.**\
&#x20;    **:r!sed -n 2,8p file.txt**               Insert **lines 2 to 8 from file file.txt below the cursor**\
&#x20;    **:r  !ls**                                         Insert **a directory** listing below the cursor.

### Closing Files:

**:wq** Save currently opened file and exit Vim (even if file is not changed). \
\&#xNAN;**:x** Exit Vim but write only when changes have been made. \
**ZZ** Equivalent to **:x**. Notice there’s no **:** This is a key press \
\&#xNAN;**:q!** Exit Vim without saving currently opened file. \
\&#xNAN;**:qa** Exit all open files in current Vim session

### Saving Files

**:w** Save currently opened file (which was previously saved). \
\&#xNAN;**:w** file.txt Save currently opened file as file.txt. \
\&#xNAN;**:w!** file.txt Save file as file.txt with overwrite option. \
\&#xNAN;**:sav** file.txt Save current buffer as a new file file.txt. \
\&#xNAN;**:up\[date**] file.txt Like :w but only save when the buffer has been modified

### Navigation

| Key Command                                          | Description                                                                                         |
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
| h or left arrow (5h or 5 left arrow)                 | Go one character to the left (or 5 characters left)                                                 |
| j or down arrow (5j or 5 down arrow)                 | Go down one line                                                                                    |
| k or up arrow (6k or 6up arrow)                      | Go up one line                                                                                      |
| l or right arrow (6l or 6right arrow)                | Go one character to the right                                                                       |
| H                                                    | Go to top of the current window                                                                     |
| M                                                    | Go to middle of the current window                                                                  |
| L                                                    | Go to bottom of current window                                                                      |
| w (or 3w etc)                                        | Move one word (incl delimiters)to the right (can also use 3w etc)                                   |
| W                                                    | Move one word (excludes deliminator like .{ } - $ etc                                               |
| b (or 7b etc)                                        | Move back one word to the left (incl deliminator)(Can also use 7b etc)                              |
| B                                                    | Move back one word to the left (excludes deliminator)                                               |
| e                                                    | end of current word (incl deliminator)                                                              |
| E                                                    | end of current word (excludes deliminator)                                                          |
| 0(zero)                                              | Go to the beginning of the current line                                                             |
| ^                                                    | Go to the first non blank character on the current line                                             |
| $                                                    | Go to the end of the current line                                                                   |
| Ctrl-b                                               | Go back one page                                                                                    |
| Ctrl-f                                               | Go forward one page                                                                                 |
| i                                                    | Insert                                                                                              |
| r                                                    | Replace a single letter under the cursor                                                            |
| R                                                    | Replace multiple characters                                                                         |
| x                                                    | Deleting text under character                                                                       |
| Shift-x                                              | Deletes character before cursor                                                                     |
| cw                                                   | Change the current word with new text under cursor                                                  |
| dw                                                   | Deleting the single word beginning with character under cursor                                      |
| dd                                                   | Delete entire current line                                                                          |
| Shift-d                                              | Deletes everything from the cursor to the end of the line                                           |
| 5dd                                                  | Deletes 5  (or whatever number) lines beginning with current line                                   |
| D                                                    | Delete remainder of the line starting with current cursor position                                  |
| :set nu                                              | Displays all line number                                                                            |
| :12,22w test                                         | <p>Writes lines 12 - 22 to new file 'test' in pwd </p><p></p>                                       |
| Replaces every occurrence (the g) of apple with pear | %s/apple/pear/g                                                                                     |
| :%s/^M//g                                            | How to remove Ctrl-M characters from a file in UNIX.(Hold down Ctrl key press V and M in succession |
| yy                                                   | copy(yank,cut) the current line into the buffer                                                     |
| Nyy                                                  | copy(yank,cut) the next N lines including current line                                              |
| p                                                    | put(paste) the line(s) in the buffer after the current line                                         |

### Jumping Around the File

**gg**              Go to the top of the file \
**G**                Go to the bottom of the file \
\&#xNAN;**{**               Go to the beginning of current paragraph \
\&#xNAN;**}**                 Go to the end of current paragraph \
\&#xNAN;**%**              Go to the matching pair of ( ), \[ ], { } \
**50%**          Go to line at the 50% of the file \
\&#xNAN;**:NUM**    Go to line NUM. :28 jumps to line 28

### Navigating in INSERT MODE

![](/files/-McaTwc9jYlEtR-VPrxR)

### Search

**/hello -** search hello using the forward slash, use **n** for next or **N** to search backwards\
Can use **ggn** to start from the top or **GN** to start from the bottom up :)\
\&#xNAN;**\*** or **#  Search for word under cursor use \* going forward and # searching backwards - COOL!!**\
\
**/ or ?       + up or down arrows to search previous history**

{% hint style="success" %}
When you cut and paste into vi form an outside program, the formatting can get a little messed up! Vim comes with a feature called "pastetoggle" which you can enable by editing the line **/etc/vim/vimrc** and adding the following at the bottom:

```
nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
set showmode
```

* [x] &#x20;The first line sets a mapping so that pressing F2 in normal mode will invert the `'paste'` option, and will then show the value of that option. The second line allows you to press F2 when in insert mode, to toggle `'paste'` on and off. The third line enables displaying whether `'paste'` is turned on in insert mode.
  {% endhint %}

![Hit F2 either in normal mode or after INSERT mode and you can see if 'paste' or 'no paste' mode is set](/files/-MC-5x-AYqhQseckgQJW)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://linux.microcisco.com/linux-how-to-do/untitled/vim-editor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
