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