makecols.vim: converting lists to columns made simple
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. " makecols.vim - Makecolumns
  2. " Author: Levi Olson <http://leviolson.com/>
  3. " Version: 1.0
  4. function! s:beep()
  5. exe "norm! \<Esc>"
  6. return ""
  7. endfunction
  8. function! s:get_visual_selection()
  9. let [lnum1, col1] = getpos("'<")[1:2]
  10. let [lnum2, col2] = getpos("'>")[1:2]
  11. " let no_of_lines = lnum2 - lnum1
  12. let lines = getline(lnum1, lnum2)
  13. execute lnum1 . "," . lnum2 . "delete"
  14. " let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  15. " let lines[0] = lines[0][col1 - 1:]
  16. let c = 0
  17. let no_of_cols = 6
  18. let new_string = ""
  19. let @z = ""
  20. for i in lines
  21. " start combining
  22. if (c == 0)
  23. let new_string = join([new_string, lines[i]], "")
  24. else
  25. if (c % no_of_cols)
  26. let new_string = join([new_string, lines[i]], "\t")
  27. else
  28. let new_string = join([new_string, lines[i]], "\n")
  29. endif
  30. endif
  31. let c += 1
  32. endfor
  33. let @z = new_string
  34. return lines
  35. endfunction
  36. function! s:replace_selected_text()
  37. execute "normal! \"zP"
  38. echo "Just tried to replace the selection with the lines."
  39. return ""
  40. endfunction
  41. function! s:makecols() range
  42. let mode = visualmode()
  43. if (mode !=# "V")
  44. echo "You must be in linewise visual mode"
  45. return s:beep()
  46. else
  47. echo "You are in the right mode"
  48. endif
  49. echo s:get_visual_selection()
  50. return s:replace_selected_text()
  51. endfunction
  52. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  53. " vim:set ft=vim sw=4 sts=4 et: