Vim is a free and powerful text editor that is often pre-installed in Mac or Linux operating systems.
Here at WEBGIIG, it is among the most loved text editors, to the extent that you may even have the impression that we’ve built a religion around it.
In this ultimate vim beginners guide, you’ll learn the basics of this fantastic text editor and hopefully love it like we do.
You can open files with Vim like in any command-line editor.
If the file you want to edit is Leben.Md, navigate to its directory and enter the command below in the Terminal window:
Vim will open in this terminal with the file loaded. If the file wasn’t exiting, vim will automatically create it.
Vim is a terminal program, not a graphical system program.
Basics of Vim modes
Vim has four modes: Normal, Insert, Visual, and Command. Each mode is displayed in the status bar of the program at the bottom left.
When you start Vim, it’ll be in Normal mode. In this mode, you will be able to use all the command keys to navigate the file and start editing. Also, if you exit any of the other modes, Vim will take you back to Normal mode.
Vim gets into the insert mode whenever you press any of the keys [ A, I, or O] while in Normal mode. Once in Insert, the editor will remain in that mode until you press the Esc key. Any other key pressed is inserted into the file directly at the current cursor position.
Visual Mode happens when you use a v, V, or Ctrl-v commands from normal mode. In Visual mode, you can select text. When you use a navigation command, the pane from the beginning of Visual mode until you exit Visual mode is the selected text.
Whenever you use the “:” Command in Normal Mode, you will enter Command Mode. In this mode, you can perform complex editing functions, file actions, or shell actions. The Command Mode is the only mode in which nothing is displayed in the status bar. However, the command entered will appear below the status bar if something else is typed and the cursor is stationary.
Saving files and closing Vim
In Normal mode, you can enter ZZ to save everything and exit. You can also save the file with : w!. The : will get you into command mode, the w writes the file and the ! forces the operation to write without questions. Or you can type : wq or : wq!. The q terminates the editor. You can also use : q! without canceling saving.
Basic cursor movements
In Normal mode, you can move around the file and make certain changes to the file. The h key moves the cursor to the left. The l key moves the cursor to the right. The j key moves the cursor down one line, while the k key moves the cursor up one line. To move on to the next word, use the w command. The previous word command is b.
If you want to move more than one space, word, or line at a time, enter a number first, and then type the directional key. The cursor moves in that direction so many times. For example, if you type 10j, the cursor will move down 10 lines.
The command mode allows you to toggle the line numbering to absolute or relative:
- Absolute The numbering mode is normal: each line has a unique number in sequential order.
- Relative Numbering mode displays the number of rows that are away from the current edit row.
To get absolute line numbering, you can use the :set number command. To display no line numbers, use the :set non-number command.
Set Relative Numbering, Type :Set Relative Number. To put it back Absolute numbering, type :set norelative number.
By setting both modes with :set Number and :set Relative Number, your Vim will then display relative numbers for all but the current line. The current edit line shows the absolute numbering.
By using Relative Numbering mode, you can quickly see the number of lines you want to move using the j or k commands. For example, to get to the line with list, you would press 2j.
To get to the beginning of a line, use the 0 (which is a zero) command. To get to the end of a line, use the $ command. The gg command moves the cursor to the top of the file while the G command moves to the end of the file.
The .vimrc file
You may always want to have the Relative line numbering, but it’s difficult to always set it at startup Vim. That’s where the Vim configuration file comes in handy. In the terminal of your home directory, type
The .vimrc file is Vim’s configuration file. Any command you enter in command mode can be added to this file. It runs every time Vim is launched. In this file, use the I command to insert text. Now add and save the following lines:
Well, every time you open Vim it will have the mixed Absolute and Relative line numbering mode set, with all the search results also highlighted. The highlighted search is helpful in the next section.
You can do a lot more with the .vimrc file, but that will have to wait for another tutorial.
Search & Replace
You can search with the / command in normal mode. By typing /This, you will see all of these words as highlighted below.
By typing n, your cursor jumps to the next occurrence of the search pattern. By using N, you can revert to a previous occurrence. The pattern after the / can be any regular expression. Refer to the article Advanced search and replacing with RegEx to better understand regular expressions.
To replace text, you need to use command mode. In command mode, the s command is for substitution in the current line, %s is for substitution throughout the file, and ,s is for swapping line number to line number.
The format is /// gi are regular regular expressions. In the example above, I’m replacing any existence of Dies with That. The “I” after the G makes the search insensitive. One I would make the search case sensitive. The G makes the substitution global in the line. Without the G, it performs the substitution once per line.
To insert text to the left of the current cursor position, use I command. The one command is inserted to the right of the current cursor position. The I command inserts at the beginning of the line, while ON inserts to the end of the line.
The O command inserts a whole new line after the line where the cursor is located and sets the Editor Insert mode at the beginning of that line. The O does the same thing, but adds the line above the current cursor position.
To delete characters, use the d command and then a direction to delete the character in that direction, or the spacebar to delete the character under the cursor. If you prefix a number, then Vim deletes that number in the specified direction. The dd command deletes the current line. The D command deletes everything from the current cursor to the end of the line.
The x command deletes the cursor character. The X command clears before the cursor. Both x and X commands need a number prefix to perform the action as many times as possible.
Copy, Cut, and Paste
When you press v in Normal mode, Visual mode starts. All cursor movements cause a selection from the beginning Visual mode. If you have a choice, use the y command Yanks or copy the selected text. Switch to a new location and use the p command to paste after the cursor, while the p command inserts before the cursor.
Once selected, the x command clears the selection. Using the d command cuts out the section so that you can paste it with the p command.
To select a block of text, start with the -v command. The V command launches Visual mode selection by lines and not by characters.
Practice makes perfect.
Now that you know the basic commands that are used in Vim, you need to practice. Vim Adventure is a great way to practice the Vim commands. It is an adventure game where you practice the different Vim commands in order to explore the Vim world.
Related: What problems does cloud computing solve?
Vim Reference is a cheat sheet that helps you remember the most commonly used Vim commands. Above all, keep practicing and you will be able to master this simple but powerful editor.