VIM User Guide for Beginners (Part 1)

Tram Ho

Have you ever wanted to learn Vim, but not sure how to get started? No problem. This will be an article that shows you how to use Vim and where to start using the world’s best text editor.

Note that this is an obvious post for the beginners of Vim. If you’ve been using Vim for a while, all of this should look completely familiar – and you might wonder why some topics aren’t covered. Be patient, I will get everything done in the right time, but there is a lot more to be worked out!

What is Vim, and why should I use it?

Vim is the editor chosen by many developers and users. This is a “modal” text editor based on the vi editor written by Bill Joy in the 1970s for a UNIX version. It inherits the main constraints of vi, but also adds a lot of the functionality and expandability that is missing from the original micro.In Vim, you can do a lot without taking your hands away from the keyboard. While Vim is the editor for many people, I’ll be very frank: you probably don’t want to. If you will never do any heavy system administration or text editing and if you don’t want to invest time in learning the capabilities Vim has, then learning Vim may not should be the best use of your time.

Modes

Vim’s design is based on the idea that programmers spend a lot of time reading, navigating, and making small edits, as opposed to writing long streams of text. For this reason, Vim has many modes of operation.

  • Normal: to move around the file and make edits
  • Insert: to insert text
  • Replace: to replace the text
  • Visual (plain, line, or block): to select blocks of text
  • Command-line: to run a command

The key combinations have different meanings in different modes of operation. For example the x in Insert mode inserts only an ‘x’ character, but in Normal mode it will remove the character under the cursor and in Visual mode it will clear the selection.

In its default configuration, Vim displays the current mode in the lower left. The initial / default mode is Normal mode. Generally speaking, you will spend most of your time between Normal mode and Insert mode.

You change the modes by pressing <ESC> (exit key) to switch from any mode back to Normal mode. From Normal mode, enter Insert mode with i, Replace mode with R, Visual with v, Visual Line with V, Visual Block with <Cv> (Ctrl-V, sometimes also written ^ V) and Line mode command with :

Move in Vim

The first thing you want to learn is how to move around the file. When in command mode, you’ll want to remember the following keys and what they do:

  • h moves the cursor one character to the left.
  • j moves the cursor one line down.
  • k moves the cursor one line up.
  • l moves the cursor one character to the right.
  • 0 moves the cursor to the beginning of the line.
  • $ moves the cursor to the end of the line.
  • w moves forward one word.
  • b go back a word.
  • G moves to the end of the file.
  • gg moves to the beginning of the file.

The best way to learn is to practice. Take a few minutes to try Vim. If you are using the Linux system right now, open a terminal and enter the vim file name. Enter insert mode and type a little (or copy some text from this article into Vim) then press Escape to start practicing moving around the file. When you feel like you got it, it’s time to try editing.

Inserting text

From Normal mode, press i to enter Insert mode. Now, Vim works like any other text editor, until you press <ESC> to go back to Normal mode. This, along with the basics explained above, is all you need to get started editing files with Vim (though not particularly effective, if you’re spending all your time on editing. from Insert mode).

Command-line

Command-line can be entered by typing : in Normal mode. Your cursor will move to the command line at the bottom of the screen when you press:. This mode has many functions, including opening, saving, and closing files, and exiting Vim.

  • : q exit (close window)
  • : w save (“write”)
  • : wq save and exit
  • : e {name of file} opens file for editing
  • : ls shows open buffer
  • : help {topic} opens help
  • : help: w opens help for the command: w

Editing Vim

Now that you know how to move around a bit, let’s give it a try. Move the cursor to the beginning of a word. Now type x. What happened? You have deleted the character that the cursor is on. You want to undo it? No problem. Type u (to undo) and it will be restored.

Do you want to delete an entire word? Move your cursor to the beginning of the word again. Use dw. Note that this will only remove the word from the cursor when enabled – so if you have a cursor in the middle of the word, it will only delete from that point on. Again, you will undo it. Note that Vim has multiple levels of undo, so you can undo the previous change and the previous one, etc.

Want to undo your undo? Press Ctrl-r. That will redo your last undo.

Again, here is the longer list of commands you definitely want to know when getting started:

  • d starts a delete operation.
  • dw will delete a word.
  • d0 will delete to the beginning of the line.
  • d $ will delete to the end of the line.
  • dgg will delete to the beginning of the file.
  • dG will delete to the end of the file.
  • u will undo the last operation.
  • Ctrl-r will redo the last undo.

You may have noticed that some commands combine text manipulation and scroll keys. gg takes you to the end of the file and d is used for deletion. Combining them gives you something more powerful. Vim is like that. If you’re working in Vim and think “hey, I wonder if I can combine the two things I know to make something easier”, the answer is usually (but not the time). any same).

Copying And Pasting

You have learned how to delete text. The last text that you deleted is stored in the buffer ready to be pasted into the document. So if you ran dd and deleted the entire line, you can now press p or P to paste it back into the document. This applies to single lines, multiple lines, and even entire documents.

Once you’ve highlighted what you want, press y and it will “drag” the text into the buffer to be pasted later. So a common paste operation might look like this:

Press v to highlight some text. Then press y to drag it to the buffer. Then move the cursor to where you want and use p in command mode. That’s it – you just pasted some text!

The commands you need most to get started:

  • v marks one character at a time.
  • V marks the lines one by one.
  • Ctrl-v highlights by column.
  • p pastes the text after the current line.
  • P pastes text on current line.
  • y copies text into the copy buffer.

Above is the introduction to Vim as well as basic usage for newcomers. In the next article, I will talk more about the other common commands in vim and config help work more efficiently. You can refer to some more documents about vim for newbies below:

Thank you for reading the article !

Share the news now

Source : Viblo