On Thu, 15 Jun 2006, Sylvain wrote:

[snip]
I'm using setline() function to replace the line..so I will delete it before I add mine, it's not very elegant so if anyone has a better way, I will take it also :-)
[snip]

I modified Vimtip#329 some time ago to be able to swap visual
highlighted words. I also modified it into a function form because it
got long.

The code below has an advantage over tip#329 in that it does not pollute
the search history. In addition, it also works for swapping words on
different lines.

It does, however, clobber marks 'y & 'z for positioning purposes. You
can code around this if you want.

===
"tested for GVim 6.3 & 7.0
"shift word forward/backward
nnoremap <silent><A-S-Left>  "_yiw:cal ShiftWord(0)<CR>
nnoremap <silent><A-S-Right> "_yiw:cal ShiftWord(1)<CR>
vnoremap <silent><A-S-Left>  <Esc><S-Left><S-Left>:cal 
ShiftWord(2)<CR>myf<C-a>a<BS><Esc>mzgvg`zog`y
vnoremap <silent><A-S-Right> <Esc><S-Right><S-Right>:cal 
ShiftWord(3)<CR>"_xmyf<C-a>a<BS><Esc>mzgvg`yog`z

function! ShiftWord(...)
  let delimiter = "\<C-a>"
  if a:1 == 0
    let @/ = '\w\+\_W\+\%#'
    execute "normal! ?\<CR>"
    let @/ = '\%#\(\w\+\)\(\_W\+\)\(\w\+\)'
    silent! s//\3\2\1
    execute "normal! \<C-o>"
  elseif a:1 == 1
    let @/ = '\%#\(\w\+\)\(\_W\+\)\(\w\+\)'
    silent! s//\3\2\1
    execute "normal! \<C-o>"
    let @/ = '\%#\w\+\_W\+\zs'
    execute "normal! /\<CR>"
  elseif a:1 == 2
    let t_paste = &paste
    set paste
    execute "normal! g`>a\<C-v>".delimiter
    execute "normal! g`<i\<C-v>".delimiter
    let &paste = t_paste
    let @/ = '\w\+\_W\+\w*\%#'
    execute "normal! ?\<CR>"
    let @/ = 
'\%#\(\w\+\)\(\_W\{-1,}\)\(\w*\)'.delimiter.'\(.\{-1,}'.delimiter.'\)'
    silent! s//\4\2\3\1
    execute "normal! \<C-o>"
  elseif a:1 == 3
    let t_paste = &paste
    set paste
    execute "normal! g`>a\<C-v>".delimiter
    execute "normal! g`<i\<C-v>".delimiter
    let &paste = t_paste
    let @/ = '\%#\('.delimiter.'.\{-1,}'.delimiter.'\)\(\w*\)\(\_W\+\)\(\w\+\)'
    silent! s//\4\2\3\1
    execute "normal! \<C-o>"
    let @/ = '\%#\w\+\_W\+\zs'.delimiter
    execute "normal! /\<CR>"
  endif
  nohlsearch
endfunction
===

HTH :)
--
Gerald

Reply via email to