├── LICENSE ├── README.md └── plugin └── textobj └── css.vim /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-textobj-css 2 | 3 | [Text objects](http://blog.carbonfive.com/2011/10/17/vim-text-objects-the-definitive-guide/) in Vim are very useful. They allow you to modify text by word, sentence, paragraph, HTML tag, and more. [vim-textobj-user](https://github.com/kana/vim-textobj-user) is a Vim plugin that lets you define your own text objects and this project builds on that one to let you work with CSS blocks more easily. 4 | 5 | ![demo](https://cloud.githubusercontent.com/assets/6104/13205723/558fa396-d8bc-11e5-8587-f1763a98f62b.gif) 6 | 7 | ### Usage 8 | 9 | * `vic` Visually select inner CSS (inner rules) 10 | * `vac` Viscually select "all" CSS (entire block w/ selector(s)) 11 | * `cic` Change inner CSS 12 | * `cac` Change "all" CSS 13 | * `dic` Delete inner CSS 14 | * `dac` Delete "all" CSS 15 | 16 | It's often handy to visually select a nested CSS block with `vac` and then type `ac` again to expand the selection to the parent block. This is shown in the above GIF. 17 | 18 | #### :warning: Warning if you use vim-gitgutter :warning: 19 | 20 | vim-gitgutter supplies some text objects for dealing with hunks and these conflict with vim-textobj-css. As the [vim-gitgutter README describes](https://github.com/airblade/vim-gitgutter#hunks), you can override those settings with something like: 21 | 22 | ``` 23 | omap ih GitGutterTextObjectInnerPending 24 | omap ah GitGutterTextObjectOuterPending 25 | xmap ih GitGutterTextObjectInnerVisual 26 | xmap ah GitGutterTextObjectOuterVisual 27 | ``` 28 | 29 | ## Installation 30 | 31 | This plugin requires [vim-textobj-user](https://github.com/kana/vim-textobj-user). Use your favorite plugin manager to add these lines. 32 | 33 | ### vim-plug 34 | ``` 35 | Plug 'kana/vim-textobj-user' 36 | Plug 'jasonlong/vim-textobj-css' 37 | ``` 38 | 39 | ### NeoBundle 40 | ``` 41 | NeoBundle 'kana/vim-textobj-user' 42 | NeoBundle 'jasonlong/vim-textobj-css' 43 | ``` 44 | 45 | ### Vundle 46 | ``` 47 | Plugin 'kana/vim-textobj-user' 48 | Plugin 'jasonlong/vim-textobj-css' 49 | ``` 50 | -------------------------------------------------------------------------------- /plugin/textobj/css.vim: -------------------------------------------------------------------------------- 1 | if exists('g:loaded_textobj_css') 2 | finish 3 | endif 4 | 5 | call textobj#user#plugin('css', { 6 | \ '-': { 7 | \ '*sfile*': expand(':p'), 8 | \ 'select-a': 'ac', '*select-a-function*': 's:select_a', 9 | \ 'select-i': 'ic', '*select-i-function*': 's:select_i' 10 | \ } 11 | \ }) 12 | 13 | let s:start_pattern = '{' 14 | let s:end_pattern = '}' 15 | 16 | function! s:select_a() 17 | let s:flags = 'Wb' " Search backward to previous { 18 | 19 | call searchpair(s:start_pattern,'',s:end_pattern, s:flags) 20 | 21 | " Jump to match 22 | normal % 23 | let end_pos = getpos('.') 24 | 25 | " Jump back to top and look for multiple lines of selectors 26 | normal %0 27 | if line('.') > 1 " Only look further up if we're not on the first line 28 | normal {j 29 | endif 30 | let start_pos = getpos('.') 31 | 32 | return ['V', start_pos, end_pos] 33 | endfunction 34 | 35 | function! s:select_i() 36 | let s:flags = 'Wb' " Search backward to previous { 37 | 38 | call searchpair(s:start_pattern,'',s:end_pattern, s:flags) 39 | 40 | " Move down one line, and save position 41 | normal j^ 42 | let start_pos = getpos('.') 43 | 44 | " Move up again, jump to match, then up one line and save position 45 | normal k%k$ 46 | let end_pos = getpos('.') 47 | 48 | return ['V', start_pos, end_pos] 49 | endfunction 50 | 51 | let g:loaded_textobj_css = 1 52 | --------------------------------------------------------------------------------