Ultisnips: [Question] Dynamically change length of string as you type

Created on 9 Aug 2017  路  3Comments  路  Source: SirVer/ultisnips

Hi I was wondering if there was a way to dynamically change the size of a string as you type. In particular, I'm trying to change the number of dashes based on whatever value one has for &textwidth. Do you guys have any suggestions?

Here is a manual version of changing the number of dashes. With this method, you first type the border name then change the number of dashes.

snippet bordp "Add a border pair" b
" $1 {{{ ${2:------------------------------------------------------------------}
${0:${VISUAL:" Content }}
" $1 }}} $2
endsnippet

Here is what it should look like with a 7-letter word and 80-character textwidth:

" testing {{{ ------------------------------------------------------------------
" Content
" testing }}} ------------------------------------------------------------------

Here is what it would look like with a 3-letter word. Note that there is more dashes in this version, and it still has the same textwidth of 80 characters:

" foo {{{ ----------------------------------------------------------------------
" Content
" foo }}} ----------------------------------------------------------------------
user question

All 3 comments

@PratikBhusal: Try that one:

snippet bordp "Add a border pair" b
" $1 {{{ ${2:`!p snip.rv = '-' * (int(snip.opt('&tw')) - len(t[1]))`}
${0:${VISUAL:" Content }}
" $1 }}} $2
endsnippet

@seletskiy I slightly modified your version because it did not take into consideration the extra characters (ie " and {{{). Other than that, it looks good to me. Before closing, could you explain what t[1] represents?

snippet bordp "Add a border pair" b
" $1 {{{ ${2:`!p snip.rv = '-' * (int(snip.opt('&tw')) - len(t[1]) - 7)`}
${0:${VISUAL:" Content }}
" $1 }}} $2
endsnippet

@PratikBhusal I think that t is an array whose items are the texts you wrote in place of your placeholders. So, here, t[1] is the item of index 1 from the array t, which contains the text of ${1}.

If you want more information, you could have a look at :h ultisnips-python. There's also a Vimcast episode which talks about python interpolation in Ultisnips snippets.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Industrial picture Industrial  路  8Comments

Alex-Canales picture Alex-Canales  路  4Comments

MikeDacre picture MikeDacre  路  3Comments

codybuell picture codybuell  路  4Comments

robenkleene picture robenkleene  路  8Comments