Code Substitution Done Right - Switch.vim

Switch.vim

Switch.vim by Andrew Radev is a highly customizable plugin that will help you do substitutions faster than ever.

Usage

The plugin is simple to use, since it has one main entry point - the command :Switch. It’s highly encouraging to map it to a key, as the author suggests in the documentation:

1
nnoremap - :Switch<cr>

It comes with support for Ruby, PHP, Eruby, C and CoffeeScript. However, you can easily add your own switch definitions and make it even more powerful.

You can switch between the following definitions by invoking the :Switch command. Here are some of the built-ins:

  • Boolean conditions:
1
2
foo && bar
foo || bar
  • Boolean constants:
1
2
3
4
5
flag = true
flag = false

flag = True
flag = False
  • Hash style:
1
2
foo = { :one => 'two' }
foo = { one: 'two' }
  • If-clauses:
1
2
3
4
5
6
7
8
9
10
11
if predicate?
  puts 'Hello, World!'
end

if true or (predicate?)
  puts 'Hello, World!'
end

if false and (predicate?)
  puts 'Hello, World!'
end
  • Rspec should/should_not:
1
2
1.should eq 1
1.should_not eq 1
  • String style:
1
2
3
foo = 'bar'
foo = "baz"
foo = :baz
  • Coffeescript arrows
1
2
functionCall (foo) ->
functionCall (foo) =>

Customizations

Adding your own definitions is as easy as:

1
2
3
4
let g:switch_definitions =
    \ [
    \   ['if', 'unless']
    \ ]

Now you can easily switch between if and unless:

1
2
3
4
5
6
7
if foo
  # Do something
end

unless foo
  # Do something
end

View the documentation for more advanced examples.

Screencast

You can check out this awesome screencast created by Andrew to see the plugin in action:

About the author

Veselin Todorov is a software consultant focused on testing, code quality and building awesome products. He is helping companies to ship their products with confidence and style.

If you are passionate about JavaScript, Node.js, TDD, building and growing applications, you should visit his blog at RobustJS.com