less.vim 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. " Vim script to work like "less"
  2. " Maintainer: Bram Moolenaar <Bram@vim.org>
  3. " Last Change: 2017 Mar 31
  4. " Avoid loading this file twice, allow the user to define his own script.
  5. if exists("loaded_less")
  6. finish
  7. endif
  8. let loaded_less = 1
  9. " If not reading from stdin, skip files that can't be read.
  10. " Exit if there is no file at all.
  11. if argc() > 0
  12. let s:i = 0
  13. while 1
  14. if filereadable(argv(s:i))
  15. if s:i != 0
  16. sleep 3
  17. endif
  18. break
  19. endif
  20. if isdirectory(argv(s:i))
  21. echomsg "Skipping directory " . argv(s:i)
  22. elseif getftime(argv(s:i)) < 0
  23. echomsg "Skipping non-existing file " . argv(s:i)
  24. else
  25. echomsg "Skipping unreadable file " . argv(s:i)
  26. endif
  27. echo "\n"
  28. let s:i = s:i + 1
  29. if s:i == argc()
  30. quit
  31. endif
  32. next
  33. endwhile
  34. endif
  35. set nocp
  36. syntax on
  37. set so=0
  38. set hlsearch
  39. set incsearch
  40. nohlsearch
  41. " Don't remember file names and positions
  42. set shada=
  43. set nows
  44. " Inhibit screen updates while searching
  45. let s:lz = &lz
  46. set lz
  47. " Allow the user to define a function, which can set options specifically for
  48. " this script.
  49. if exists('*LessInitFunc')
  50. call LessInitFunc()
  51. endif
  52. " Used after each command: put cursor at end and display position
  53. if &wrap
  54. noremap <SID>L L0:redraw<CR>:file<CR>
  55. au VimEnter * normal! L0
  56. else
  57. noremap <SID>L Lg0:redraw<CR>:file<CR>
  58. au VimEnter * normal! Lg0
  59. endif
  60. " When reading from stdin don't consider the file modified.
  61. au VimEnter * set nomod
  62. " Can't modify the text
  63. set noma
  64. " Give help
  65. noremap h :call <SID>Help()<CR>
  66. map H h
  67. fun! s:Help()
  68. echo "<Space> One page forward b One page backward"
  69. echo "d Half a page forward u Half a page backward"
  70. echo "<Enter> One line forward k One line backward"
  71. echo "G End of file g Start of file"
  72. echo "N% percentage in file"
  73. echo "\n"
  74. echo "/pattern Search for pattern ?pattern Search backward for pattern"
  75. echo "n next pattern match N Previous pattern match"
  76. if &foldmethod != "manual"
  77. echo "\n"
  78. echo "zR open all folds zm increase fold level"
  79. endif
  80. echo "\n"
  81. echo ":n<Enter> Next file :p<Enter> Previous file"
  82. echo "\n"
  83. echo "q Quit v Edit file"
  84. let i = input("Hit Enter to continue")
  85. endfun
  86. " Scroll one page forward
  87. noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
  88. map <C-V> <Space>
  89. map f <Space>
  90. map <C-F> <Space>
  91. map <PageDown> <Space>
  92. map <kPageDown> <Space>
  93. map <S-Down> <Space>
  94. " If 'foldmethod' was changed keep the "z" commands, e.g. "zR" to open all
  95. " folds.
  96. if &foldmethod == "manual"
  97. map z <Space>
  98. endif
  99. map <Esc><Space> <Space>
  100. fun! s:NextPage()
  101. if line(".") == line("$")
  102. if argidx() + 1 >= argc()
  103. " Don't quit at the end of the last file
  104. return
  105. endif
  106. next
  107. 1
  108. else
  109. exe "normal! \<C-F>"
  110. endif
  111. endfun
  112. " Re-read file and page forward "tail -f"
  113. map F :e<CR>G<SID>L:sleep 1<CR>F
  114. " Scroll half a page forward
  115. noremap <script> d <C-D><SID>L
  116. map <C-D> d
  117. " Scroll one line forward
  118. noremap <script> <CR> <C-E><SID>L
  119. map <C-N> <CR>
  120. map e <CR>
  121. map <C-E> <CR>
  122. map j <CR>
  123. map <C-J> <CR>
  124. map <Down> <CR>
  125. " Scroll one page backward
  126. noremap <script> b <C-B><SID>L
  127. map <C-B> b
  128. map <PageUp> b
  129. map <kPageUp> b
  130. map <S-Up> b
  131. map w b
  132. map <Esc>v b
  133. " Scroll half a page backward
  134. noremap <script> u <C-U><SID>L
  135. noremap <script> <C-U> <C-U><SID>L
  136. " Scroll one line backward
  137. noremap <script> k <C-Y><SID>L
  138. map y k
  139. map <C-Y> k
  140. map <C-P> k
  141. map <C-K> k
  142. map <Up> k
  143. " Redraw
  144. noremap <script> r <C-L><SID>L
  145. noremap <script> <C-R> <C-L><SID>L
  146. noremap <script> R <C-L><SID>L
  147. " Start of file
  148. noremap <script> g gg<SID>L
  149. map < g
  150. map <Esc>< g
  151. map <Home> g
  152. map <kHome> g
  153. " End of file
  154. noremap <script> G G<SID>L
  155. map > G
  156. map <Esc>> G
  157. map <End> G
  158. map <kEnd> G
  159. " Go to percentage
  160. noremap <script> % %<SID>L
  161. map p %
  162. " Search
  163. noremap <script> / H$:call <SID>Forward()<CR>/
  164. if &wrap
  165. noremap <script> ? H0:call <SID>Backward()<CR>?
  166. else
  167. noremap <script> ? Hg0:call <SID>Backward()<CR>?
  168. endif
  169. fun! s:Forward()
  170. " Searching forward
  171. noremap <script> n H$nzt<SID>L
  172. if &wrap
  173. noremap <script> N H0Nzt<SID>L
  174. else
  175. noremap <script> N Hg0Nzt<SID>L
  176. endif
  177. cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
  178. endfun
  179. fun! s:Backward()
  180. " Searching backward
  181. if &wrap
  182. noremap <script> n H0nzt<SID>L
  183. else
  184. noremap <script> n Hg0nzt<SID>L
  185. endif
  186. noremap <script> N H$Nzt<SID>L
  187. cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
  188. endfun
  189. call s:Forward()
  190. cunmap <CR>
  191. " Quitting
  192. noremap q :q<CR>
  193. " Switch to editing (switch off less mode)
  194. map v :silent call <SID>End()<CR>
  195. fun! s:End()
  196. set ma
  197. if exists('s:lz')
  198. let &lz = s:lz
  199. endif
  200. unmap h
  201. unmap H
  202. unmap <Space>
  203. unmap <C-V>
  204. unmap f
  205. unmap <C-F>
  206. unmap z
  207. unmap <Esc><Space>
  208. unmap F
  209. unmap d
  210. unmap <C-D>
  211. unmap <CR>
  212. unmap <C-N>
  213. unmap e
  214. unmap <C-E>
  215. unmap j
  216. unmap <C-J>
  217. unmap b
  218. unmap <C-B>
  219. unmap w
  220. unmap <Esc>v
  221. unmap u
  222. unmap <C-U>
  223. unmap k
  224. unmap y
  225. unmap <C-Y>
  226. unmap <C-P>
  227. unmap <C-K>
  228. unmap r
  229. unmap <C-R>
  230. unmap R
  231. unmap g
  232. unmap <
  233. unmap <Esc><
  234. unmap G
  235. unmap >
  236. unmap <Esc>>
  237. unmap %
  238. unmap p
  239. unmap n
  240. unmap N
  241. unmap q
  242. unmap v
  243. unmap /
  244. unmap ?
  245. unmap <Up>
  246. unmap <Down>
  247. unmap <PageDown>
  248. unmap <kPageDown>
  249. unmap <PageUp>
  250. unmap <kPageUp>
  251. unmap <S-Down>
  252. unmap <S-Up>
  253. unmap <Home>
  254. unmap <kHome>
  255. unmap <End>
  256. unmap <kEnd>
  257. endfun
  258. " vim: sw=2