1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

add mapping to move single or multiple lines

This commit is contained in:
jdhao 2020-09-27 21:29:46 +08:00
parent 2d84307695
commit 5c4223b27d
2 changed files with 53 additions and 0 deletions

View File

@ -85,3 +85,48 @@ function! utils#ToggleCursorCol() abort
echo 'cursorcolumn: ON'
endif
endfunction
function! utils#SwitchLine(src_line_idx, direction) abort
if a:direction ==# 'up'
if a:src_line_idx == 1
return
endif
move-2
elseif a:direction ==# 'down'
if a:src_line_idx == line('$')
return
endif
move+1
endif
endfunction
function! utils#MoveSelection(direction) abort
" only do this if previous mode is visual line mode. Once we press some keys in
" visual line mode, we will leave this mode. So the output of `mode()` will be
" `n` instead of `V`. We can use `visualmode()` instead to check the previous
" mode, see also https://stackoverflow.com/a/61486601/6064933
if visualmode() !=# 'V'
return
endif
let l:start_line = line("'<")
let l:end_line = line("'>")
let l:num_line = l:end_line - l:start_line + 1
if a:direction ==# 'up'
if l:start_line == 1
" we can also directly use `normal gv`, see https://stackoverflow.com/q/9724123/6064933
normal gv
return
endif
silent execute printf('%s,%smove-2', l:start_line, l:end_line)
normal gv
elseif a:direction ==# 'down'
if l:end_line == line('$')
normal gv
return
endif
silent execute printf('%s,%smove+%s', l:start_line, l:end_line, l:num_line)
normal gv
endif
endfunction

View File

@ -167,4 +167,12 @@ nnoremap <silent> <leader>y :%y<CR>
" Toggle cursor column
nnoremap <silent> <leader>cl :call utils#ToggleCursorCol()<CR>
" Move current line up and down
nnoremap <silent> <A-k> <Cmd>call utils#SwitchLine(line('.'), 'up')<CR>
nnoremap <silent> <A-j> <Cmd>call utils#SwitchLine(line('.'), 'down')<CR>
" Move current visual-line selection up and down
xnoremap <silent> <A-k> :<C-U>call utils#MoveSelection('up')<CR>
xnoremap <silent> <A-j> :<C-U>call utils#MoveSelection('down')<CR>
"}