Last Updated: February 25, 2016
·
1.17K
· glucero

writing sql/one liners in vim

sometimes, i find myself using vim to write long shell/ruby one liners or sql queries on multiple lines.

this is helpful for those times. it copies the visual selection to the clipboard as a joined single line (without modifying the visual selection in place).

vnoremap <silent> <Leader>y "+y:let @+ = join(map(split(@+, '\n'), 'substitute(v:val, "^\\s\\+", "", "")'), " ")<CR>

it turns this:

select
  username, 
  machine, 
  count(*)
from 
  v$session
where 
  machine != 'db1'
group by 
  machine,
  username
order by
  machine

into this:

select username,  machine,  count(*) from  v$session where  machine != 'db1' group by  machine, username order by machine