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.

93 lines
2.1 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
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. let lines = getline(lnum1, lnum2)
  13. " let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  14. " let lines[0] = lines[0][col1 - 1:]
  15. let selection = join(lines, ",")
  16. return selection
  17. endfunction
  18. function! s:convert_selection(selection)
  19. " Remove the current selection
  20. execute lnum1 . "," . lnum2 . "delete"
  21. " Setup some variables
  22. let c = 0
  23. let selection = a:selection
  24. let old_selection = split(selection, ",")
  25. let no_of_cols = 6
  26. let new_string = ""
  27. let @z = ""
  28. " Let's print out some info
  29. echom "Old Selection: "
  30. echom join(old_selection, ", ")
  31. " For Loopage Goes here
  32. for i in old_selection
  33. if (c == 0)
  34. " If first selected line
  35. let new_string = join([new_string, old_selection[i]], "")
  36. else
  37. if (c % no_of_cols)
  38. " If regular column
  39. let new_string = join([new_string, old_selection[i]], "\t")
  40. else
  41. " If end of row
  42. let new_string = join([new_string, old_selection[i]], "\n")
  43. endif
  44. endif
  45. let c += 1
  46. endfor
  47. let @z = join([new_string, ""], "\n")
  48. return @z
  49. endfunction
  50. function! s:replace_selected_text()
  51. execute "normal! \"zP"
  52. echo "Just tried to replace the selection."
  53. return ""
  54. endfunction
  55. function! s:makecols() range
  56. let mode = visualmode()
  57. if (mode !=# "V")
  58. echo "You must be in linewise visual mode"
  59. return s:beep()
  60. else
  61. echo "You are in the right mode"
  62. endif
  63. let selection = s:get_visual_selection()
  64. echo selection
  65. let converted_text = s:convert_selection(selection)
  66. return s:replace_selected_text()
  67. endfunction
  68. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  69. " vim:set ft=vim sw=4 sts=4 et: