VIM is a powerful text editor that is widely used in the Linux community. It is known for its efficiency and versatility, and is a popular choice among developers and system administrators. In this tutorial, we will cover the basics of using VIM to edit files in Linux.
Before we dive into the tutorial, it is important to note that VIM has two main modes: normal mode and insert mode. Normal mode is the default mode, and is used for navigating and manipulating text. Insert mode is used for inserting and editing text.
To open a file in VIM, simply type the following command in your terminal:
vim filename
This will open the file in VIM. If the file does not exist, VIM will create a new file with the specified name.
One of the most useful features of VIM is its ability to quickly navigate through a file. In normal mode, you can use the following keys to move around:
h
: move leftj
: move downk
: move upl
: move rightYou can also use the arrow keys to move around, but this is generally not recommended as it can be slower and less efficient than using the h, j, k, and l keys.
To move to the beginning or end of a line, use the 0
(zero) key to go to the beginning of the line and the $
key to go to the end of the line.
To move to the beginning or end of a file, use the gg
key to go to the beginning of the file and the G
key to go to the end of the file.
To enter insert mode, simply press the i
key. This will allow you to insert text at the current cursor position. To exit insert mode and return to normal mode, press the Esc
key.
To delete a character, use the x
key. To delete a line, use the dd
keys. To delete a word, use the dw
keys.
To undo your last action, use the u
key. To redo an action that you have undone, use the Ctrl + r
keys.
To save your changes and exit VIM, use the :wq
command. This will write (save) your changes and quit VIM.
If you want to save your changes and continue editing, use the :w
command. To exit without saving your changes, use the :q!
command.
To select all text in Vim, you can use the ggVG
command.
Here’s how it works:
gg
moves the cursor to the beginning of the fileV
enters Visual Mode, which allows you to select textG
moves the cursor to the end of the fileOnce you have entered this command, all text in the file will be highlighted and you can perform actions on it, such as copying or deleting.
If you just want to select a portion of the text, you can use Visual Mode to select the text you want by moving the cursor to the beginning of the text you want to select, pressing v
, and then using the arrow keys to highlight the text. When you are finished selecting, you can perform actions on the selected text as usual.
Note: If you are in Insert Mode and want to select all text, you can press Esc
to exit Insert Mode and then use the ggVG
command to select all text.
There are many advanced techniques and features that you can use in VIM. Some of the most useful ones include:
/
key followed by a search term to search for a specific string of text.:s/old/new/g
command to replace all occurrences of the “old” string with the “new” string.v
key to enter visual mode, which allows you to select and manipulate text in a more intuitive way.:split
command to split the current window into two panes,
Leave a Reply