less.vim 5.6 KB

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