Created
May 18, 2010 20:46
-
-
Save austintaylor/405513 to your computer and use it in GitHub Desktop.
Text object for indented code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Text object for indented code | |
onoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR> | |
onoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR> | |
vnoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR><Esc>gv | |
vnoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR><Esc>gv | |
function! IndTxtObj(inner) | |
if &filetype == 'haml' || &filetype == 'sass' || &filetype == 'python' | |
let meaningful_indentation = 1 | |
else | |
let meaningful_indentation = 0 | |
endif | |
let curline = line(".") | |
let lastline = line("$") | |
let i = indent(line(".")) - &shiftwidth * (v:count1 - 1) | |
let i = i < 0 ? 0 : i | |
if getline(".") =~ "^\\s*$" | |
return | |
endif | |
let p = line(".") - 1 | |
let nextblank = getline(p) =~ "^\\s*$" | |
while p > 0 && (nextblank || indent(p) >= i ) | |
- | |
let p = line(".") - 1 | |
let nextblank = getline(p) =~ "^\\s*$" | |
endwhile | |
if (!a:inner) | |
- | |
endif | |
normal! 0V | |
call cursor(curline, 0) | |
let p = line(".") + 1 | |
let nextblank = getline(p) =~ "^\\s*$" | |
while p <= lastline && (nextblank || indent(p) >= i ) | |
+ | |
let p = line(".") + 1 | |
let nextblank = getline(p) =~ "^\\s*$" | |
endwhile | |
if (!a:inner && !meaningful_indentation) | |
+ | |
endif | |
normal! $ | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Originally from http://vim.wikia.com/wiki/Indent_text_object. Modified to behave nicely with sass and haml.