DEV Community

Cover image for What is Vim and why you should use it!
Patrice Williams
Patrice Williams

Posted on

What is Vim and why you should use it!

Recently, I began an immersive coding program and we are tasked with writing blogs on various subjects. I decided to delve deeper into text-editing. I will be exploring Vim and its importance in the world of programming.

What is Vim?

Vim is a text editor based on the first vi text editor created. VI text editor was originally created by Bill Joy for the Unix operating system in 1976. One of the great features of Vim is that it is a modal text editor. So what exactly does modal mean? Modal refers to the mode that the text editor is in will determine whether inputting alphanumeric keys outputs alphanumeric characters or will instead ‘move the cursor through the document’ (Brockmeier, 2009). For example, when you would like to save a file, in most text editors, you will need to use Ctrl-s hotkey combination, in order to save the file. By using Vim, you no longer need to use any hotkey combinations. You can simply save a file without your fingertips ever leaving the keyboard. Vim offers a multitude of features that make working with ‘structured markup languages like HTML…’ a lot easier. If you are like me, a beginner programing student, then it would behoove you to learn some Vim commands to better utilize the text-editor.

The modes of Vim

When using Vim, there are three key modes: insert-mode, command mode, and last-line mode. The mode that Vim starts in is command mode. When you first open a file, by running vim filename, you are able to edit that file. Alphanumeric keys are tied to the commands, therefore the alphanumeric keys you input will not be inserted (If you type ‘dd’, the entire line will be deleted, instead of simply inserting ‘dd’). You can solve this problem by switching to insert mode (type ‘I’). Now the keys will be inserted. To exit insert mode, hit the esc key. The last mode that will be discussed is last-line mode. Now that we’ve entered keys into the text-editor, we need to save our work. We do this by pressing ‘:’ and Vim switches to last-line mode. To save the file, enter ‘:wq’, the w writes the file and q exits the editor. Now lets discuss some important Vim commands that will make moving around Vim and editing files much easier.

Vim commands

Moving files in Vim

When in command mode, you can move your files around to where you need them to go. Here’s some commands to start out using:

  • b moves backward one word
  • w moves forward one word
  • j moves cursor down a line
  • k moves cursor up a line
  • h moves cursor one character to the left
  • 0 moves cursor
  • $ moves cursor to end of the line
  • G moves to the end of the file
  • gg moves to the beginning of the file
  • `. moves to last edit

If you need to use any of these move commands a certain number of times, you can put a number before the command. For instance, if you enter 6h, the cursor will move 6 characters to the left. If you enter 4j, the cursor will move down 4 lines. Once you feel comfortable, start playing around with this cool trick. (Zonker, 2009)

How to edit things in Vim

Alt Text
Editing text in Vim has never been easier, especially now that you know how to move around your file. Lets try deleting some text. Move the cursor to the end of the word and type x. By doing so, you should have deleted the letter the cursor was on. If this is not what you meant to do, no worries, we can simply undo the command by typing u. Now, if you would like to undo your undo, input Ctrl-r. This will redo your last command entered. If you want to delete to the end of the file, hit dG. The following are more editing commands that you will want to be familiar with:

  • dw deletes a word
  • d0 will delete to the beginning of a line
  • d$ deletes to the end of a line
  • dgg deletes to the beginning of a file
  • dG deletes to the end of the file
  • u undos the last action
  • Ctrl-r redos the last action

Searching/Replacing text

We now know how to move around our text and edit our text, but what about if we need to search or replace text? Searching the document is best accomplished from command mode. Use ‘/’ and then the text you want to search for. So if I was searching for “puppy” I would type /puppy and then hit enter. A few more commands:

  • /text to search for text in the document
  • n move cursor to next instance of the text from the last search
  • N moves the cursor to the previous instance of the text from the last search
  • ?text searches for text in the document, going backwards

Copying and Pasting

Copying and pasting text is important when trying to edit the document. If you are trying to select text, in the command mode, hit V and you will be able to highlight the text by using your cursor. Some commands that will help to get you started are:

  • v highlights one character at a time
  • V highlights one line at a time
  • Ctrl-v highlight by columns
  • p paste text after current line
  • P paste text on the current line
  • y yank text into the copy buffer

Saving and Quitting

Now that we have edited our document, we must save the work we have just created. If you are in insert mode, make sure you hit esc. Then enter :, and you should see a line at the bottom of the screen. This means that your cursor is ready for your input. In order to write the file you are currently editing, enter w. (w) writes the file to the existing filename. When you are finished, hit :q to quit. You can couple the commands and add a ! to quit the program with no warning, :wq!. Here’s some great exit commands:

  • :wq writes file to disk and quits the editor
  • :q! quits file with no warning
  • :q quits file with warning
  • ZZ save workspace and quits editor (same as :wq)
  • : 10, 15 w temp (writes lines 10 through 15 into file named temp) Other line numbers can be used

In conclusion, Vim is a great text-editor to use because it makes working with structured markup languages easier. Your fingers never have to leave the keyboard, so you can do-away with using your mouse altogether. You should become familiar with using the above Vim commands in order to cutdown on the time it takes to edit your documents.

Sources
Arsenault, C. (2020, July 13). Popular Vim Commands - Comprehensive Vim Cheat Sheet. Retrieved December 14, 2020, from https://www.keycdn.com/blog/vim-commands
Brockmeier, Joe. (2009, November 21). Vim 101: A Beginner's Guide to Vim. Retrieved December 14, 2020, from https://www.linux.com/training-tutorials/vim-101-beginners-guide-vim/
R. (n.d.). Vim Editor Commands. Retrieved December 14, 2020, from https://www.radford.edu/~mhtay/CPSC120/VIM_Editor_Commands.htm
Vi. (2020, December 13). Retrieved December 14, 2020, from https://en.wikipedia.org/wiki/Vi

Oldest comments (0)