## Posts Tagged ‘Vim’

### Amending vim syntax

In Compilers this semester, we’re making heavy use of a powerful Scheme match macro which includes a ,[Cata -> Id] syntax for inline calls to helper functions. However, the default vim syntax highlighting for Scheme flags -> as a syntax error, because it is an invalid identifier. It also fails to syntax-highlight match itself, and a number of other helpful macros that we’re using, like define-who.

To get around this problem without write access to the actual syntax file itself (located at /usr/share/vim/vim72/syntax/scheme.vim in the burrow) you can simply create an additional syntax file called ~/.vim/after/syntax/scheme.vim. This file will be automatically loaded after the default Scheme syntax, whenever a Schemely buffer is loaded. I put the following lines in my file:

syn keyword schemeSyntax define-who match -> syntax region schemeMultilineComment start=/#|/ end=/|#/ contains=schemeMultilineComment

This also fixes the problem that, in the default Scheme mode, #| and |# are not treated as comment delimiters. Of course, you can add any personal macros to the list of syntax keywords.

One last problem: I’m using Aaron Hsu’s ChezWEB framework, and he uses .w as the extension for ChezWEB source files, which ought to be highlighted as Scheme source. Add this line to ~/.vimrc to load Scheme syntax whenever a .w buffer is loaded:

au BufNewFile,BufRead *.w set filetype=scheme

### vim protips

There are two schools of thought when it comes to text editing: vim and emacs. Although the “official” text editor of IU’s Computer Science department is emacs, I am a die-hard vim user. (If you have no idea what I’m talking about, this post isn’t for you.)

I think emacs has a perfectly fine control scheme, and is certainly a very powerful program, but I prefer the simplicity of vim’s modal input. Many vim users, however, seem unaware of many things that vim can do. (Many emacs users, similarly, seem to think vim isn’t capable of emacs-like tricks.)

But I am here to dispel these false notions! Here’s a list of many possibly-lesser-known vim tricks, many of which I use on a daily basis. They aren’t complicated, so you don’t need to know much about vim to figure them out. If you have any other tips, leave them in a comment. If you don’t know how to use vim but want to find out, type “vimtutor” at a command line and read the instructions.

Multipliers: Prefixing any command with a number causes that command to occur that many times. If you want to write the character ‘x’ 80 times, just type 80 i x [ESC] and the character x will be inserted 80 times. This works for all sorts of things.

Actions and motions: You might be unaware, but actions like delete (d), change (c), and yank (y) take “motions” as an argument of sorts. Motions include end of file (G), start of line (^), end of line (\$), next word (w), and matching brace (%). Executing any of these motions as a command causes the cursor to immediately jump to that location. (If the cursor is on a parenthesis, square bracket, or curly brace, % jumps you to the matching one, whether before or after.) Their true power comes when you combine them with actions, however: d% deletes the matching braces and everything in between; cG deletes the rest of the file and places you into insert mode; yw will copy the next word. In Scheme, I’ve found d% and y% indispensable for moving around S-expressions.

Repeat last command: The . command repeats the last thing you did. If your last action was to delete the current line, . will delete the new current line. That’s all.

Navigating directories/archives: If you open a directory or archive in vim, it will show a file listing. You can move up and down the list as usual, but if you hit Enter, it will open the file/directory that your cursor is on. If you’re in an archive, it extracts a copy into a temporary directory.

Make: The command :mak or :make runs “make” in the current directory, showing you the result. If there are any compile errors, vim will automagically jump your cursor to the first one, even if it’s in a different file. The command :cn gets you to the next error, and :cN to the previous. This drastically reduces the time it takes to compile and fix errors, and is definitely the most useful tip here for any C programmers.

TOhtml: Your files are syntax highlighted when you view them in vim. But what if you want a syntax-highlighted copy of your file to distribute to others? Use the command :TOhtml, which opens a new edit buffer with an HTML syntax-highlighted version of your file, ready to save. If you prefer CSS instead of <font> tags, which you should, then first use the command :let html_use_css=1 or, better yet, put it in your .vimrc file so it’s always set.

Searching: To find a string in a file, use / followed by that string. For instance, /char [Enter] will jump you to the next occurrence of “char.” The setting :set hlsearch (best placed in your .vimrc) causes vim to highlight every matching search occurrence in the file. Simply typing / [Enter] will get you to the next one. :noh turns off the highlighting.

Indentation: If you want to indent with spaces instead of tabs, set expandtab in your .vimrc. Either way, set both shiftwidth and tabstop to the desired size of your tabs. To reindent the current line, use the command ==. A single = is an action, so =G will reindent every line from the current one to the end of the file. gg=G will therefore reindent your entire file. (gg moves to the beginning of the file.)

Splitting: You can edit multiple files side-by-side in a single instance of vim. Type in :sp for a horizontal split, or :vs for a vertical split, followed by the filename you want to open. You can further split either of these new windows. ctrl-W followed by an arrow key (or h, j, k, l) moves your cursor between files. Commands like :e, :w, and :q only affect the active split. (For instance, to close one file and continue editing the others, type in :q while that window is active.

This is just a small subset of vim’s features. I try to learn something new every week. By slowly adding more features to your repertoire, you’ll get increasingly efficient with your editing, without any additional thinking. And after all, that’s the goal with any text editor.