1

This is the current snippet that i am using

snippet req
  const ${1:} = require('$1');

As you might have guessed it works well with packages that i installed from npm but not so well for local file. Suppose i have a file at ../user.js that i want to include in my nodejs project rather than showing up as

const ../user.js = require('../user.js');

I would like it to be as

const user = require('../user.js');

from what i assume i would somehow have to split the file path using / get last element and again get first element after exploding with . which would work well with both global install and local file inclusion. Problem is that i am new to snipmate and have not figured out how can this be achieved. Any nudge in right direction is greatly appreciated. Thanks in advance for helping me out.

Update

Refering to snipmate documentation here, i found that i can use system to utilize standard unix commands to perform operation on string. Now i am still stuck up on how to exactly pass $1 inside the system. Here is the updated code that i have

snippet req
  const `system('echo $1')` = require('${1:}'); 
georoot
  • 3,557
  • 1
  • 30
  • 59
  • How well would https://github.com/honza/vim-snippets/blob/master/snippets/javascript/javascript.node.snippets#L7 work? – Jeremy Pridemore Jan 24 '17 at 15:19
  • @JeremyPridemore let me try that and get back to you :) – georoot Jan 24 '17 at 15:20
  • @JeremyPridemore nope thats just more typing for me... I could achieve the same using `const ${1} = require('${2}');` exploding the string might make my requrie process faster... Plus some nice learning experience on how to process variables in snipmate :) – georoot Jan 24 '17 at 15:24
  • Ah, ok. I use vim and node, but had never heard of vim-snippet before. If that doesn't do it, I'm not entirely sure. Good luck! – Jeremy Pridemore Jan 24 '17 at 15:26
  • @JeremyPridemore have a look now if you can help somehow :) – georoot Jan 24 '17 at 15:50

2 Answers2

3

The original snipMate cannot manipulate mirrored tabstops; I think UltiSnips allows this, though. (The `...` interpolation is only executed only during snippet expansion; you cannot pass $1 into it.)

If you don't want to switch, you can split the definition of the included file into a path and module name, and only mirror the latter into the variable name:

snippet req
    const $2 = require('${1:path/}${2:module}');
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
1

I don't have your snippet setup, but now that you can send it to bash, I'd assume that you can pipe it through grep to pull out the part of the value that you want. If you put in something like ../models/user.js it should just pull the user.js out.

snippet req
  const `system('echo $1 | grep -Po "(?<=/)[^/]*$"')` = require('${1:}');
Jeremy Pridemore
  • 1,995
  • 1
  • 14
  • 24
  • Still stuck on sending to `system` the docs are not that clear on snipmate :( still figuring out how to set variable and pass to system. – georoot Jan 24 '17 at 16:11