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.

83 lines
1.9 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
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. " Why is this not a built-in Vim script function?!
  10. let [lnum1, col1] = getpos("'<")[1:2]
  11. let [lnum2, col2] = getpos("'>")[1:2]
  12. execute lnum1 . "," . lnum2 . "delete"
  13. let lines = getline(lnum1, lnum2)
  14. " let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  15. " let lines[0] = lines[0][col1 - 1:]
  16. return join(lines, ",")
  17. endfunction
  18. function! s:convert_selection(selection)
  19. let c = 0
  20. let no_of_cols = 6
  21. let new_string = ""
  22. let old_selection = split(selection, ",")
  23. echom "Old Selection: "
  24. echom join(old_selection, ", ")
  25. let @z = ""
  26. for i in old_selection
  27. " start combining
  28. if (c == 0)
  29. let new_string = join([new_string, old_selection[i]], "")
  30. else
  31. if (c % no_of_cols)
  32. let new_string = join([new_string, old_selection[i]], "\t")
  33. else
  34. let new_string = join([new_string, old_selection[i]], "\n")
  35. endif
  36. endif
  37. let c += 1
  38. endfor
  39. let @z = join([new_string, ""], "\n")
  40. return old_selection
  41. endfunction
  42. function! s:replace_selected_text()
  43. execute "normal! \"zP"
  44. echo "Just tried to replace the selection."
  45. return ""
  46. endfunction
  47. function! s:makecols() range
  48. let mode = visualmode()
  49. if (mode !=# "V")
  50. echo "You must be in linewise visual mode"
  51. return s:beep()
  52. else
  53. echo "You are in the right mode"
  54. endif
  55. let selection = s:get_visual_selection()
  56. s:convert_selection(selection)
  57. return s:replace_selected_text()
  58. endfunction
  59. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  60. " vim:set ft=vim sw=4 sts=4 et: