Need help with vim-vsnip?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

hrsh7th
271 Stars 17 Forks MIT License 295 Commits 14 Opened issues

Description

Snippet plugin for vim/nvim that supports LSP/VSCode's snippet format.

Services available

!
?

Need anything else?

Contributors list

vim-vsnip

VSCode(LSP)'s snippet feature in vim.

Features

Concept

  • Pure Vim script
  • Well tested
  • Support VSCode snippet format
  • Provide integration with many plugins

Related repository

friendly-snippets - Set of preconfigured snippets for all kind of programming languages that integrates really well with vim-vsnip, so all users can benefit from them and not to worry about setting up snippets on their own. It already includes:

  • HTML, Pug, Jade
  • CSS, Sass, Less, Stylus
  • Javascript, Typescript, Javascriptreact, Typescriptreact
  • PHP
  • Python
  • C
  • C++
  • Rust
  • Go
  • Eruby
  • Ruby
  • Swift

Usage

1. Install

You can use your favorite plugin managers to install this plugin.

Plug 'hrsh7th/vim-vsnip'
Plug 'hrsh7th/vim-vsnip-integ'

call dein#add('hrsh7th/vim-vsnip') call dein#add('hrsh7th/vim-vsnip-integ')

NeoBundle 'hrsh7th/vim-vsnip' NeoBundle 'hrsh7th/vim-vsnip-integ'

2. Setting

" NOTE: You can use other key to expand snippet.

" Expand imap vsnip#expandable() ? '(vsnip-expand)' : '' smap vsnip#expandable() ? '(vsnip-expand)' : ''

" Expand or jump imap vsnip#available(1) ? '(vsnip-expand-or-jump)' : '' smap vsnip#available(1) ? '(vsnip-expand-or-jump)' : ''

" Jump forward or backward imap vsnip#jumpable(1) ? '(vsnip-jump-next)' : '' smap vsnip#jumpable(1) ? '(vsnip-jump-next)' : '' imap vsnip#jumpable(-1) ? '(vsnip-jump-prev)' : '' smap vsnip#jumpable(-1) ? '(vsnip-jump-prev)' : ''

" Select or cut text to use as $TM_SELECTED_TEXT in the next snippet. " See https://github.com/hrsh7th/vim-vsnip/pull/50 nmap s (vsnip-select-text) xmap s (vsnip-select-text) nmap S (vsnip-cut-text) xmap S (vsnip-cut-text)

" If you want to use snippet for multiple filetypes, you can g:vsnip_filetypes for it. let g:vsnip_filetypes = {} let g:vsnip_filetypes.javascriptreact = ['javascript'] let g:vsnip_filetypes.typescriptreact = ['typescript']

3. Create your own snippet

Snippet file will store to

g:vsnip_snippet_dir
per filetype.
  1. Open some file (example:
    Sample.js
    )
  2. Invoke
    :VsnipOpen
    command.
  3. Edit snippet.
{
  "Class": {
    "prefix": ["class"],
    "body": [
      "/**",
      " * @author ${VIM:\\$USER}",
      " */",
      "class $1 ${2:extends ${3:Parent} }{",
      "\tconstructor() {",
      "\t\t$0",
      "\t}",
      "}"
    ],
    "description": "Class definition template."
  }
}

The snippet format was described in here or here.

Recipe

$TMFILENAMEBASE

You can insert the filename via

fname\(vsnip-expand)
.
{
  "filename": {
    "prefix": ["fname"],
    "body": "$TM_FILENAME_BASE"
  }
}

Log $TMSELECTEDTEXT

You can fill

$TM_SELECTED_TEXT
by
(vsnip-select-text)
or
(vsnip-cut-text)
.
{
  "log": {
    "prefix": ["log"],
    "body": "console.log(${1:$TM_SELECTED_TEXT});"
  }
}

Insert environment vars

You can insert value by Vim script expression.

{
  "user": {
    "prefix": "username",
    "body": "${VIM:\\$USER}"
  }
}

Insert UUID via python

You can insert UUID via python.

{
  "uuid": {
    "prefix": "uuid",
    "body": [
      "${VIM:system('python -c \"import uuid, sys;sys.stdout.write(str(uuid.uuid4()))\"')}"
    ]
  }
}

NOTE:

$VIM
is only in vsnip. So that makes to lost the snippet portability.

DEMO

LSP integration

Nested snippet expansion

 with 
$TM_SELECTED_TEXT

<Plug&rt;(vsnip-cut-text) with $TM_SELECTED_TEXT

Development

How to run test it?

You can run

npm run test
after install vim-themis.

How sync same tabstop placeholders?

  1. compute the
    user-diff
    ...
    s:Session.flush_changes
  2. reflect the
    user-diff
    to snippet ast ...
    s:Snippet.follow
  3. reflect the
    sync-diff
    to buffer content ...
    s:Snippet.sync & s:Session.flush_changes

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.