views:

309

answers:

2

Hi all,

I frequently use Shift+J in visual mode to join several selected lines into a single line with the original lines separated by spaces. But I am wondering if there is an opposite shortcut such that it will split selected words into separate lines (one word per line).

Of course I can do:

:'<,'>s/ /^M/g

But something more succinct in terms of keystrokes would be very useful. Has anyone else found a way to do this?

Thanks in advance,

-aj

+3  A: 

Map it if you are using it often in your ~/.vimrc file or similar

vnoremap \ll :'<,'>s/ /^M/g<cr>
nnoremap \ll :s/ /^M/g<cr>

if you are only wanting to to it multiple times now you can use & command to repeat last search also

Theres also gqq but thats for textwidth eg 80 chars

michael
That should probably be `vnoremap` for visually selected words.
too much php
ahh true I hadn't noticed the <>
michael
A: 

Recently I stumbled across the same problem. My solution is the following vim function (put in my .vimrc):

function SplitToLines() range
  for lnum in range(a:lastline, a:firstline, -1)
    let words = split(getline(lnum))
    execute lnum . "delete"
    call append(lnum-1, words)
  endfor
endfunction

This can be used with line ranges, e.g., as follows

:26call SplitToLines()

which would split line number 26. But the code also handles ranges of lines gracefully (that's why the range in the for loop is built in reverse order).

1,10call SplitToLines()

will split lines 1 to 10 into several lines. However, I mostly use this in visual mode, like

'<,'>call SplitToLines()

which splits all lines that are visually marked. Of course you may define some single letter abbreviation for this function call (with auto completion by TAB I do not find it necessary). Also note that by adding an additional argument which would also be used by 'split' you can have a function that does split lines at specific patterns (instead of just white space).

griff