Vim 401: Extending Vim and More

1695

Now that you’ve learned your way around Vim, I wanted to focus the last piece in the series on extending Vim with scripts and plugins, and some other features that users should be aware of.

If you’ve been following this series so far, you know how to move around in Vim, setting up mappings and abbreviations, and configuring Vim and calling external commands. Now let’s take a look at scripts, plugins, and the Vim GUI.

Scripts and Plugins

Vim isn’t limited to what you get “out of the box.” It’s possible to extend Vim pretty heavily, and many people have added features to Vim far and above plain old text editing.

You already know you can edit the .vimrc to customize Vim’s behavior. By adding scripts and plugins, you can not only customize existing features, you can add features or modify Vim in ways that you can’t do through the .vimrc.

Let’s start with installing scripts. In your home directory, you should have a .vim directory. Under that directory, you should have a scripts and plugin directory. If not, go ahead and create them.

What’s the difference between a script and a plugin? Basically this: A plugin is loaded automatically when Vim starts. Scripts have to be explicitly called–which can be done through the .vimrc like this:

autocmd FileType html,xml,xsl source ~/.vim/scripts/closetag.vim 

Or you might want to use mapping to set up a shortcut to call a script from time to time.

To install a script or plugin, just put them in the appropriate directory. Writing scripts is a bit beyond this article, but you can find a ton of pre-written scripts on Vim.org. If there’s something you can think of that you want to do with Vim, odds are someone has already written a script to do it.

Let’s look at a quick example of installing a plugin. One plugin I really like is Bash Support, which turns Vim into a “Bash IDE” for writing Bash scripts.

To install it, grab the file bash-support.vim and put it in your .vim/plugin directory. Some plugins require some additional configuration to be added to your .vimrc. In the case of bash-support.vim, you need to define your name, email, and company variable:

let g:BASH_AuthorName   = 'First Last'
let g:BASH_Email = ' This e-mail address is being protected from spambots. You need JavaScript enabled to view it '
let g:BASH_Company = 'Acme Corp'

Vim won’t automatically load it, but when you restart Vim it will load it and magic will start to happen. When you open a new file ending in .sh, Vim will automatically create a header for your script that includes the date, the information here, and so on.

There are literally thousands of Vim scripts floating around. From useful tools like the Bash Support plugin, to TwitVim–a script to use Vim to post to Twitter.

One important note–some scripts you find online may be in a DOS file format instead of UNIX. If this is the case, Vim on Linux will throw some errors at you when starting Vim because it tries to interpret the line endings as commands. To fix this, use the dos2unix command to convert the file, which should be installed by default on most distros or available from the repos.

Bookmarking in Vim

When working on a really long document or when programming, you might have sections you revisit frequently. One way to move back and forth is to search for specific terms that might be in the section you want to revisit. Another, easier, way is to use Vim’s marks to set bookmarks.

This is really easy to do. When you’re in a section where you’d like to set a bookmark, just type m in command mode, followed by a letter to use as the bookmark. So, you might type ma to set the first, mb to set the second, etc. But whatever is easiest to remember–Vim doesn’t require that the bookmarks be set in sequential or any order.

After you’ve set the bookmark, type the backtick character and then the letter for the bookmark, ‘n where n is the letter you’ve set for the bookmark. So to go to mark “a” type ‘a and so on.

Note that marks are buffer-specific. So if you have multiple tabs open, or if you’re editing two documents in split viewports, you can have two a marks, and so on–setting a mark in one buffer won’t carry over to another.

You can set global bookmarks with upper-case letters, but the behavior is a bit unexpected. If you have two (or more) buffers open and run mA on one, you’ll set a mark named A. Going into another buffer, you can return to the previous mark with `A but Vim will want to close the open buffer first. So it’s possible to set global marks, but the default behavior is a bit odd.

After hours of frenzied editing, it’s possible you might forget where some of the marks are. No problem, just run :marks in command mode. You’ll see something like this:

mark line  col file/text
' 127 4 if &filetype == "html" || exists("b:closetag_html_style")
a 127 4 if &filetype == "html" || exists("b:closetag_html_style")
b 310 0 function! s:Pop(sname)
z 321 0 call s:RestoreKeywords()
0 352 30 /usr/share/vim/vim72/doc/starting.txt
1 1 0 ~/.vimrc
2 5 533 ~/Desktop/OStatic/christmas_alek.html
" 1 0 " File: closetag.vim
[ 1 0 " File: closetag.vim
] 327 0 endfunction

That shows manually set marks as well as some automatic marks (files edited previously).

GUI Vim

Yes, Virginia, Vim does have a GUI. On most Linux distros, this isn’t installed by default. To get Vim’s GUI (we’ll just say GVim from here out) search your package repositories for gvim.

For instance, if you want to get GVim on openSUSE, you can grab it by searching for gvim in the Software Manager, or just run:

sudo zypper in gvim

To run GVim, you can either find it in your desktop’s application menu or run gvim at the terminal or using your desktop’s “Run Application” dialog. (Usually brought up with Alt+F2, depending on the desktop you’re using.)

Since GVim uses the same commands as Vim, what you already know about Vim applies immediately to GVim. One thing that’s useful for learning Vim is that most of the menus expose the Vim commands needed to perform operations. So, for instance, if you click on the File menu, it will show you the commands for most of the operations available under that menu. This might make it easier to pick up commands by discovery.

Why did I put this in the “advanced” piece instead of one of the other tutorials? A couple of reasons. I think it’s important to know the basics before using a “crutch,” as it were. You can use GVim to hide some of the complexity of Vim which is great for casual use, but it’s not terribly effective for a learning situation. Plus, you can make use of Vim in almost any situation–whereas GVim requires a desktop session. Not very handy if you need to SSH into a remote system and do some troubleshooting or system configuration.

Many scripts extend the GUI to add menus to make editing specific types of files easier, and so on. One that you might find useful if you spend time working with HTML is the HTML menu for GVim. While it may not be an inventive name, it is descriptive! Just copy the file to your .vim/plugin directory and then add the following to your .vimrc:

source ~.vim/plugin/vim_menu_HTMLpol

I tend to prefer the standard version of Vim, but take GVim for a spin and see if it suits your working style.

Finally, a word about Vim’s charity. Vim is actually “charityware“–its license is considered GPL-compatible, but it includes an added request of users to consider making a donation to help needy children in Uganda. Actually, you don’t need to be a Vim user to do this, but if you are finding Vim to be helpful, you might consider chipping in.

I hope that you’ve found the Vim tutorials helpful. While we haven’t covered everything in Vim, this should be sufficient to get you started with Vim and eventually master it.