Skip to content

Instantly share code, notes, and snippets.

@kosei27
Created August 1, 2013 04:09
Show Gist options
  • Save kosei27/6128349 to your computer and use it in GitHub Desktop.
Save kosei27/6128349 to your computer and use it in GitHub Desktop.
" Select CSS Rule Set
" カーソルから一番近いCSSルールセットを選択する
"
" vimrcに↓を追加すると var で動きます。
" nnoremap <silent> var :<C-u>call SelectCssRuleSet()<CR>
function! SelectCssRuleSet()
let save_reg = @@
silent normal $va{Voy
let first_yank = @@
" 最初にヤンクした内容に'{'が含まれるかどうかチェック
let chk_first_yank = matchstr(first_yank, '{')
if chk_first_yank == ''
" 最初にヤンクした内容に'{'が含まれない場合は次の行をチェックする
let chk_next_line = matchstr(getline(line(".")+1), '{')
while chk_next_line == '' " 次の行に'{'がない場合は見つかるまで繰り返す
silent normal j
" ファイルの最終行に来たらループ抜けてエラーメッセージを表示する
if line(".")==line("$")
execute "normal \<Esc>"
echohl ErrorMsg
echo 'no match css rule sets.'
echohl None
return
break
endif
let chk_next_line = matchstr(getline(line(".")+1), '{')
endwhile
silent normal j
endif
execute "normal \<Esc>"
silent normal $va{Vo
let chk_prev_line = matchstr(getline(line(".")-1), ',')
while chk_prev_line != '' " 上の行に','があればセレクタグループとして選択する
silent normal k
let chk_prev_line = matchstr(getline(line(".")-1), ',')
endwhile
let @@ = save_reg
endfunction
" __END__ "{{{1
" vim: expandtab softtabstop=4 shiftwidth=4
" vim: foldmethod=marker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment