Skip to content

Instantly share code, notes, and snippets.

@austintaylor
Created May 18, 2010 20:46
Show Gist options
  • Save austintaylor/405513 to your computer and use it in GitHub Desktop.
Save austintaylor/405513 to your computer and use it in GitHub Desktop.
Text object for indented code
" 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
@austintaylor
Copy link
Author

Originally from http://vim.wikia.com/wiki/Indent_text_object. Modified to behave nicely with sass and haml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment