# VI/VIM

#### Line Numbers

vim can display line numbers using the below command:

```
:set number
```

##### Skip to specific line number

To skip to line 255:

```
:255
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

##### Searching strings

To search for a string in a text file, the / option can be used. In the below example, we're searching for the word test

```
:/test
```

To skip to the next found instance of the word, press the n key.

##### Search string on a range of lines

In this example, I'm searching for the string 'test' on lines 1-100:

```
:1,100/test
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### Deleting Lines

Vim can delete lines using a number of methods

##### Remove X amount of lines.

 The below command would remove 5 lines from (and including) the current line:

```
:5d
```

##### Remove a range of lines:

```
1,5d
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### Search and replace

##### Search and replace a word through the whole file:

```
:%s/wordtosearch/wordtoreplacewith
```

##### Search and replace on specific lines

We can couple the search and replace function of vim with the line number range tool. The below would find any instances of the word 'test' within the line range of 1-100, and then replace all instances of the word with 'testing':

```
:1,100s/test/testing
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### Copy &amp; pastse

##### Copy the currently selected line  


```
:yy
```

##### Copy a range of lines

The below would copy lines 1-1000

```
:1,1000y
```

##### Paste the line:

```
p
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB">Saving &amp; exiting </span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB">save and quit:</span>

```
:wq
```

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB">don't save and quit</span>

```
:q!
```

##### Save as new file

```
:w newfilename
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>