Move plugin.
authorIain Patterson <me@iain.cx>
Wed, 2 Oct 2013 16:35:25 +0000 (17:35 +0100)
committerIain Patterson <me@iain.cx>
Mon, 14 Oct 2013 11:44:20 +0000 (12:44 +0100)
Move lines or blocks with alt+d/j/k/u.

.vim/doc/move.txt [new file with mode: 0644]
.vim/plugin/move.vim [new file with mode: 0644]
.vimrc

diff --git a/.vim/doc/move.txt b/.vim/doc/move.txt
new file mode 100644 (file)
index 0000000..48a557b
--- /dev/null
@@ -0,0 +1,115 @@
+*move.txt* Moving lines and selections up and even down
+
+Author: Matthias Vogelgesang <github.com/matze>
+License: MIT (see |move-license|)
+
+===============================================================================
+Contents                                                        *move-contents*
+
+    1. Usage..................................|move-usage|
+    2. Mappings...............................|move-mappings|
+    3. License................................|move-license|
+    4. Changelog..............................|move-changelog|
+
+===============================================================================
+1. Usage                                                           *move-usage*
+
+The move plugin is used to move lines and visual selections up and down by
+wrapping the :move command.
+
+===============================================================================
+2. Mappings                                                     *move-mappings*
+
+To enable custom key maps you must disable the automatic key maps with >
+
+    let g:move_map_keys = 0
+
+The plugin provide finger-friendly mappings to move text around by using <jk>
+keys. Your can specify the key modifier that uses in key bindings with >
+
+    let g:move_key_modifier = 'M'
+
+All mappings can be prefixed with a {count} and will move {count} steps
+instead of one.
+
+-------------------------------------------------------------------------------
+2.1 <Plug>MoveBlockDown
+
+Move selected block down by one line.
+
+Default: vmap <A-j> <Plug>MoveBlockDown
+
+-------------------------------------------------------------------------------
+2.2 <Plug>MoveBlockUp
+
+Move selected block up by one line.
+
+Default: vmap <A-k> <Plug>MoveBlockUp
+
+-------------------------------------------------------------------------------
+2.3 <Plug>MoveLineDown
+
+Move current line down by one.
+
+Default: nmap <A-j> <Plug>MoveLineDown
+
+-------------------------------------------------------------------------------
+2.4 <Plug>MoveLineUp
+
+Move current line up by one.
+
+Default: nmap <A-k> <Plug>MoveLineUp
+
+-------------------------------------------------------------------------------
+2.5 <Plug>MoveBlockHalfPageDown
+
+Move selected block down by half a page size.
+
+Default: not mapped
+
+-------------------------------------------------------------------------------
+2.6 <Plug>MoveBlockHalfPageUp
+
+Move selected block up by half a page size.
+
+Default: not mapped
+
+-------------------------------------------------------------------------------
+2.7 <Plug>MoveLineHalfPageDown
+
+Move current line down by half a page size.
+
+Default: not mapped
+
+-------------------------------------------------------------------------------
+2.7 <Plug>MoveLineHalfPageUp
+
+Move current line up by half a page size.
+
+Default: not mapped
+
+===============================================================================
+3. License                                                       *move-license*
+
+This plugin is copyright by Matthias Vogelgesang and licensed under the MIT
+license.
+
+===============================================================================
+3. Changelog                                                   *move-changelog*
+
+v1.3-dev
+    * Silence :move commands to avoid first-time errors on read-only files.
+
+v1.2
+    * Released on 08/14/13
+    * Add key modifier for bindings (@vitalk)
+    * Use <A-j> and <A-k> in normal and visual mode (@vitalk)
+    * Add <Plug>Move{Block,Line}HalfPage{Up,Down} to move text in larger
+      increments.
+v1.1
+    * Released on 08/10/13
+    * Minor bug fixes (helptags, mapping, docs)
+v1.0
+    * Released on 08/10/13
+
+vim:ft=help:
diff --git a/.vim/plugin/move.vim b/.vim/plugin/move.vim
new file mode 100644 (file)
index 0000000..150a170
--- /dev/null
@@ -0,0 +1,141 @@
+" =============================================================================
+" File: plugin/move.vim
+" Description: Move lines and selections up and even down.
+" Author: Matthias Vogelgesang <github.com/matze>
+" =============================================================================
+
+if exists('loaded_move') || &cp
+    finish
+endif
+
+let loaded_move = 1
+
+if !exists('g:move_map_keys')
+    let g:move_map_keys = 1
+endif
+
+if !exists('g:move_key_modifier')
+    let g:move_key_modifier = 'A'
+endif
+
+function! s:ResetCursor()
+    normal! gv=gv^
+endfunction
+
+function! s:MoveBlockDown(start, end, count)
+    let next_line = a:end + a:count
+
+    if v:count > 0
+        let next_line = next_line + v:count - 1
+    endif
+
+    if next_line > line('$')
+        call s:ResetCursor()
+        return
+    endif
+
+    execute "silent" a:start "," a:end "m " next_line
+    call s:ResetCursor()
+endfunction
+
+function! s:MoveBlockUp(start, end, count)
+    let prev_line = a:start - a:count - 1
+
+    if v:count > 0
+        let prev_line = prev_line - v:count + 1
+    endif
+
+    if prev_line < 0
+        call s:ResetCursor()
+        return
+    endif
+
+    execute "silent" a:start "," a:end "m " prev_line
+    call s:ResetCursor()
+endfunction
+
+function! s:MoveLineUp(count) range
+    let distance = a:count + 1
+
+    if v:count > 0
+        let distance = distance + v:count - 1
+    endif
+
+    if (line('.') - distance) < 0
+        execute 'silent m 0'
+        normal! ==
+        return
+    endif
+
+    execute 'silent m-' . distance
+
+    normal! ==
+endfunction
+
+function! s:MoveLineDown(count) range
+    let distance = a:count
+
+    if v:count > 0
+        let distance = distance + v:count - 1
+    endif
+
+    if (line('.') + distance) > line('$')
+        execute 'silent m $'
+        normal! ==
+        return
+    endif
+
+    execute 'silent m+' . distance
+    normal! ==
+endfunction
+
+function! s:MoveBlockOneLineUp() range
+    call s:MoveBlockUp(a:firstline, a:lastline, 1)
+endfunction
+
+function! s:MoveBlockOneLineDown() range
+    call s:MoveBlockDown(a:firstline, a:lastline, 1)
+endfunction
+
+function! s:MoveBlockHalfPageUp() range
+    let distance = winheight('.') / 2
+    call s:MoveBlockUp(a:firstline, a:lastline, distance)
+endfunction
+
+function! s:MoveBlockHalfPageDown() range
+    let distance = winheight('.') / 2
+    call s:MoveBlockDown(a:firstline, a:lastline, distance)
+endfunction
+
+function! s:MoveLineHalfPageUp() range
+    let distance = winheight('.') / 2
+    call s:MoveLineUp(distance)
+endfunction
+
+function! s:MoveLineHalfPageDown() range
+    let distance = winheight('.') / 2
+    call s:MoveLineDown(distance)
+endfunction
+
+function! s:MoveKey(key)
+    return '<' . g:move_key_modifier . '-' . a:key . '>'
+endfunction
+
+
+vnoremap <silent> <Plug>MoveBlockDown           :call <SID>MoveBlockOneLineDown()<CR>
+vnoremap <silent> <Plug>MoveBlockUp             :call <SID>MoveBlockOneLineUp()<CR>
+vnoremap <silent> <Plug>MoveBlockHalfPageDown   :call <SID>MoveBlockHalfPageDown()<CR>
+vnoremap <silent> <Plug>MoveBlockHalfPageUp     :call <SID>MoveBlockHalfPageUp()<CR>
+
+nnoremap <silent> <Plug>MoveLineDown            :call <SID>MoveLineDown(1)<CR>
+nnoremap <silent> <Plug>MoveLineUp              :call <SID>MoveLineUp(1)<CR>
+nnoremap <silent> <Plug>MoveLineHalfPageDown    :call <SID>MoveLineHalfPageDown()<CR>
+nnoremap <silent> <Plug>MoveLineHalfPageUp      :call <SID>MoveLineHalfPageUp()<CR>
+
+
+if g:move_map_keys
+    execute 'vmap' s:MoveKey('j') '<Plug>MoveBlockDown'
+    execute 'vmap' s:MoveKey('k') '<Plug>MoveBlockUp'
+    execute 'nmap' s:MoveKey('j') '<Plug>MoveLineDown'
+    execute 'nmap' s:MoveKey('k') '<Plug>MoveLineUp'
+endif
diff --git a/.vimrc b/.vimrc
index 19d9e77..18e34fa 100644 (file)
--- a/.vimrc
+++ b/.vimrc
@@ -1070,6 +1070,12 @@ if has("autocmd")
 endif
 endif "}}}1
 
+" move.
+nmap <A-u> <Plug>MoveLineHalfPageUp
+nmap <A-d> <Plug>MoveLineHalfPageDown
+vmap <A-u> <Plug>MoveBlockHalfPageUp
+vmap <A-d> <Plug>MoveBlockHalfPageDown
+
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 " Handle options only available in Vim 7 and above.
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""