I’ve used wikis for years. I’ve even written a few. I’d use one myself to manage my notes and ideas, but I hate typing in textareas and don’t want to launch a web server and new browser tab whenever an idea strikes. Ideally, I could just type a quick note in Vim, as I always have a command-line somewhere. Here are the three lines of code it took to make a single-user Wiki in Vim.
My first goal was to be able to hit a simple key combination to jump
between pages in the wiki. Ctrl-w seems like a good mnemonic — W is for
wiki, of course, and it’s not something I’m likely to hit accidentally. I can
also store pages as simple text files within a directory. All I needed to do
was to bind Ctrl-w to some sort of Vim command to jump between pages.
I ended up adding two lines to my .vimrc file:
map ^Ww <ESC>:args % <cword><CR>:n<CR> map ^Wi <ESC>:args % index<CR>:n<CR>
map binds an action to a key combination. Here, there are two
combinations, Ctrl-w w (or wiki-wiki) and Ctrl-w i (for wiki index). Both use
:args command to replace the current list of files
being edited. The first uses the current file (
%) and the file
named after the word under the current cursor position
<cword>), so Vim opens both files (creating the latter, at
least Vim-wise if it doesn’t exist), and moves to the new file
:n). The wiki index command uses the page named index
as the destination page.
Opening pages in this fashion allows the use of the
:N commands to move to the next and previous files in the
argument list, respectively.
To make this even more useful, I added a directory to hold all of my wiki
files (~/personal/vimwiki/) and a bash alias to launch a new Vim
session, starting with the index page:
alias vimwiki='pushd ~/personal/vimwiki; vi index'; popd
The only interesting point there is the use of
popd to make the command idempotent. To make Vimwiki simple and
generic (I could have aliases for multiple projects in separate directories),
I decided to use relative addressing when accessing pages.
It took longer to write this weblog than to write the code. Hooray for