I use Vim, all my homies use Vim.. do you use Vim? Vim is a powerful and popular text editor for Linux that is known for its efficiency and flexibility. It is widely used by developers and system administrators for writing code, configuration files, and scripts. While Vim can be intimidating for those new to the text editor, its features and functionality make it well worth the time to learn.
In this article, we will go over the basics of using Vim in Linux, including how to open files, navigate the text, and perform basic edits. We'll then go into some more advanced features after that.
Getting started with Vim
To open a file in Vim, simply type
To save the changes you've made to the file, press Esc to return to Command mode, and then type
Navigating text
In Command mode, you can use the cursor keys to navigate the text, but Vim provides several shortcuts that can make navigation even faster. For example, the
Performing basic edits
Once you've navigated to the desired location in your text, you can perform basic edits. In Insert mode, you can insert text as you normally would in any text editor. To delete a character, navigate to it in Normal mode and press the
Undoing changes
If you make a mistake while editing your text, you can undo your changes by pressing the
Searching and replacing text
Vim also provides powerful search and replace functionality. To search for a word in your text, press
Similarly, you can search upwards by typing
To replace a word in your text, type
More advanced edits / commands
Conclusion
Vim is a powerful and flexible text editor that is widely used by developers and system administrators. While it can be intimidating for those new to the text editor, its features and functionality make it well worth the time it takes to master it.
Type
In this article, we will go over the basics of using Vim in Linux, including how to open files, navigate the text, and perform basic edits. We'll then go into some more advanced features after that.
Getting started with Vim
To open a file in Vim, simply type
vim [file_name]
in the terminal. This will open the file in Vim, and you will be in Command mode, which is used for navigating the text and executing commands. To start editing the text, press i to enter Insert mode. You can then type in your text and use the cursor keys to navigate.To save the changes you've made to the file, press Esc to return to Command mode, and then type
:w
followed by the Enter key. To exit Vim, type :q
and press Enter. If you want to exit and save your changes, type :wq
and press Enter. Another way to quickly save and quit is to just type ZZ
(no hitting Enter required).Navigating text
In Command mode, you can use the cursor keys to navigate the text, but Vim provides several shortcuts that can make navigation even faster. For example, the
h
key moves the cursor to the left, the j
key moves it down, the k
key moves it up, and the l
key moves it to the right. The w
key moves the cursor to the next word, and the b
key moves it to the previous word. The 0
(zero) key moves the cursor to the beginning of the line, and the $
key moves it to the end of the line.Performing basic edits
Once you've navigated to the desired location in your text, you can perform basic edits. In Insert mode, you can insert text as you normally would in any text editor. To delete a character, navigate to it in Normal mode and press the
x
key. To delete a word, navigate to the first letter of the word and type the dw
keys. To delete a line, navigate to the line and type dd
.Undoing changes
If you make a mistake while editing your text, you can undo your changes by pressing the
u
key in Command mode. To redo a change, press ctrl-r
.Searching and replacing text
Vim also provides powerful search and replace functionality. To search for a word in your text, press
/
followed by the word you're searching for and press Enter. Vim will highlight the first occurrence of the word and move the cursor to that location. You can press the n
key to jump to the next occurrence, and the N
key to jump to the previous occurrence.Similarly, you can search upwards by typing
?
in Command mode!To replace a word in your text, type
:%s/old_word/new_word/g
and press Enter. This will replace all occurrences of old_word with new_word.More advanced edits / commands
:w filename
- This command allows you to save the contents of the file to a different file name.:r filename
- This command reads the contents of another file and inserts it into the current file at the cursor location.:set number
- This command displays line numbers in the left margin of the editor, making it easier to navigate large files. To turn off line numbers, use the command :set nonumber
.:g/pattern/d
- This command deletes all lines in the file that contain the specified pattern. For example, :g/error/d
would delete all lines that contain the word "error".:split
filename - This command splits the editor window into two panes, with the specified file open in the new pane.:vsplit
filename - This command splits the editor window vertically into two panes, with the specified file open in the new pane.:tabnew
filename - This command opens the specified file in a new tab in the editor.:tabclose
- This command closes the current tab.:tabs
- This command displays a list of all open tabs in the editor.gt
- Will bring you to the next tabgT
- Will put you on the previous tabConclusion
Vim is a powerful and flexible text editor that is widely used by developers and system administrators. While it can be intimidating for those new to the text editor, its features and functionality make it well worth the time it takes to master it.
Type
man vim
for more info! Also check out the vimtutor
application!
Last edited: