DEV Community

Fabio Rosado
Fabio Rosado

Posted on • Edited on

Introduction to Vim

This post was first created for an open source project that I contribute to. The project creator suggested that I should post it in a blog so I have expanded the post a bit and submitted it to my personal blog. I've decided to share it with you, I hope you enjoy it!

Vim, stands for vi improved and is a command line text editor that comes installed with every platform other than windows (unless you install it). It can be hard to figure out how to do things in this editor, this post is meant to give you the basic knowledge of vim and how to do things with it.

Let's start with the basics. How do you open vim?

If you are running Linux or MacOS is most likely that you have vim installed already so you can just open your terminal and run the command vim <filename> to edit that file.

Note: You can only edit files that are within your working directory.

If you are on windows you could download it from vim.org.

Vim Modes

Vim comes with three different modes:

  • Normal Mode (Default mode)
  • Insert Mode (Press i key)
  • Visual Mode (Press v key)

These modes are quite easy to understand and you can see which mode you are in on the bottom left corner of the terminal window.

The normal mode is the default mode and the one activated as soon as you open Vim, you can move around, read the text, copy, insert lines, etc. The only thing you can't do in this mode is editing the text.

Editing text

If you want to edit the text you can just press the i key on your keyboard you can notice that the bottom left corner of the window now shows the text: --INSERT--.

You will now be able to use your terminal as a text editor, you can add, edit and delete text like you would do in any other text editor.

Once you are happy with the changes that you have just made, press the esc key on your keyboard and you will be back to the Normal mode.

Visual Mode

There is nothing much to say about this mode. It allows you to select big chunks of text in order for you to copy or cut. When this mode is active you can read the text: --VISUAL-- in the bottom left corner of the window.

Another thing you can do in the visual mode is to highlight text and then make small changes to the highlighted text such as changing to uppercase or indenting lines.

Vim Commands

Vim is meant to help you do things fairly quickly without the need for a mouse. Everything can be done with a keyboard, so learning some of the Vim commands will be helpful.

Things that you will learn:

  • how to save a file
  • how to quit Vim
  • how to move around
  • how to see line numbers

Note: Vim has a lot of different commands and combinations, this is just an introduction and you should read other sources if you want to learn how to use Vim properly.

Saving and quitting Vim

Now that you know how to edit a text in Vim, the most important thing you will learn will be how to save your changes and quit. If you press the : key of your keyboard, you will be able to enter commands to Vim.

To save a file all you need to do is type :w and then press enter.

To quit a file and go back to the command line you need to type :q.

Note that if you made changes to the file and didn't save them, Vim won't automatically exit, instead, it will tell you to run the command :q! which basically translates to force quit.

These two commands can be combined into one :wq. This will write the changes to the file and then quit Vim, if you want to be quicker the key combination shift+zz will achieve the same results.

You can also use the command :w <filename> to save a new file with that name - useful if you didn't open a file with vim.

Line numbers

Showing line numbers can be very useful when editing a file. VVim allows you to jump straight into a line if you know its number, so your editing can be done quicker if you know exactly where and what to edit.

To show line numbers you need to run the command :set number, once you press enter, you can see that Vim will show the number of each line.

If you want to jump straight to a line you can type the command :<line number> and the cursor will jump to the beginning of that line.

Moving around

To move around all you need is pressing a few keys to do different things. Moving around doesn't require you to enter the command mode by pressing the : key.

  • h or arrow left - move the cursor one character to the left
  • l or arrow right - move the cursor one character to the right
  • j or arrow down - move the cursor one line down
  • k or arrow up - move the cursor one line up
  • 0 - move the cursor to the beginning of the line
  • $ - move the cursor to the end of the line
  • w - move the cursor one word forward
  • b - move the cursor one word back
  • gg - move to the beginning of the file (line 1)
  • G - move to the end of the line (last line)

This two commands can be used while moving/editing the file:

  • o - adds an empty line below the cursor, moves the cursor to that line, enters edit mode
  • O - adds an empty line above the cursor, moves the cursor to that line, enters edit mode

Deleting things

You can delete things by pressing the d key, this will serve as the cut command as well. Vim allows you to combine commands to achieve a porpose, so you can combine the moving around commands with the delete to improve your editing skills.

  • de - delete from where the cursor is until the end of the current word
  • dw - delete to next word
  • d2w - delete two words from cursor
  • d$ or D - delete to the end of the line from where the cursor is
  • dd - delete the whole line

Other useful commands

Since we tend to delete the wrong things vim also comes with an undo and redo command that can come in handy in plenty of situations.

  • u - undo previous command/action
  • CTRL-u - redo previous command/action
  • . - repeats last command that modified something (such as 2dd - delete two lines)

You can also make vim search for a term in the whole file with the commands:

  • :?<term> - searches for that term from the cursor down
  • :\<term> - searches for that term from the cursor up
  • n or / - go to the next searched term
  • N or ? - go to the previous searched term

Conclusion

This concludes the introduction to Vim, hopefully, you found it useful. There is a lot of things that you still need to learn but this should give you the basics to work around in this text editor.

If you would like to know more about Vim, you can run the Vim tutor by running this command on your terminal:

$ Vimtutor

This will open a text file with step-by-step instructions that cover all the basic commands in Vim.

Andrea Benfatti suggested on Twitter that https://vim-adventures.com was good for anyone new to vim. I didn't know about this site before but I checked it out and it does seem a very fun way to practice a lot of the vim commands with this game so do check it out!

Top comments (9)

Collapse
 
ferricoxide profile image
Thomas H Jones II

To start: Always looking for guides I can link our new teammates to, so, thanks!

Small quibbles:

  • dw - delete from where the cursor is until the end of the word

Is slightly inaccurate. What it really is "delete to next word". de would be "delete from where the cursor is until the end of the current word". The difference is subtle but can be seen if you're nuking characters/words from a multi-word line of text: dw deletes the separators between two words while de terminates the delete-action before the word-separator

  • d$ - delete to the end of the line from where the cursor is

While correct, can also be accomplished with just D. If you're a lazy-typist like I am, that's two keystrokes (<SHIFT> and d) rather than three (d, <SHIFT> and 4).

With your search tips, once you've done either /<TERM> or ?<TERM>, simply typing / or ? will accomplish the same thing as n or N, respectively.

If you're a really lazy-typist (like I am), you'll make heavy use of '.' ...which repeats whatever your last modifying-action was — whether delete, insert or whatever — each time you type it.

And of course the real beauty of vim is most commands are predictably modifiable or stackable. Want to delete the line you're on: dd. Want to delete three lines - including the one you're on, 3dd. Want to delete to the third word to the right, 3dw. Want to delete to the beginning of the line you're on, d0. Want to delete from where you are to the end of the file, dG. ...ad nauseam.

Yeah: vi/vim enthusiast for just shy of thirty years. =)

Collapse
 
fabiorosado profile image
Fabio Rosado

Thank you so much for your comment Thomas, I got to admit that I didn't know that . would repeat the previous command, this will save me a lot of time! I don't use the search command all that much to be honest so I didn't knew that you could use the / or ? would accomplish the same thing.

I've updated the post following your suggestions and comments, so thank you so much for them as I have learned new things today as well :D

Collapse
 
rossijonas profile image
Jonas B. R.

Thomas you get right in the vein! All of us vimmers are obsessed counting keystrokes 😂😂!!! It looks like you're a very eperienced vim user, and I'd like to extend to you the invitation to help more people learn vim, bringin some digested tips.
I invite you to check vim drops, and start to write some tips there!

Have a good weekend!✌️

Collapse
 
ferricoxide profile image
Thomas H Jones II • Edited

First time I used vi would have been sometime in 1989 (criminy, I'm old): one of my dorm floormates was a CE major. He worked at one of the CS/CE college's UNIX labs and created an account for me to play around with (mostly for Usenet). Was an early eye-opener to the powers of regex ...and an introduction to the vi/emacs wars.

At any rate: posted a link to your Medium page to our Slack's "Linux Q&A" channel.

Thread Thread
 
rossijonas profile image
Jonas B. R.

Cool Thomas! Nice to hear form you, I'll definetely check the link over there and follow the topics! Thanks for sharing that! BTW, I'm not that young too, I'm almost 38 rsrsrs, looking to all this tech professionals , we could be considered dinosaurs...hehehe Have a great week!

Collapse
 
rossijonas profile image
Jonas B. R.

Nice text Fabio! It's so cool to see people getting excited with Vim and sharing the love!

If you'd like a quick cheatsheet always on hands, just hit duckduckgo, it has an embedded vim cheatsheet:
cheatsheet

If you feel like checking some quick tips and tutorials for beginners, consider visiting vim drops, I'm writing a new article every week!

Cheers! ✌️

Collapse
 
fabiorosado profile image
Fabio Rosado

Thank you so much for your words Jonas glad you enjoyed the post. I use duckduckgo but I didn't know about the cheatsheet feature of it, it looks pretty great thanks for sharing it!

I also didn't know about vim drops on medium but looks good I've added a few articles to my to read list haha

Collapse
 
ferricoxide profile image
Thomas H Jones II

Oop. Sorry, I wasn't clear on the '.': that repeats the last command you issued that modified something. That is, if you'd done 2dd to delete two lines, hitting '.' would delete a further two lines. If you try to use it to repeat a search, bad things can happen (depending on what modifying-actions might be in you action-history).

Collapse
 
rossijonas profile image
Jonas B. R.

Thank you Fabio! I hope you like it! And if you feel like writing any tip to vim drops hit me at the email mentioned on the end of the article, it'd be a pleasure to have more people engaged on offering a hand to people learning vim!