7

I would like to do create a snippet in which the ${1:} command is present in multiple locations, allowing me to editing in multiple locations at once.

In particular I would like this snippet to work as "expected".

cout << "${1:} = " << ${1:} << "\n";

usage:

1) type cout

2) expand the snippet

3) type the name of the variable you want to print

Nisba
  • 905
  • 8
  • 19

1 Answers1

7

Try this snippet:

# -*- mode: snippet -*-
# name: cout variable
# key: cvar
# --
cout << "${1:var} = " << $1 << endl;$0

I believe the main problem is that yasnippet doesn't recognize the second ${1:} as a mirroring placeholder, and instead attempts to fix the snippet by changing ${1:} to ${2:}.

It may be an idea to open an issue on Github to at least issue an warning during snippet expansion for these kinds of errors, possibly when yas-verbosity is set to a sufficiently high value.

Xaldew
  • 1,231
  • 9
  • 21