Skip to content

Instantly share code, notes, and snippets.

@melrief
Created July 10, 2014 19:25
Show Gist options
  • Save melrief/300f1dd7ab7db0cfa237 to your computer and use it in GitHub Desktop.
Save melrief/300f1dd7ab7db0cfa237 to your computer and use it in GitHub Desktop.
Set the current selection indent to the indent of the line above it with <C-Tab> in vim
function! IndentAsPrevLine()
python << EOF
import itertools as I
from vim import *
if current.range.start > 0:
def takewhile(pred,l): return ''.join(I.takewhile(pred,l))
prev_line = current.buffer[current.range.start - 1]
spaces = takewhile(lambda c:c.isspace(),prev_line)
for idx in xrange(current.range.start,current.range.end+1):
line = current.buffer[idx]
curr_spaces = takewhile(lambda c:c.isspace(),line)
# avoid multiple indent
if len(spaces) != len(curr_spaces):
current.buffer[idx] = spaces + line.lstrip()
EOF
endfunction
map <C-Tab> :call IndentAsPrevLine()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment