1" Vim script to work like "less" 2" Maintainer: Bram Moolenaar <[email protected]> 3" Last Change: 2014 May 13 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 <PageDown> <Space> 91map <kPageDown> <Space> 92map <S-Down> <Space> 93map z <Space> 94map <Esc><Space> <Space> 95fun! s:NextPage() 96 if line(".") == line("$") 97 if argidx() + 1 >= argc() 98 " Don't quit at the end of the last file 99 return 100 endif 101 next 102 1 103 else 104 exe "normal! \<C-F>" 105 endif 106endfun 107 108" Re-read file and page forward "tail -f" 109map F :e<CR>G<SID>L:sleep 1<CR>F 110 111" Scroll half a page forward 112noremap <script> d <C-D><SID>L 113map <C-D> d 114 115" Scroll one line forward 116noremap <script> <CR> <C-E><SID>L 117map <C-N> <CR> 118map e <CR> 119map <C-E> <CR> 120map j <CR> 121map <C-J> <CR> 122map <Down> <CR> 123 124" Scroll one page backward 125noremap <script> b <C-B><SID>L 126map <C-B> b 127map <PageUp> b 128map <kPageUp> b 129map <S-Up> b 130map w b 131map <Esc>v b 132 133" Scroll half a page backward 134noremap <script> u <C-U><SID>L 135noremap <script> <C-U> <C-U><SID>L 136 137" Scroll one line backward 138noremap <script> k <C-Y><SID>L 139map y k 140map <C-Y> k 141map <C-P> k 142map <C-K> k 143map <Up> k 144 145" Redraw 146noremap <script> r <C-L><SID>L 147noremap <script> <C-R> <C-L><SID>L 148noremap <script> R <C-L><SID>L 149 150" Start of file 151noremap <script> g gg<SID>L 152map < g 153map <Esc>< g 154map <Home> g 155map <kHome> g 156 157" End of file 158noremap <script> G G<SID>L 159map > G 160map <Esc>> G 161map <End> G 162map <kEnd> G 163 164" Go to percentage 165noremap <script> % %<SID>L 166map p % 167 168" Search 169noremap <script> / H$:call <SID>Forward()<CR>/ 170if &wrap 171 noremap <script> ? H0:call <SID>Backward()<CR>? 172else 173 noremap <script> ? Hg0:call <SID>Backward()<CR>? 174endif 175 176fun! s:Forward() 177 " Searching forward 178 noremap <script> n H$nzt<SID>L 179 if &wrap 180 noremap <script> N H0Nzt<SID>L 181 else 182 noremap <script> N Hg0Nzt<SID>L 183 endif 184 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L 185endfun 186 187fun! s:Backward() 188 " Searching backward 189 if &wrap 190 noremap <script> n H0nzt<SID>L 191 else 192 noremap <script> n Hg0nzt<SID>L 193 endif 194 noremap <script> N H$Nzt<SID>L 195 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L 196endfun 197 198call s:Forward() 199cunmap <CR> 200 201" Quitting 202noremap q :q<CR> 203 204" Switch to editing (switch off less mode) 205map v :silent call <SID>End()<CR> 206fun! s:End() 207 set ma 208 if exists('s:lz') 209 let &lz = s:lz 210 endif 211 unmap h 212 unmap H 213 unmap <Space> 214 unmap <C-V> 215 unmap f 216 unmap <C-F> 217 unmap z 218 unmap <Esc><Space> 219 unmap F 220 unmap d 221 unmap <C-D> 222 unmap <CR> 223 unmap <C-N> 224 unmap e 225 unmap <C-E> 226 unmap j 227 unmap <C-J> 228 unmap b 229 unmap <C-B> 230 unmap w 231 unmap <Esc>v 232 unmap u 233 unmap <C-U> 234 unmap k 235 unmap y 236 unmap <C-Y> 237 unmap <C-P> 238 unmap <C-K> 239 unmap r 240 unmap <C-R> 241 unmap R 242 unmap g 243 unmap < 244 unmap <Esc>< 245 unmap G 246 unmap > 247 unmap <Esc>> 248 unmap % 249 unmap p 250 unmap n 251 unmap N 252 unmap q 253 unmap v 254 unmap / 255 unmap ? 256 unmap <Up> 257 unmap <Down> 258 unmap <PageDown> 259 unmap <kPageDown> 260 unmap <PageUp> 261 unmap <kPageUp> 262 unmap <S-Down> 263 unmap <S-Up> 264 unmap <Home> 265 unmap <kHome> 266 unmap <End> 267 unmap <kEnd> 268endfun 269 270" vim: sw=2 271