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.

85 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
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 selection = a:selection
  21. let no_of_cols = 6
  22. let new_string = ""
  23. let old_selection = split(selection, ",")
  24. echom "Old Selection: "
  25. echom join(old_selection, ", ")
  26. let @z = ""
  27. for i in old_selection
  28. " start combining
  29. if (c == 0)
  30. let new_string = join([new_string, old_selection[i]], "")
  31. else
  32. if (c % no_of_cols)
  33. let new_string = join([new_string, old_selection[i]], "\t")
  34. else
  35. let new_string = join([new_string, old_selection[i]], "\n")
  36. endif
  37. endif
  38. let c += 1
  39. endfor
  40. let @z = join([new_string, ""], "\n")
  41. return @z
  42. endfunction
  43. function! s:replace_selected_text()
  44. execute "normal! \"zP"
  45. echo "Just tried to replace the selection."
  46. return ""
  47. endfunction
  48. function! s:makecols() range
  49. let mode = visualmode()
  50. if (mode !=# "V")
  51. echo "You must be in linewise visual mode"
  52. return s:beep()
  53. else
  54. echo "You are in the right mode"
  55. endif
  56. let selection = s:get_visual_selection()
  57. echo selection
  58. let converted_text = s:convert_selection(selection)
  59. return s:replace_selected_text()
  60. endfunction
  61. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  62. " vim:set ft=vim sw=4 sts=4 et: