Skip to content

Commit

Permalink
Version 1.0.1: Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhedberg authored and vim-scripts committed Sep 9, 2011
0 parents commit f0a1d4e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README
@@ -0,0 +1,20 @@
This is a mirror of http://www.vim.org/scripts/script.php?script_id=3724

indent-motion is a Vim plugin which maps `<Leader>[` and `<Leader>]` in normal, visual, and operator-pending modes to move to the beginning and end (respectively) of your current indentation-delimited block (`<Leader>` refers to your current user-defined "mapleader", which is `\` by default).

For example (using JavaScript):

function foo(a, b, c) {
var x = 1;
var y = 2;
if (x == y) {
alert("This will never happen!");
++y;
}
}

In the above snippet, if the cursor is positioned on the `var x = 1;` line, then `<Leader>]` will move the cursor to the next-to-last line (closing brace of the conditional block) and `<Leader>[` will move it back to the original location. If the cursor is positioned on the `alert` call, then `<Leader>]` will move down 1 line to `++y;`.

If one of the mappings is executed on an empty line (containing no characters, not even whitespace), then the assumed indentation will be that of the next non-empty line, where "next" is in the direction of the requested motion.

Currently, the mappings are not configurable, but this can easily be changed upon request.
47 changes: 47 additions & 0 deletions plugin/indent-motion.vim
@@ -0,0 +1,47 @@
function! s:next_line(l, up)
let nl = a:l + (a:up ? -1 : 1)
if nl < 1 || nl > line('$')
throw 'buf_bound'
endif
return nl
endfunction

function! <SID>to_indent_end(up, vis)

let ln = line('.')
if indent(ln) == 0 && getline(ln) =~ '^$'
while getline(ln) =~ '^$'
try
let ln = s:next_line(ln, a:up)
catch 'buf_bound'
break
endtry
endwhile
endif
let orig_ind = indent(ln)
try
let ln = s:next_line(line('.'), a:up)
catch 'buf_bound'
return
endtry
if a:vis
normal! gv
endif
while indent(ln) >= orig_ind || getline(ln) =~ '^$'
execute 'normal! ' . (a:up ? 'k' : 'j')
try
let ln = s:next_line(ln, a:up)
catch 'buf_bound'
return
endtry
endwhile

endfunction

nnoremap <silent> <Leader>] :call <SID>to_indent_end(0, 0)<CR>
nnoremap <silent> <Leader>[ :call <SID>to_indent_end(1, 0)<CR>
onoremap <silent> <Leader>] :call <SID>to_indent_end(0, 0)<CR>
onoremap <silent> <Leader>[ :call <SID>to_indent_end(1, 0)<CR>
vnoremap <silent> <Leader>] :call <SID>to_indent_end(0, 1)<CR>
vnoremap <silent> <Leader>[ :call <SID>to_indent_end(1, 1)<CR>

0 comments on commit f0a1d4e

Please sign in to comment.