Cross platform vim script development

I’ve developed vim-project on my Mac and I wanted to be sure that it worked on Windows too. I bought a Windows 8 license some time ago for only 29,99 € to update my Acer from Windows Vista. I could try the plugin in my Acer, but I wanted to have something similar to a Vagrant setup. So I’ve created a VirtualBox for Windows 8, but I’ve encountered some obstacles.

Uninstall windows 8

You can have only one installation of Windows 8. So I followed the instructions to uninstall the Windows 8 license from my Acer from How-To Geek

It wasn’t enough. I had to call Microsoft to activate Window 8.

Windows 8 upgrade with an empty disk

If you install windows 8 on an empty partition, you cannot activate your Windows 8. I have found how to fix the problem in the Microsoft forum

Installation of Vim

I’ve installed this version of vim

Setup of vundle in Windows

I’ve followed the instructions from Vundle for Windows to setup Git and Curl.

Shared Folder with VirtualBox

This is the hard part. I wanted to be able to modify my plugin from MacVim and then restart Gvim to test it.

To achive this, you need to configure a VirtualBox shared folder. Then you need to setup a network unit in Windows. The problem is that the network unit must be visible by all users, because the symbolic links are created as Administrator while vim is started as normal user.

I’ve found the solution on stackoverflow:

For this hack you will need psexec SysinternalsSuite by Mark Russinovich:

  1. Open an elevated cmd.exe prompt (Run as administrator). Press the right command. Write cmd and then <C-S-CR> (ctrl-shit-enter).

  2. Elevate again to root using PSExec.exe: Navigate to the folder containing SysinternalsSuite and execute the following command psexec -i -s cmd.exe you are now inside of a prompt that is nt authority\system and you can prove this by typing whoami. The -i is needed because drive mappings need to interact with the user

  3. Create the persistent mapped drive as the SYSTEM account with the following command net use z: \\VBOXSVR\gvim /persistent:yes

Now you can create the links, but you have to do this hack everytime you restart Windows.

Creating symbolic links in NTFS

My Gvim setup is in /Users/amiorin/Code/gvim, gvim from now on. gvim/_vimrc is a link to ~/.vimrc. vimfiles is not a link, but a real directory, because I use Vundle. But gvim/vimfiles/bundle/vim-project is a link to ~/.vim/bundle/vim-project. gvim is Z: in Windows 8.

Using an elevated cmd.exe (<C-S-CR)

1
2
3
4
5
cd %USERPROFILE%
mklink _vimrc z:\_vimrc
mklink /D vimfiles z:\vimfiles
# Vundle uses .vim also on Windows.
mklink /D .vim vimfiles

Inside Gvim

1
BundleInstall

Then I removed my plugin from vimfiles/bundle and I have created a link from ~/.vim/bundle/vim-project to be able to use the same code in MacVim and Gvim.

Powerline with Consolas font

Here you find a patched consolas for powerline

1
set guifont=Consolas\ for\ Powerline\ FixedD:h12

Conditional .vimrc

Some configurations are specific to one manchine. I’ve used the function hostname() in this way.

1
2
3
4
5
if hostname() ==# 'air.local'
  set guifont=Menlo\ for\ Powerline:h12
elseif hostname() ==# 'TOSHIBA'
  set guifont=Consolas\ for\ Powerline\ FixedD:h12
endif

air.local is the Mac and TOSHIBA is the Windows 8.

Conclusion

With this setup I can write my plugins in MacVim and test them on Gvim and MacVim at the same time without any copy and paste of files or any commit and pull. I could use Gvim, but I use a lot of shortcuts starting with the left command in MacVim and the left command used by VirtualBox is a dead key inside the guest os.

A Vim plugin for editing fenced code blocks

Introduction

Fenced code blocks are a very useful feature of GitHub Flavored Markdown. With this plugin you can edit the fragment of code embedded inside the markdown with the right filetype.

Demo

Install

If you use Vundle you can install this plugin using Vim command :BundleInstall amiorin/vim-fenced-code-blocks. Don’t forget put a line Bundle 'amiorin/vim-fenced-code-blocks' in your .vimrc.

If you use Pathogen, you just execute following:

1
2
cd ~/.vim/bundle
git clone https://github.com/amiorin/vim-fenced-code-blocks.git

If you don’t use either plugin management system, copy the plugin directory to your .vim directory.

*nix: $HOME/.vim Windows: $HOME/vimfiles

Use

This plugin defines 4 new commands:

  • :E to edit in the same window, CTRL-^ to go back to the markdown file
  • :EV to edit in the vertical split window, CTRL-W l to go back to the markdown file
  • :ES to edit in the horizontal split window, CTRL-W j to go back to the markdown file
  • :ET to edit in a new tab.

All the commands accept the bang.

Self-Promotion

The Zen of Wiki with Gollum

I’ve always loved wikis. They are very useful for bookmarks and snippets. After many years of Dokuwiki, I switched to Gollum for these reasons:

  • I can use Vim to edit my wiki pages.
  • I can use Ack for searching.
  • I can read/edit it on Github, when I’m not on my MacBook.

Setup

Create a repository in Github (I use the repository of my blog). Click wikiGit Access. Clone the wiki and you are done.

Edit

If you want to edit your wiki in Chrome with Gollum, read this article. If you prefer to use Vim, I suggest you to install these plugins with Vundle:

1
2
3
4
5
6
" open links in chrome
Bundle 'tyru/open-browser.vim'
" syntax highlight
Bundle 'tpope/vim-markdown'
" follow internal links of the wiki
Bundle 'mmai/wikilink'

Reloadlive

If you want to see the wiki page rendered in your browser while you are writing it, I have written a gem called Reloadlive.

1
2
3
$ gem install reloadlive
# inside the directory of gollum
$ reloadlive

Reloadlive demo

Sync

It’s very boring to do every time you modify your wiki:

1
2
3
4
git pull
git add -A
git commit -m "minor changes"
git push

But if you use Rvm, you can create a .rvmrc in the root of your wiki:

1
2
3
4
# .rvmrc
if [[ $- == *i* ]] # check for interactive shells
then git pull && git add -A && git commit -m "minor changes" && git push
fi

Every time you cd, Rvm looks for a file called .rvmrc. If it finds it, it executes it. If you don’t use Rvm, you can use Autoenv.

1
2
# alias to sync when you are already in the root of the wiki.
alias gsync="sh ./.rvmrc"

Conclusion

  • No database.
  • Password protected if you need.
  • Distribuited.
  • Autosync.
  • It works offline.
  • Vim editing instead of the textarea.
  • Regex search with Ack.
  • Instant preview in the browser.
  • Markdown language like in Jekyll.

I’m ok for the next 10 years. :-)

Tab Pages in Vim

This tip works only with MacVim (or GVim).

In Vim you can use one tab page per file, like in other editors, but I think it doesn’t work very well. I prefer to use one tab page for every project. I used to use a MacVim window for project, but the copy and paste was a nightmare.

The rule is simple. Every tab page is a project and it has a [local] working directory that is displayed in the guitablabel.

The code

1
2
3
4
5
6
7
" IMPORTANT! Inside your .gvimrc
augroup cwd
  au!
  au CursorMoved,WinEnter * call settabvar(tabpagenr(),'cwd', substitute(getcwd(), $HOME, '~', ''))
augroup END
set guitablabel=%{gettabvar(v:lnum,'cwd')}
set showtabline=1

Event DirEnter and DirLeave

I would like to see in the next versions of Vim these two events. They would be very useful to write new plugins project-oriented. In the meanwhile I’ve downloaded the source code of Vim. After ten years, maybe I’ll try again to write some C code again.

For more info :h autocommand-events and :h tabpages.

vim

PowerToys for Mac OS X

Fasd

Command-line productivity booster.

Zsh

Prompt with 2 lines.
Advanced completion cp README.md ,octo<Tab>cp README.md /Users/amiorin/Code/octopress.

iTerm2

256 colors and Solized.

MacVim

Best editor.

Divvy

Good window manager.

Fluid

Turn Your Favorite Web Apps into Real Mac Apps.
Cookie Storage for separeted from Safari ⇒ Multiple Gmail accounts without problems.

Due

Good reminder.

MagicPrefs

Better MacBook Trackpad.

Skitch

Annotated screenshots.

Homebrew

The missing package manager for OS X.

Rvm

RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.

Git

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Pow

Pow is a zero-config Rack server for Mac OS X.

Zeus

Boot any rails app in under a second.

GitX

GitX-dev is a fork (variant) of GitX, a long-defunct GUI for the git version-control system. It has been maintained and enhanced with productivity and friendliness oriented changes, with effort focused on making a first-class, maintainable tool for today’s active developers.

TotalFinder

Finder sucks.

Sequel Pro

Sequel Pro is a fast, easy-to-use Mac database management application for working with MySQL databases.

osx

Autoread and autowrite in Vim

What

Don’t save or reload your buffers. Vim can do that for you.

How

From my .vimrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
" autoread and autowrite
augroup save
  au!
  au FocusLost * wall
augroup END
set nohidden
set nobackup
set noswapfile
set nowritebackup
set autoread
set autowrite
set autowriteall

" persistent-undo
set undodir=~/.vim/undo
set undofile

Why

:w<CR> and <D-s> take too much time. We should not be worried of data loss, because we have Git and :h persistent-undo.

Additionaly how many times it’s happened to waste time in Chrome, reloading the page n-times, because we had forgot to save in Vim?

Using nohidden, autowrite and update we are sure that everything is saved.

1
2
# don't forget to create the directory for the undo files
mkdir ~/.vim/undo

You don’t need the scratch plugin

You would like to try a VimL snippet, create a scratch.vim somewhere. Use a global mark mS (S is scratch).

Now with 'S you are in your scratch buffer.

Use vim-eval to evaluate (<C-c) the file, the region or the line.

Other global marks that I use

  • 'V my .vimrc
  • 'T my todos
  • '0 last file edited

But the undo is not user-friendly

Yes, you’re right, try Losh’s gundo

1
Bundle 'sjl/gundo.vim'

The command is GundoToggle.

vim