Skip to content
wallace11 edited this page May 30, 2019 · 10 revisions

Magic characters

Additions (sa) or Deletions (sd) usually require ( for a () pair, or " for a "" pair.

Sandwich.vim includes several functional inputs (the ( and " in the last example) to assist cumbersome editing:

Input Name Examples
f / F Function surroundings arg -> func(arg)
i / I Instant surroundings text -> [before]text[after]
t / T HTML style tags text -> <p>text</p>

It might be helpful for your work, give it a try!

Function surroundings

  • f
  • F

Press saiwf to surround a word by function. After inputting f key, user would be requested to input function name and press <CR>, then the target textobject would be surrounded by parenthesis with the function name. <Tab> key completes function names from the current buffer in input.

arg      -- saiwffunc<CR> -->     func(arg)

The key sequence sdf, conversely, deletes function surrounding.

func(arg)       -- sdf -->             arg

In case of nested functions, sdf deletes the function under the cursor while sdF deletes the function surrounding.

cursor is on 'func2':
func1(func2(arg))  -- sdf -->   func1(arg)
                   -- sdF -->   func2(arg)

Function surroundings demo

Instead of the default one, the following recipe is also useful to surround by a function. It starts insert mode after surrounding by parentheses, thus it can use the power of insert mode completion, while there is a drawback it doesn't work well with . command repeating.

let g:sandwich#recipes = deepcopy(g:sandwich#default_recipes)
let g:sandwich#recipes += [
  \   {
  \     'buns': ['(', ')'],
  \     'cursor': 'head',
  \     'command': ['startinsert'],
  \     'kind': ['add', 'replace'],
  \     'action': ['add'],
  \     'input': ['f']
  \   },
  \ ]

Instant surroundings

  • i
  • I

It realizes to define *"i"*nstant surroundings. saiwi ask user for inputting former and latter surroundings. For example, saiwifoo<CR>bar<CR> makes a word surrounded by foo and bar. <Tab> key completes words from the current buffer, just simply. On the other hand sdi deletes arbitrary surroundings. For example, sdifoo<CR>bar<CR> makes foowordbar to word, the inputs would be interpreted as regular expressions. This is useful when a lot of targets are there because the action could be repeated by dot command. saiwI and sdI re-call the last used input again.

HTML style tags

  • t
  • T

The inputs t and T support to edit HTML style tags. saiwt ask user to input a name of element, then a textobject would be surrounded by the tag. saiwT works as same.

word      -- saiwtp<CR> -->   <p>word</p>

sdt deletes the nearest tag surroundings. sdT works as same.

<p>word</p>  -- saiwtp<CR> -->      word

t and T works differently only when replacing surroundings. srtt replaces only the name of element, does not touch attributes. srTT replaces the whole body of tags.

Emmet-like abbreviation

If the input is matched to Emmet abbreviation syntaxes, it could be expanded. The following attribute operator syntaxes are now supported.

  • id : div#id1 to <div id="id1"></div>
  • class : div.class1 to <div class="class1"></div>
  • custom attributes : div[attr=value] to <div attr="value"></div>

Emmet-like abbreviation demo

Clone this wiki locally