Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active July 7, 2024 22:35
Show Gist options
  • Save romainl/ca742f241457b8609836202fe05ee5c0 to your computer and use it in GitHub Desktop.
Save romainl/ca742f241457b8609836202fe05ee5c0 to your computer and use it in GitHub Desktop.
Poor man's Surround

Poor man's Surround

The following shows how to replicate some of Surround's features if you don't happen to have it installed.

NOTE: We only dealt with the simplest cases, here, and the convoluted nature of some of these commands shows how well designed and useful that plugin is. And let's not talk about all the corner cases it handles.

Install it, it's worth it.

The list

Surround

  • ys<motion><thing>:

    " surround current word with parentheses
    " surround: ysiw)
    ciw(<C-r>")<Esc>
    
  • vS<thing>:

    " surround current visual selection with square brackets
    " surround: S]
    c[<C-r>"]<Esc>
    

Delete

  • ds', ds", and ds`:

    " delete surrounding single quotes
    " surround: ds'
    di'vhP
    
  • ds< with <:> added to :help matchpairs, ds(, ds{, and ds[:

    " delete surrounding parentheses
    " surround: ds)
    di(v%P
    
  • dst:

    " delete surrounding *ML tag
    " surround: dst
    ditvatP
    

Change

  • cs'<thing>, cs"<thing>, and cs`<thing>:

    " change surrounding single quotes to double quotes
    " surround: cs'"
    di'vh"_c"<C-r>""<Esc>
    
  • cs<<thing> with <:> added to :help matchpairs, cs(<thing>, cs{<thing>, and cs[<thing>:

    " change surrounding parentheses to braces
    " surround: cs)}
    di("_c%{<C-r>"}<Esc>
    
  • cst<new>:

    " change surrounding *ML tag to <new>…</new>
    " surround: cst<new<CR>
    dit"_cat<new><C-r>"</new><Esc>
    

My Vim-related gists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment