We’ve all heard of copy, cut and paste right?
In vi we have similar features.
But to make things a little more challenging we have given these features different names. We can’t make it too easy, right?
Delete A Line Of Text (Cutting)
So once we have pressed the Escape key to enter command mode, we can now try to delete a line of text from our text file.
To do this we simply press the ‘d’ key twice (dd). Think of it as ‘deleting’ the line, or ‘cutting’.
The deleted line is now placed into what we call the register (The deleted line of text is placed into memory). Think of the register as a ‘clipboard’.
Delete Words
If we don’t want to delete a whole line of text with the ‘dd’ command, we can use ‘dw’ (delete word) to delete a word instead.
Note that characters such as an exclamation mark (!) is considered it’s own word.
Delete A Letter
We can also delete just one letter (character) while in command mode too. Using dl (lowercase L)
Yank
To copy a line of text from our file, we first make sure we are in Command mode (by pressing Escape).
Then by pressing the ‘y’ key twice, we copy the whole line that the cursor is at. In vi, we call this ‘yanking’.
We are copying this line of text into the register, and we don’t see anything happening on the screen when we do this.
Next we need to ‘put’ (paste) this text back into our file (See ‘Putting’ below).
We can also yank a single letter by using the command ‘yl’ or yank a single work with the command ‘yw’.
Putting (or Pasting)
If we use the navigation keys to move the cursor around the screen (HJKL), we can then press the ‘p’ key anywhere we like, and the text that is held in the register will be placed down.
Think of ‘p’ as ‘pasting’, but in vi we call this ‘put’.
We can use ‘put’ right after copying with yank, or by deleting a whole line with ‘dd’ for example.
Try these command yourself to get familiar with them (dd, dw, dl, p, yy)
NOTE About The New Line Character
I don’t want to go into much detail here, but in linux we have an invisible character at the end of a line of text.
This character tells the system that there’s a line break here, and the next set of characters are on a new line.
When we use ‘yy’ to copy a line of text, we are also copying this invisible character.
So when it comes to placing down this line of text, this invisible character is also placed down, and so, the line appears on a new line from where your cursor happens to be.