Tips: Making Vim easy

4395

Author: Joe 'Zonker' Brockmeier

Vim’s flexibility and countless features are a major asset for experienced users, but a challenge for newbies. If you’ve always wanted to try Vim but were put off by your first attempts, you can start off gradually by getting to know Vim’s GUI and easy mode. This article is a primer for those who haven’t used Vim much and want to wade in gradually.

If you’re new to Vim and vi-like editors, the Vim GUI is probably the best way to get started. You can perform the operations you’re familiar with using Vim’s keybindings, but when you get stuck, you can use the mouse and the gVim menus to get things done quickly and efficiently.

Even better, when there is a keybinding for a command, it’s usually displayed in the gVim menu — so if you keep performing the same operation, you’ll probably pick up the keybinding through repetition and eventually be able to go mouseless — if that’s your goal, of course.

There’s little, if anything, you can do using gVim that you can’t do in the standard version of Vim, and many operations are easier or more intuitive when you have menus and icons to deal with.

Staring gVim

To start gVim, run gvim or vim -g. If you want to edit a file with gVim right away, you can launch gVim with gvim filename or vim -g filename.

As with Vim, you can also launch gVim with the +n option, where n is the line number you want to start on. So, for instance, gvim +10 filename will bring up gVim with filename loaded and the cursor on line 10.

Another trick is to load gVim at the first instance of a search term. Let’s say you want to start editing at the first instance of the word foobar. Use:

gvim +/foobar filename

Once you start gVim, you’ll notice that it looks more or less like any other GUI text editor. Like any other editor, it provides several menus, a scrollbar on the right-hand side of the window (which can be moved, or done away with, if desired), and a toolbar at the top of the window for quick access to common functions like Save, Undo, and Search.

Easy Vim

Vim also has an “easy” mode, which isn’t touched on too often in Vim tutorials and docs, since it really goes against what most users are looking for from Vim. Vim, as you know if you’ve used it much, is a “modal” editor, based on Bill Joy’s classic vi. Vim has an insert mode, a command mode, a visual mode, and (depending on whom you ask) a few additional modes. Once you get the hang of it, the modal nature of Vim is extremely useful. However, until you get used to it, Vim can be a hassle when trying to figure out simple operations like searching and replacing text.

Easy mode is actually a collection of various Vim options that tell Vim to act like any standard text editor. Instead of using y to “yank” (copy) text, and then pasting text using p or P in normal mode, Vim’s easy mode lets you use the standard Ctrl-c to copy text and Ctrl-v to paste text. Anybody who has use Notepad or Microsoft Word for a week or two will be familiar with these shortcuts, and emulating them makes Vim more intuitive for the average user. Check Vim’s help for more on easy mode to find out exactly what Vim’s doing behind the scenes.

Why would you want to use Vim without access to its modes? Well, Vim still includes a number of spiffy tools and features that aren’t dependant on its vi-like modes — macros, syntax highlighting, sessions, and other Vim goodness that you might not find in an editor like Kate or gedit.

Easy mode also enables a context menu, which isn’t present in standard Vim/gVim. When you right-click in the gVim window, you’ll see a menu that will allow you to select text by the word, sentence, paragraph, line, or all text in the buffer. You can also use the context menu to copy, cut, delete, and paste text, and to undo operations.

If you like the context menu, but don’t want to run Vim in easy mode, you can simply set the mouse behavior using this command:

:behave mswin

Then you’ll have the context menu, even when you’re not in easy mode.

By default, pressing Esc in easy mode will not put you in command mode. If you decide you want to start using gVim in regular mode, you can go to Edit -> Global Settings -> Toggle Insert Mode, which will put you into command mode. After running this, you’ll be able to switch between easy mode and normal gVim/Vim modes. If you want to drop back exclusively to easy mode, toggle it with the menu or run :set im! in command mode.

The rest of this article will assume that you’re using gVim in normal mode, but I wanted to point out the easy gVim mode for users who might like to get started with Vim is the friendliest manner possible.

By default, gVim will start with a conservative color scheme of black text on a white background. If you prefer something a bit more interesting, gVim has several color schemes available. Go to the Edit menu and select Color Scheme. You should see about a dozen choices, including blue, darkblue, and peachpuff.

Working in gVim

This probably goes without saying, but since gVim is designed as a GUI text editor, you can select text using the mouse in addition to Vim’s usual text selection methods. This means that all you need to do to highlight a letter, word, or sentence is to grab the mouse and highlight the text that you’d like to select. You can also place the cursor anywhere on the screen just by moving the mouse. (You can also enable this in regular Vim running in an xterm or other terminal emulator, but it requires mouse support being built in and is not on by default.)

However, don’t forget that the mouse is a clumsy tool compared to Vim’s normal selection capabilities. Using v you can start the visual mode for selecting by character, V to select by lines, and Ctrl-v to select text blocks.

It’s also worth noting that you have two clipboards in gVim. There’s the current selection, which means any text that’s highlighted, and there’s text copied to the clipboard, which persists after you’ve selected new text. For example, if you use the mouse to select a sentence and then click the middle button, you’ll paste the text that you just selected. But if you select text and use Vim’s Edit menu to copy text, it will be stored in the clipboard even when you select new text. This can be useful if you have a string that you’ll be pasting several times (say, a letter template) and need to use the standard selection method for other text.

I’ve written about Vim’s viewports before, but viewports are much easier to manage in gVim. You can use the normal Vim keybindings to open a viewport, or you can just go to Window -> New to create a new viewport so you can edit a new file, Window -> Split to split the window using the same file that’s already open, or Window -> Split Vertically to divide the viewport vertically instead of horizontally. Using Ctrl-w will move you from viewport to viewport, or you can use the Window menu to move to another viewport, or just click the left mouse button in the viewport you want the cursor to be in.

The really nice thing about viewports in gVim, though, is that you can easily resize them with the mouse. In standard Vim, you need to use Ctrl-w - and Ctrl-w + to resize the windows, but in gVim you can resize them using the mouse or the standard Vim keybindings, depending on which you find easier to use.

Even though gVim is a GUI-based program, the shell is never far away. To get to a shell from a gVim session, just :sh; type exit when you want to get out of the shell. Note that gVim’s terminal emulation may leave something to be desired in terms of screen refresh, so you probably want to stick to a regular terminal emulator for most of your work.

gVim’s file explorer

You can use gVim to edit remote files almost as easily as using Vim in a remote shell. I usually edit files on my server by using SSH to get a shell on the remote machine and then editing a file, but you can do the equivalent using gVim (or regular Vim) from your local session.

To open a remote file using secure copy (scp), do the following:

:e scp://user@remote.host.net/path/to/file

This will open a secure connection on the remote machine as the user specified. If you have an SSH key set up, you can log in without a password. If you don’t have a key set up, you will be prompted for a password each time you save the file, which can be a bit of an annoyance.

Note that this will work with several protocols, so you could substitute sftp, ftp, rsync, and other methods. See the Vim help guide for the complete list.

What if you don’t know the name of a remote file, or the exact path? Leave off the filename and you’ll be dropped into Vim’s file browser:

:e scp://user@remote.host.net/

You can browse through the directories and find whatever file you need to edit. Press Enter once the file you want is highlighted and start editing.

Finally, I would be remiss if I didn’t mention gVim’s spelling features. If you turn on behave mswin and turn on spellchecking, you can use the context menu to add a word to the dictionary, accept an alternate spelling, or ignore a word for spellchecking purposes. To turn on the spellcheck feature, go to Tools -> Spelling -> Spell Check On. You’ll probably need to turn syntax highlighting off for the spellcheck to work properly; you can do that from the Syntax menu.

If you’ve never used Vim, or if you’ve used Vim and found it too difficult, try gVim for a few days. It has all of the amenities that you’d want from a boring old text editor, but with all of Vim’s features lurking below the surface where you can unleash them at your leisure.