Convert Figma logo to code with AI

kana logovim-textobj-user

Vim plugin: Create your own text objects

1,464
47
1,464
22

Top Related Projects

Vim plugin that provides additional text objects

Quick Overview

vim-textobj-user is a Vim plugin that provides a framework for creating custom text objects in Vim. It simplifies the process of defining new text objects, allowing users to extend Vim's functionality with their own custom selections based on specific patterns or rules.

Pros

  • Simplifies the creation of custom text objects in Vim
  • Provides a consistent API for defining text objects
  • Supports both visual mode and operator-pending mode
  • Allows for easy integration with other Vim plugins

Cons

  • Requires some knowledge of Vim scripting to create custom text objects
  • May have a slight learning curve for users new to text object concepts
  • Documentation could be more comprehensive for advanced use cases

Code Examples

  1. Defining a simple text object for "around comma":
call textobj#user#plugin('comma', {
\   'around': {
\     'pattern': ',\s*\zs.\{-}\ze\s*,',
\     'select': 'a,',
\   },
\ })
  1. Creating a text object for "inside function":
call textobj#user#plugin('function', {
\   'inside': {
\     'pattern': ['(', ')'],
\     'select-a': 'af',
\     'select-i': 'if',
\   },
\ })
  1. Defining a text object with a more complex pattern:
call textobj#user#plugin('date', {
\   'date': {
\     'pattern': '\<\d\{4}-\d\{2}-\d\{2}\>',
\     'select': ['ad', 'id'],
\   },
\ })

Getting Started

  1. Install the plugin using your preferred Vim plugin manager. For example, with vim-plug:
Plug 'kana/vim-textobj-user'
  1. Define your custom text object in your .vimrc file:
call textobj#user#plugin('example', {
\   'word': {
\     'pattern': '\w\+',
\     'select': ['aw', 'iw'],
\   },
\ })
  1. Restart Vim or source your .vimrc file.

  2. Use your new text object with commands like vaw (visual select around word) or diw (delete inside word).

Competitor Comparisons

Vim plugin that provides additional text objects

Pros of targets.vim

  • Provides a more extensive set of text objects out-of-the-box
  • Offers advanced features like seeking and remote targets
  • Easier to use for beginners without additional configuration

Cons of targets.vim

  • Less flexible for creating custom text objects
  • May have a steeper learning curve due to its many features
  • Potentially slower performance with its more complex functionality

Code Comparison

vim-textobj-user:

call textobj#user#plugin('example', {
\   'date': {
\     'pattern': '\<\d\d\d\d-\d\d-\d\d\>',
\     'select': ['ad', 'id'],
\   },
\ })

targets.vim:

let g:targets_seekRanges = 'cc cr cb cB lc ac Ac lr rr lb ar ab lB Ar aB Ab AB'
let g:targets_jumpRanges = 'bb bB BB aa Aa AA'

Summary

vim-textobj-user is a framework for creating custom text objects, offering greater flexibility but requiring more setup. targets.vim provides a rich set of predefined text objects and advanced features, making it more accessible for immediate use but potentially less customizable. The choice between them depends on whether you prefer a ready-to-use solution or the ability to create tailored text objects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

vim-textobj-user - Create your own text objects

Build Status

vim-textobj-user is a Vim plugin to create your own text objects without pain. It is hard to create text objects, because there are many pitfalls to deal with. This plugin hides such details and provides a declarative way to define text objects. You can use regular expressions to define simple text objects, or use functions to define complex ones.

Examples

Simple text objects defined by a pattern

Define ad/id to select a date such as 2013-03-16, and define at/it to select a time such as 22:04:21:

call textobj#user#plugin('datetime', {
\   'date': {
\     'pattern': '\<\d\d\d\d-\d\d-\d\d\>',
\     'select': ['ad', 'id'],
\   },
\   'time': {
\     'pattern': '\<\d\d:\d\d:\d\d\>',
\     'select': ['at', 'it'],
\   },
\ })

Simple text objects surrounded by a pair of patterns

Define aA to select text from << to the matching >>, and define iA to select text inside << and >>:

call textobj#user#plugin('braces', {
\   'angle': {
\     'pattern': ['<<', '>>'],
\     'select-a': 'aA',
\     'select-i': 'iA',
\   },
\ })

Complex text objects defined by functions

Define al to select the current line, and define il to select the current line without indentation:

call textobj#user#plugin('line', {
\   '-': {
\     'select-a-function': 'CurrentLineA',
\     'select-a': 'al',
\     'select-i-function': 'CurrentLineI',
\     'select-i': 'il',
\   },
\ })

function! CurrentLineA()
  normal! 0
  let head_pos = getpos('.')
  normal! $
  let tail_pos = getpos('.')
  return ['v', head_pos, tail_pos]
endfunction

function! CurrentLineI()
  normal! ^
  let head_pos = getpos('.')
  normal! g_
  let tail_pos = getpos('.')
  let non_blank_char_exists_p = getline('.')[head_pos[2] - 1] !~# '\s'
  return
  \ non_blank_char_exists_p
  \ ? ['v', head_pos, tail_pos]
  \ : 0
endfunction

Text objects for a specific filetype

Define a( to select text from \left( to the matching \right), and define i( to select text inside \left( to the matching \right), but only for tex files:

call textobj#user#plugin('tex', {
\   'paren-math': {
\     'pattern': ['\\left(', '\\right)'],
\     'select-a': [],
\     'select-i': [],
\   },
\ })

augroup tex_textobjs
  autocmd!
  autocmd FileType tex call textobj#user#map('tex', {
  \   'paren-math': {
  \     'select-a': '<buffer> a(',
  \     'select-i': '<buffer> i(',
  \   },
  \ })
augroup END

Further reading

You can define your own text objects like the above examples. See also the reference manual for more details.

There are many text objects written with vim-textobj-user. If you want to find useful ones, or to know how they are implemented, see a list of text objects implemented with vim-textobj-user.