• Stars
    star
    1,386
  • Rank 32,810 (Top 0.7 %)
  • Language
    Vim Script
  • Created about 9 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Set of operators and textobjects to search/select/edit sandwiched texts.

vim-sandwich

Build Status Build status

sandwich.vim is a set of operator and textobject plugins to add/delete/replace surroundings of a sandwiched textobject, like (foo), "bar".

Quick start

Add

Press sa{motion/textobject}{addition}. For example, a key sequence saiw( makes foo to (foo).

Delete

Press sdb or sd{deletion}. For example, key sequences sdb or sd( makes (foo) to foo. sdb searches a set of surrounding automatically.

Replace

Press srb{addition} or sr{deletion}{addition}. For example, key sequences srb" or sr(" makes (foo) to "foo".

That's all. Now you already know enough about sandwich.vim. If you are using vim-surround, you can use a preset keymappings similar as it. See here

sandwich.vim has some functional input for {addition}/{deletion}. Check here!

Design

This plugin provides functions to add/delete/replace surroundings of a sandwiched text. These functions are implemented genuinely by utilizing operator/textobject framework. Thus their action can be repeated by . command without any dependency. It consists of two parts, operator-sandwich and textobj-sandwich.

operator-sandwich

A sandwiched text could be resolved into two parts, {surrounding} and {surrounded text}.

  • Add surroundings: mapped to the key sequence sa

    • {surrounded text} ---> {surrounding}{surrounded text}{surrounding}
  • Delete surroundings: mapped to the key sequence sd

    • {surrounding}{surrounded text}{surrounding} ---> {surrounded text}
  • Replace surroundings: mapped to the key sequence sr

    • {surrounding}{surrounded text}{surrounding} ---> {new surrounding}{surrounded text}{new surrounding}

textobj-sandwich

  • Search and select a sandwiched text automatically: mapped to the key sequence ib and ab
  • Search and select a sandwiched text with query: mapped to the key sequence is and as

ib and is selects {surrounded text}. ab and as selects {surrounded text} including {surrounding}s.

             |<----ib,is---->|
{surrounding}{surrounded text}{surrounding}
|<-----------------ab,as----------------->|

Configuration

The point is that it would be nice to be shared the definitions of {surrounding}s pairs in all kinds of operations. User can freely add new settings to extend the functionality. If g:sandwich#recipes was defined, this plugin works with the settings inside. As a first step, it would be better to copy the default settings in g:sandwich#default_recipes.

let g:sandwich#recipes = deepcopy(g:sandwich#default_recipes)

Each setting, it is called recipe, is a set of a definition of {surrounding}s pair and options. The key named buns is used for the definition of {surrounding}.

let g:sandwich#recipes += [{'buns': [{surrounding}, {surrounding}], 'option-name1': {value1}, 'option-name2': {value2} ...}]

For example: {'buns': ['(', ')']}
    foo   --->   (foo)

Or there is a different way, use external textobjects to define {surrounding}s from the difference of two textobjects.

let g:sandwich#recipes += [{'external': [{textobj-i}, {textobj-a}], 'option-name1': {value1}, 'option-name2': {value} ...}]

For example: {'external': ['it', 'at']}
    <title>foo</title>   --->   foo

Features

Unique count handling

As for the default operators, the possible key input in normal mode is like this.

        [count1]{operator}[count2]{textobject}

Default operators do not distinguish [count1] and [count2] but operator-sandwich does. [count1] is given for {operators} and [count2] is given for {textobject}.

Linewise and blockwise operations

Operator-sandwich works linewise with the linewise-visual selection and linewise motions.

" press Vsa(
    foo  --->  (
               foo
               )

Using command option, user can execute vim Ex-commands after an action. For example it can be used to adjust indent automatically.

let g:sandwich#recipes += [
      \   {
      \     'buns'        : ['{', '}'],
      \     'motionwise'  : ['line'],
      \     'kind'        : ['add'],
      \     'linewise'    : 1,
      \     'command'     : ["'[+1,']-1normal! >>"],
      \   },
      \   {
      \     'buns'        : ['{', '}'],
      \     'motionwise'  : ['line'],
      \     'kind'        : ['delete'],
      \     'linewise'    : 1,
      \     'command'     : ["'[,']normal! <<"],
      \   }
      \ ]

" press Vsa{
    foo    --->  {
                   foo
                 }

" press V2jsd
    {      --->  foo
      foo
    }

Operator-sandwich also can work blockwise with the blockwise-visual selection and blockwise motions.

" press <C-v>2j2lsa(
    foo        (foo)
    bar  --->  (bar)
    baz        (baz)

There is an option to skip white space skip_space, it is valid in default. Empty line is ignored.

" press <C-v>3j$sa(
    fooooooo            (fooooooo)
      baaaar   --->       (baaaar)

    baaaz               (baaaz)

Expression surroundings and regular expression matching

The option expr enables to evaluate surroundings (buns) before adding/deleting/replacing surroundings. The following recipe is an simple example to wrap texts by html style tags.

let g:sandwich#recipes += [
      \   {
      \     'buns'    : ['TagInput(1)', 'TagInput(0)'],
      \     'expr'    : 1,
      \     'filetype': ['html'],
      \     'kind'    : ['add', 'replace'],
      \     'action'  : ['add'],
      \     'input'   : ['t'],
      \   },
      \ ]

function! TagInput(is_head) abort
  if a:is_head
    let s:TagLast = input('Tag: ')
    if s:TagLast !=# ''
      let tag = printf('<%s>', s:TagLast)
    else
      throw 'OperatorSandwichCancel'
    endif
  else
    let tag = printf('</%s>', matchstr(s:TagLast, '^\a[^[:blank:]>/]*'))
  endif
  return tag
endfunction

The option regex is to regard surroundings (buns) as regular expressions to match and delete/replace. The following recipe is an simple example to delete both ends of html tag.

let g:sandwich#recipes += [
      \   {
      \     'buns'    : ['<\a[^[:blank:]>/]*.\{-}>',
      \                  '</\a[^[:blank:]>/]*>'],
      \     'regex'   : 1,
      \     'filetype': ['html'],
      \     'nesting' : 1,
      \     'input'   : ['t'],
      \   },
      \ ]

However the above example is not so accurate. Instead of the example, there are excellent built-in textobjects it and at, these external textobjects also can be utilized through external.

let g:sandwich#recipes += [
      \   {
      \     'external': ['it', 'at'],
      \     'noremap' : 1,
      \     'filetype': ['html'],
      \     'input'   : ['t'],
      \   },
      \ ]

Demo

sandwich.vim

Pioneers

More Repositories

1

vim-highlightedyank

Make the yanked region apparent!
Vim Script
831
star
2

vim-swap

Reorder delimited items.
Vim Script
287
star
3

vim-columnmove

Move cursor along a column.
Vim Script
53
star
4

vim-textobj-delimited

A textobject plugin to do well with each delimited part of a string.
Vim Script
50
star
5

vim-Verdin

An omni completion function for Vim script
Vim Script
36
star
6

vim-vimhelplint

A lint tool for vim help files.
Vim Script
35
star
7

vim-colorscheme-imas

Vimmerさん!colorschemeですよ、colorscheme!
Vim Script
29
star
8

vim-multiselect

A library plugin to handle multiple visual selections
Vim Script
28
star
9

vim-highlightedundo

Make the undo region apparent!
Vim Script
26
star
10

vim-colorscheme-kemonofriends

ようこそジャパリパークへ!
Vim Script
25
star
11

vim-textobj-functioncall

The vim textobject plugin to treat function-call regions.
Vim Script
22
star
12

asyncomplete-ezfilter.vim

Helper functions to build a custom preprocessor for asyncomplete.vim
Vim Script
18
star
13

vim-colorscheme-tatami

A green colorscheme for Vim text editor.
Vim Script
16
star
14

DoubleExponentialFormulas.jl

One-dimensional numerical integration using the double exponential formula
Julia
16
star
15

vim-masquerade

Tools to edit multiple selections
Vim Script
14
star
16

vim-colorscheme-snowtrek

A light colorscheme for vim text editor
Vim Script
13
star
17

vim-patternjump

Move cursor as you like.
Vim Script
13
star
18

vim-fim

"f" imitated; not improved.
Vim Script
8
star
19

asyncomplete-unicodesymbol.vim

A completion source for asyncomplete.vim to convert a LaTeX-like sequence to a unicode symbol
Vim Script
5
star
20

vim-lsp-julia

The Julia programming language support for vim-lsp using LanguageServer.jl
Vim Script
5
star
21

vim-operator-jerk

Indent inside lines.
Vim Script
4
star
22

vital-Schedule

Handling tasks
Vim Script
4
star
23

vim-colorscheme-reki

A desolate colorscheme for vim.
Vim Script
4
star
24

vim-textobj-char

The vim textobject plugin to select a character.
Vim Script
4
star
25

vimrc

my vimrc
Vim Script
3
star
26

vim-textobj-equation

The vim textobject plugin to selet a equation like text block.
Vim Script
3
star
27

vim-multitarget-gn

Yet another gn command taking a count as a number of operation
Vim Script
2
star
28

vim-scilabcomplete

The omni completion function of scilab for vim.
Vim Script
1
star
29

vim-event-DotCommandPre

Add an autocmd event 'DotCommandPre'
Vim Script
1
star
30

look

I just want to use look command in windows os.
C
1
star
31

vim-colorscheme-franchouchou

よかったい、よかったい
Vim Script
1
star