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.

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