Super Easy Introduction Guide to Using Vim To Edit Your Files

Super Easy Introduction Guide to Using Vim To Edit Your Files Thumbnail image

Vim is one of the oldest but still most popular text editors out there. It was released in 1991, and is still in active development. You can often find vim plugins for many IDE editors, and is installed by default on many Linux distributions.

Why learn Vim?

  • It is installed or easily available on almost all operating systems. It is also often available as a plugin or extension to many IDEs. It can be very useful to be able to use Vim when editing files (especially config files) via SSH.
  • It can handle large files - it will be hard to find a text file (or log output) that'll be too large to open in Vim.
  • But most importantly: efficiency - once you have learned the basics of Vim you will realise that you are much faster at editing text files. This is the reason I use it!

The most important things to know about VIM

There are two 'modes' that are important. The normal mode is for moving your cursor around the text document. The insert is for actually inserting text and editing. (There are other modes, like visual (for making text selections) and command-line for entering commands - but for now you don't have to worry about these modes).

At first it takes a while to get used to switching between modes. After you get used to it, it will feel like second nature. When using normal text editors you will by mistake start using Vim commands (especially pressing the "Escape" key to 'exit' insert mode, even though you are in TextEdit/Notepad...

The most important Vim things to know:

How to save a document:

Press : (colon) this will bring up the command-line model. Then write w and hit enter.

How to quit vim:

How to exit or quit vim

Press : to bring up the command-line mode (at the bottom of the window/screen). Then type q and hit enter.

How to save and quit at the same time:

Press : (to go into command-line mode) then write wq (which stands for 'write' + 'quit') and hit enter.

After using Vim for a while these will become second nature (and I guarantee you'll try and do them in other apps...)

How to go from 'normal' mode into 'insert' mode

If you are in normal mode (which is used for moving the cursor around the text document) and you want to start editing text then you will want to go into 'insert' mode.

Press i (for insert) to switch to insert mode. Then you can write text at the cursors position.

Other common options for switching into insert mode (while in normal mode):

  • a (for append) to start insert mode at the next character (one character to the right of the cursor).
  • A go to insert mode at the very end of the current line
  • I go to insert mode at the very start of the current line
  • o to start a new line and go into insert mode

How to move around the document using the keyboard

If you are in normal mode then you can move around the document very easily.

  • h/j/k/l = move left/down/up/right. You can also use the normal arrow keys on most keyboards, but using the h/j/k/l keys are slightly easier once you get used to it, as it means your hands don't move around as much.
  • G = move to the very bottom of the document
  • gg = move to the very top of the document

Those are the simple ways of moving around a document. You should be able to get used to them quite quickly.

To be quicker at moving around documents it can be useful to move between words or blocks of text:

  • w (for word) go to the next word (first character in the next word)
  • e (for end) go to the end of next word (last character in the next word)
  • { go to the previous 'block', } go to the next 'block'.

You can also combine almost any command with a number, to repeat it a certain number of times.

For example, to move 5 words forward, type 5w (5 words).

If you are in insert mode then you have to use the arrow keys to move around. But I recommend exiting insert mode (press escape) and go into normal mode, then move around using the instructions above.

More Blog Posts in this series:

webdevetc profile pic
webdevetc

I am a 29 year old freelance backend web developer from London, mostly focusing on PHP and Laravel lately. This is my site - I mostly write about PHP here. Contact me here (especially for any contracting jobs early 2019 in London ;) ).

Leave a Comment