sponsor Vim development Vim logo Vim Book Ad

switch.vim : Switch segments of text with predefined replacements

 script karma  Rating 0/0, Downloaded by 2731  Comments, bugs, improvements  Vim wiki

created by
Andrew Radev
script type
Github project: https://github.com/AndrewRadev/switch.vim
Screencast: http://youtu.be/zIOOLZJb87U

The main entry point of the plugin is a single command, ":Switch". When the command is executed, the plugin looks for one of a few specific patterns under the cursor and performs a substition depending on the pattern. For example, if the cursor is on the "true" in the following code:

    flag = true

Then, upon executing ":Switch", the "true" will turn into "false".

It is highly recommended to map the ":Switch" command to a key. For example, to map it to "-", place the following in your .vimrc:

    nnoremap - :Switch<cr>

Here's a list of some of the built-in switch definitions. To see all definitions, read the documentation. To see the actual definitions with their patterns and replacements, look at the file plugin/switch.vim.


  Boolean conditions:
      foo && bar
      foo || bar

  Boolean constants:
      flag = true
      flag = false

      flag = True
      flag = False


  Hash style:
      foo = { :one => 'two' }
      foo = { one: 'two' }

      if predicate?
        puts 'Hello, World!'

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

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

  Rspec should/should_not:
      1.should eq 1
      1.should_not eq 1

    foo = user.comments.map(&:author).first
    foo = user.comments.tap { |o| puts o.inspect }.map(&:author).first

  String style (Note that it only works for single-word strings):
    foo = 'bar'
    foo = "baz"
    foo = :baz


  "Echo" in tags:
      <?php "Text" ?>
      <?php echo "Text" ?>


      <% if predicate? %>
        <%= 'Hello, World!' %>
      <% end %>

      <% if true and (predicate?) %>
        <%= 'Hello, World!' %>
      <% end %>

      <% if false or (predicate?) %>
        <%= 'Hello, World!' %>
      <% end %>

  Tag type:
      <% something %>
      <%# something %>
      <%=raw something %>
      <%= something %>

  Hash style:
      <% foo = { :one => 'two' } %>
      <% foo = { one: 'two' } %>


  Pointer dots/arrows:
    Object* foo = bar.baz;
    Object* foo = bar->baz;


    functionCall (foo) ->
    functionCall (foo) =>

For more detailed information, please read the docs or check out the README on github.
install details
There are several ways to install the plugin. The recommended one is by using Tim Pope's pathogen (http://www.vim.org/scripts/script.php?script_id=2332). In that case, you can clone the plugin's git repository like so:

    git clone git://github.com/AndrewRadev/switch.vim.git ~/.vim/bundle/switch

If your vim configuration is under git version control, you could also set up the repository as a submodule, which would allow you to update more easily. The command is (provided you're in ~/.vim):

    git submodule add git://github.com/AndrewRadev/switch.vim.git bundle/switch

Another way is to simply copy all the essential directories inside the ~/.vim directory: plugin, autoload, doc.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
switch.zip 0.3.0 2015-01-27 7.0 Andrew Radev - Haml support
- Coffeescript dictionary shorthand
- Default mapping
- Various bugfixes
switch.zip 0.2.0 2014-01-26 7.0 Andrew Radev - Better customization
- More switches
switch.zip 0.1.1 2012-09-11 7.0 Andrew Radev Bugfix for multibyte characters
switch.zip 0.1.0 2012-09-09 7.0 Andrew Radev - Better customization options
- Builtin for C++ pointers
- Builtin for coffeescript arrows
- Builtin for ruby's tap method
- Builtin for ruby string types
- Bugfixes
switch.zip 0.0.1 2012-08-15 7.0 Andrew Radev Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.