Get some vim and vigour

Little helper.

A photo posted by Beth McMillan (@teraspawn) on

I have a peculiar fondness for programs that you can use at the terminal without having to pop out into something graphical, because I spend a lot of time logged into machines that I'm not physically sitting at, and which don't always have a great deal of memory. I used to use Nano for making small changes to files, Gedit for web design and Python, and Eclipse for C++. Since learning to use Vim, I now use it for everything.

There is a steep learning curve at the beginning. It took me a long time to get used to having two different modes for editing and reviewing, and I still have trouble navigating around the text - if you use the arrow key to get to the end of a line, you don't automatically end up on the next line, which I find very jarring. However, the useful features of Vim far outweigh the minor inconveniences.

The basics

To go into "insert" mode, which lets you edit the document, press i. To get back into "normal" mode, where you can enter commands, press Esc.

To save, go into normal mode and type :w, then press enter. To quit, use :q, or to quit without saving, type :q!. You can also string the two commands together and type :wq to save and quit.

All of the following commands are to be typed in normal mode. If a command begins with a colon, you have to press enter before the command will execute.

Undo and redo

Press u to undo, and Ctrl-R to redo.

Copy and paste

To copy a line, press Y, and then to paste either press p to paste below the current line, or P to paste above. To cut a line, type dd.

To highlight a block of text, either type v to highlight by the character, or type V to highlight by the line. You can then copy the text by pressing y, or cut the text by pressing d. This text can then be pasted as above.

Find and replace

You can use regular expressions in Vim to find and replace text. The simplest one, which I use the most often, is :%s/foo/bar/g to replace each instance of "foo" with "bar".

Smart indent

To indent your whole file at once, type gg=G, or type == to indent the current line.

Finding words

Type /word to find each instance of "word" in your file. Press n to move to the next instance, or N to move to the previous instance. Once you're finished, you can turn off the highlight by typing :noh.

Autocomplete

Autocomplete words, commands or variable names using Ctrl-N.

Opening multiple files in tabs

If you're editing a few files at a time, you can open them all up at the same time and switch between them using tabs. Use :tabe filename to open a file in a new tab, then to switch between tabs use :tabn for the next tab and :tabp for the previous tab. You can see the tabs at the top of the screen. If you want to close a tab, use :q as normal.

Vim is really the Dwarf Fortress of text editors: there are layers and layers to it that I haven't yet discovered. I have the Vim graphical cheat sheet printed out and pinned up next to my desk, and I find if I google something like "Vim record macro" there will usually be several helpful tutorials available.

Happy coding!

...

One thought on “Get some vim and vigour”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.