• Stars
    star
    237
  • Rank 169,885 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Control a vim instance through ruby code

Vimrunner Build Status

Using Vim's client/server functionality, this library exposes a way to spawn a Vim instance and control it programatically. Apart from being a fun party trick, this can be used to do integration testing on Vimscript.

Demo

The latest stable documentation can be found on rubydoc.info.

Any issue reports or contributions are very welcome on the GitHub issue tracker.

Usage

If you don't already have a running Vim server, Vimrunner can be used in one of two main ways:

# Vim will automatically be started and killed.
Vimrunner.start do |vim|
  vim.edit "file.txt"
  vim.insert "Hello"
  vim.write
end
# Vim will automatically be started but you must manually kill it when you are
# finished.
vim = Vimrunner.start
vim.edit "file.txt"
vim.insert "Hello"
vim.write
vim.kill

Vimrunner will attempt to start up the most suitable version of Vim available, meaning one of the following:

  • vim if it supports headlessly creating servers (see Requirements below for more information);
  • mvim if you are on Mac OS X;
  • gvim.

If you wish to always start a GUI Vim (viz. skip using a headless vim) then you can use start_gvim like so:

Vimrunner.start_gvim do |vim|
  # ...
end

If you require an even more specific version of Vim, you can pass the path to it by instantiating your own Server instance like so:

Vimrunner::Server.new(:executable => "/path/to/my/specific/vim").start do |vim|
  vim.edit "file.txt"
end

(You can also use the non-block form of start in both of the above examples.)

Calling start (or start_gvim) will return a Client instance with which you can control Vim. For a full list of methods you can invoke on the remote Vim instance, check out the Client documentation.

If you already have a remote-capable Vim server running, you can connect Vimrunner to it directly by using Vimrunner.connect or Vimrunner.connect! like so:

# Assuming a running Vim server called FOO...
vim = Vimrunner.connect("FOO")
if vim
  vim.insert("Hello world!")
end

# Or, if you're confident there's a running server...
vim = Vimrunner.connect!("FOO")
vim.insert("Hello world!")

In case of failure to find the server FOO, the first form will return nil, while the second form will raise an exception.

Testing

If you're using Vimrunner for testing vim plugins, a simple way to get up and running is by requiring the vimrunner/rspec file. With that, your spec_helper.rb would look like this:

require 'vimrunner'
require 'vimrunner/rspec'

Vimrunner::RSpec.configure do |config|
  # Use a single Vim instance for the test suite. Set to false to use an
  # instance per test (slower, but can be easier to manage).
  config.reuse_server = true

  # Decide how to start a Vim instance. In this block, an instance should be
  # spawned and set up with anything project-specific.
  config.start_vim do
    vim = Vimrunner.start

    # Or, start a GUI instance:
    # vim = Vimrunner.start_gvim

    # Setup your plugin in the Vim instance
    plugin_path = File.expand_path('../..', __FILE__)
    vim.add_plugin(plugin_path, 'plugin/my_plugin.vim')

    # The returned value is the Client available in the tests.
    vim
  end
end

This will result in:

  • A vim helper in every rspec example, returning the configured Vimrunner::Client instance.
  • Every example is executed in a separate temporary directory to make it easier to manipulate files.
  • A few helper methods from the Vimrunner::Testing module (documentation).

The specs would then look something like this:

require 'spec_helper'

describe "My Vim plugin" do
  specify "some behaviour" do
    write_file('test.rb', <<-EOF)
      def foo
        bar
      end
    EOF

    vim.edit 'test.rb'
    do_plugin_related_stuff_with(vim)
    vim.write

    IO.read('test.rb').should eq normalize_string_indent(<<-EOF)
      def bar
        foo
      end
    EOF
  end
end

If you need a different setup, please look through the file lib/vimrunner/rspec.rb for ideas on how to build your own testing scaffold.

Requirements

Vim needs to be compiled with +clientserver. This should be available with the normal, big and huge featuresets or by using MacVim on Mac OS X. In order to start a server without a GUI, you will also need +xterm-clipboard as described in the Vim manual.

The client/server functionality (regrettably) needs a running X server to function, even without a GUI. This means that if you're using it for automated tests on a remote server, you'll probably need to start it with Xvfb.

If you are using MacVim, note that you will need the mvim binary in your PATH in order to start and communicate with Vim servers.

Experimenting

The vimrunner executable opens up an irb session with $vim set to a running gvim (or mvim) client. You can use this for interactive experimentation. A few things you can try:

$vim.edit 'some_file_name'  # edit a file
$vim.insert 'Hello, World!' # enter insert mode and write some text
$vim.normal 'T,'            # go back to the nearest comma
$vim.type 'a<cr>'           # append a newline after the comma
$vim.write                  # write file to disk

More Repositories

1

splitjoin.vim

Switch between single-line and multiline forms of code
Vim Script
1,918
star
2

switch.vim

A simple Vim plugin to switch segments of text with predefined replacements
Ruby
648
star
3

sideways.vim

A Vim plugin to move function arguments (and other delimited-by-something items) left and right.
Ruby
480
star
4

linediff.vim

A vim plugin to perform diffs on blocks of code
Vim Script
463
star
5

tagalong.vim

Change an HTML(ish) opening tag and take the closing one along as well
Vim Script
412
star
6

inline_edit.vim

Edit code that's embedded within other code
Vim Script
150
star
7

typewriter.vim

Make cool typewriter sounds in insert mode
Vim Script
88
star
8

bufferize.vim

Execute a :command and show the output in a temporary buffer
Vim Script
83
star
9

Vimfiles

My .vim folder
Vim Script
79
star
10

deleft.vim

Delete a wrapping if-clause, try-catch block, etc. and shift left.
Vim Script
71
star
11

quickpeek.vim

Show a preview popup on quickfix entries
Vim Script
70
star
12

undoquit.vim

Undo a :quit -- reopen the last window you closed
Ruby
64
star
13

diffurcate.vim

Split a git diff into separate files
Ruby
59
star
14

dsf.vim

Delete surrounding function call
Ruby
52
star
15

id3.vim

"Edit" mp3 files with Vim, or rather, their ID3 tags
Vim Script
51
star
16

ember_tools.vim

Tools for working with ember projects
Vim Script
51
star
17

writable_search.vim

Grep for something, then write the original files directly through the search results.
Vim Script
51
star
18

quickmd

Quickly preview a markdown file
Rust
37
star
19

gnugo.vim

Play a game of Go in your text editor, using GnuGo
Vim Script
33
star
20

discotheque.vim

Emphasize pieces of text, with style.
Vim Script
32
star
21

simple_bookmarks.vim

A small plugin to create named bookmarks in Vim
Vim Script
31
star
22

gapply.vim

Before committing, edit a git diff and apply it directly to the index
Vim Script
28
star
23

whitespaste.vim

Automatically adjust number of blank lines when pasting
Vim Script
26
star
24

image-processing

Some experiments with simple image processing algorithms
Ruby
22
star
25

rails_extra.vim

Some extra tools for working with Rails projects, on top of vim-rails
Vim Script
21
star
26

id3-image

A tool to embed images into mp3 files
Rust
18
star
27

vim-eco

Eco (embedded coffee-script) support for Vim
Vim Script
17
star
28

strftime.vim

Make it easier to read and write strftime strings
Vim Script
16
star
29

exercism.vim

Vim plugin to help out with exercism.io. A thin wrapper around the `exercism` command-line
Vim Script
15
star
30

dealwithit.vim

Show the "deal with it" dog animation in Vim
Vim Script
14
star
31

tagfinder.vim

A simple vim plugin to look for tags of specific kinds: classes, functions, etc.
Vim Script
13
star
32

whatif.vim

What if we could see which if-else branch gets executed? Haha jk... unless?
Ruby
12
star
33

ghundle

A package manager for git hooks
Ruby
11
star
34

yankwin.vim

Yank and paste windows around
Ruby
9
star
35

waiting-on-rails

Bored of waiting on "rails server"? No more!
Ruby
9
star
36

cucumber-vimscript

Cucumber step definitions for testing vimscript
Ruby
9
star
37

scripts

Small scripts to solve minor problems
Ruby
9
star
38

qftools.vim

Tools to manipulate the quickfix window
Vim Script
8
star
39

vim-lectures

Lectures for the Vim course in FMI
Vim Script
8
star
40

id3-json

Read and write ID3 tags with machine-readable input and output
Rust
7
star
41

progressor

Measure iterations in a long-running task
Ruby
7
star
42

popup_scrollbar.vim

A scrollbar for Vim windows built with the popup API
Vim Script
7
star
43

coffee_tools.vim

A work-in-progress plugin with tools for working with coffeescript
Vim Script
7
star
44

dotfiles

My linux configuration files
Shell
6
star
45

awesome-config

My awesome configuration ("awesome" as in the window manager, not as a quality)
Lua
5
star
46

better_netrw.vim

A better file manager for Vim
Vim Script
5
star
47

vim-fmi

The site for the Vim course at Sofia University: https://vim-fmi.bg/
Ruby
5
star
48

dot-shell

My zsh configuration
Shell
5
star
49

xmonad-config

My personal xmonad configuration (not maintained, switched to awesome)
Haskell
5
star
50

andrews_nerdtree.vim

My personal collection of NERDTree extensions
Vim Script
4
star
51

ginitpull.vim

Open a pull request directly from Vim
Vim Script
4
star
52

iseven.vim

Check if a number is even or not
Vim Script
4
star
53

rtranslate.vim

Easier translations with rails
Vim Script
3
star
54

archivitor.vim

Vim plugin for editing the contents of archives
Vim Script
3
star
55

libmarks

A small website to bookmark programming libraries. Inspired by The Ruby Toolbox
Ruby
3
star
56

daily-sites

A website to administer links to regularly visited sites
Ruby
3
star
57

simple_gl

A thin wrapper around ruby's opengl bindings
Ruby
3
star
58

rimplement.vim

Implement a ruby class or method.
Vim Script
2
star
59

rust-shooter

A toy game in Rust
Rust
2
star
60

egui-mp3s

A simple egui demo app
Rust
2
star
61

vim-learning-website

A website that uses the vimgolf gem for vim exercises.
Ruby
2
star
62

modsearch.vim

A command that lets you change the previous search in predefined ways
Ruby
2
star
63

rust-hangman

A simple game of hangman, built for learning purposes
Rust
2
star
64

do-after

A small C program to execute something after a set amount of time.
C
2
star
65

ctags_reader

Read ctags files, provide a ruby interface to get data out of them
Ruby
2
star
66

rust-spotiferris

A Rust web experiment -- a music management app (very incomplete)
Rust
2
star
67

subtitles.vim

(Not working) A vim plugin that helps you with editing subtitles
Vim Script
2
star
68

randfiles

A tool that outputs a list of random files in given directories
Ruby
2
star
69

rustbucket.vim

[WIP] A collection of Rust tools for Vim
Vim Script
2
star
70

gtk4-example

A very basic Rust example of using GTK4
Rust
2
star
71

rust_wrap.vim

Ok-wrap (Some-wrap, Rc-wrap, etc.) Rust functions
Vim Script
2
star
72

bioinformatics-experiments

Sandbox to play around with bioinformatics-related stuff
Python
2
star
73

onchange.vim

Trigger an autocommand upon a code change
Vim Script
2
star
74

digits

Simple experimental project that recognizes a digit in a given BMP image
C++
1
star
75

vsnips.vim

An early stage experiment with snippets
Vim Script
1
star
76

hello-rusty-web

Some example code for a web application in Rust
Rust
1
star
77

rust-bookworm

A toy project that indexes txt-formatted books and allows searching through them
Rust
1
star
78

mount_archive

Mount an archive as a virtual filesystem
Ruby
1
star
79

declarators

Useful method decorators for ruby
Ruby
1
star
80

vim-fmi-cli

The command-line tool for https://vim-fmi.bg
Rust
1
star
81

rust-eliza

A simple chatbot in Rust. Not intended for "serious" use (whatever the serious use for a chatbot might be), just an experiment.
Rust
1
star
82

tdd_workshop

Python
1
star
83

sticky_line.vim

[WIP] Pin lines to the window to always keep them visible while scrolling
Vim Script
1
star
84

protein-runway

Integrated Bioinformatics Project
Python
1
star