Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the case within Snipmate.vim snippets?

Tags:

vim

snipmate

Is it possible to change the case of variable values within Snipmate snippets?

For example:

snippet dc
  def create
    @${1} = $1.new
  end

Should output:

def create
  @product = Product.new
end

I tried to use backticks to call a custom function:

snippet dc
  def create
    @${1} = `ToUpperCase('$1')`.new
  end

And defined this function in Vim:

function! ToUpperCase(str)
    let result = substitute(a:str, '\(\w\)', '\u\1', '')
    return result
endfunction

This doesn't work as it seems that Snipmate expands its $n variables after it has executed the backticks.

like image 401
Jari Jokinen Avatar asked Feb 21 '11 09:02

Jari Jokinen


2 Answers

Disclaimer: I am the main author of UltiSnips.

For your interest and rollin-the-drum purposes, I present two snippet definitions for UltiSnips which has been mentioned here before. Both do what the OP wants. The first one uses transformations (TextMate syntax):

snippet dc "create" b
def create
   @$1 = ${1/.*/\u$0/}.new
end
endsnippet

The second uses python code interpolation. For my tastes this is easier to read but it is a little more verbose.

snippet dc "create" b
def create
   @$1 = `!p snip.rv = t[1].title()`.new
end
endsnippet

Since version 1.3 UltiSnips comes with a script that can convert snipMate snippets, switching should therefore be easy.

like image 131
SirVer Avatar answered Sep 30 '22 06:09

SirVer


The current release of snipMate is not capable of performing transformations on the mirrored text. Look up :help snipMate-disadvantages where it says:

Regex cannot be performed on variables, such as "${1/.*/\U&}"

If you really want this feature, you might want to try one of the other snippet plugins out there. UltiSnips uses the same syntax for defining snippets, and claims to have all of the same features as TextMate.

like image 44
nelstrom Avatar answered Sep 30 '22 07:09

nelstrom