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