blob: 17433f4ab8f3d3a94e41a7c370c6f46d48e13f41 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim script to work like "less"
2" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar4072ba52020-12-23 13:56:35 +01003" Last Change: 2020 Dec 17
Bram Moolenaar071d4272004-06-13 20:20:40 +00004
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
Bram Moolenaar4072ba52020-12-23 13:56:35 +010038" we don't want 'compatible' here
39if &cp
40 set nocp
41endif
42
43" enable syntax highlighting if not done already
44if !get(g:, 'syntax_on', 0)
45 syntax enable
46endif
47
Bram Moolenaar071d4272004-06-13 20:20:40 +000048set so=0
49set hlsearch
50set incsearch
51nohlsearch
52" Don't remember file names and positions
53set viminfo=
54set nows
55" Inhibit screen updates while searching
56let s:lz = &lz
57set lz
58
Bram Moolenaare392eb42015-11-19 20:38:09 +010059" Allow the user to define a function, which can set options specifically for
60" this script.
61if exists('*LessInitFunc')
62 call LessInitFunc()
63endif
64
Bram Moolenaar071d4272004-06-13 20:20:40 +000065" Used after each command: put cursor at end and display position
66if &wrap
67 noremap <SID>L L0:redraw<CR>:file<CR>
68 au VimEnter * normal! L0
69else
70 noremap <SID>L Lg0:redraw<CR>:file<CR>
71 au VimEnter * normal! Lg0
72endif
73
74" When reading from stdin don't consider the file modified.
75au VimEnter * set nomod
76
Bram Moolenaar388a5d42020-05-26 21:20:45 +020077" Can't modify the text or write the file.
78set nomodifiable readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80" Give help
81noremap h :call <SID>Help()<CR>
82map H h
83fun! s:Help()
84 echo "<Space> One page forward b One page backward"
85 echo "d Half a page forward u Half a page backward"
86 echo "<Enter> One line forward k One line backward"
87 echo "G End of file g Start of file"
88 echo "N% percentage in file"
89 echo "\n"
90 echo "/pattern Search for pattern ?pattern Search backward for pattern"
91 echo "n next pattern match N Previous pattern match"
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020092 if &foldmethod != "manual"
93 echo "\n"
94 echo "zR open all folds zm increase fold level"
95 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 echo "\n"
97 echo ":n<Enter> Next file :p<Enter> Previous file"
98 echo "\n"
99 echo "q Quit v Edit file"
100 let i = input("Hit Enter to continue")
101endfun
102
103" Scroll one page forward
104noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
105map <C-V> <Space>
106map f <Space>
107map <C-F> <Space>
Bram Moolenaare968e362014-05-13 20:23:24 +0200108map <PageDown> <Space>
109map <kPageDown> <Space>
110map <S-Down> <Space>
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200111" If 'foldmethod' was changed keep the "z" commands, e.g. "zR" to open all
112" folds.
113if &foldmethod == "manual"
114 map z <Space>
115endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116map <Esc><Space> <Space>
117fun! s:NextPage()
118 if line(".") == line("$")
119 if argidx() + 1 >= argc()
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200120 " Don't quit at the end of the last file
121 return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 endif
123 next
124 1
125 else
126 exe "normal! \<C-F>"
127 endif
128endfun
129
130" Re-read file and page forward "tail -f"
131map F :e<CR>G<SID>L:sleep 1<CR>F
132
133" Scroll half a page forward
134noremap <script> d <C-D><SID>L
135map <C-D> d
136
137" Scroll one line forward
138noremap <script> <CR> <C-E><SID>L
139map <C-N> <CR>
140map e <CR>
141map <C-E> <CR>
142map j <CR>
143map <C-J> <CR>
Bram Moolenaare968e362014-05-13 20:23:24 +0200144map <Down> <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146" Scroll one page backward
147noremap <script> b <C-B><SID>L
148map <C-B> b
Bram Moolenaare968e362014-05-13 20:23:24 +0200149map <PageUp> b
150map <kPageUp> b
151map <S-Up> b
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152map w b
153map <Esc>v b
154
155" Scroll half a page backward
156noremap <script> u <C-U><SID>L
157noremap <script> <C-U> <C-U><SID>L
158
159" Scroll one line backward
160noremap <script> k <C-Y><SID>L
161map y k
162map <C-Y> k
163map <C-P> k
164map <C-K> k
Bram Moolenaare968e362014-05-13 20:23:24 +0200165map <Up> k
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
167" Redraw
168noremap <script> r <C-L><SID>L
169noremap <script> <C-R> <C-L><SID>L
170noremap <script> R <C-L><SID>L
171
172" Start of file
173noremap <script> g gg<SID>L
174map < g
175map <Esc>< g
Bram Moolenaare968e362014-05-13 20:23:24 +0200176map <Home> g
177map <kHome> g
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
179" End of file
180noremap <script> G G<SID>L
181map > G
182map <Esc>> G
Bram Moolenaare968e362014-05-13 20:23:24 +0200183map <End> G
184map <kEnd> G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186" Go to percentage
187noremap <script> % %<SID>L
188map p %
189
190" Search
191noremap <script> / H$:call <SID>Forward()<CR>/
192if &wrap
193 noremap <script> ? H0:call <SID>Backward()<CR>?
194else
195 noremap <script> ? Hg0:call <SID>Backward()<CR>?
196endif
197
198fun! s:Forward()
199 " Searching forward
200 noremap <script> n H$nzt<SID>L
201 if &wrap
202 noremap <script> N H0Nzt<SID>L
203 else
204 noremap <script> N Hg0Nzt<SID>L
205 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000206 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207endfun
208
209fun! s:Backward()
210 " Searching backward
211 if &wrap
212 noremap <script> n H0nzt<SID>L
213 else
214 noremap <script> n Hg0nzt<SID>L
215 endif
216 noremap <script> N H$Nzt<SID>L
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000217 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218endfun
219
220call s:Forward()
Bram Moolenaare968e362014-05-13 20:23:24 +0200221cunmap <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
223" Quitting
224noremap q :q<CR>
225
226" Switch to editing (switch off less mode)
227map v :silent call <SID>End()<CR>
228fun! s:End()
229 set ma
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000230 if exists('s:lz')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 let &lz = s:lz
232 endif
233 unmap h
234 unmap H
235 unmap <Space>
236 unmap <C-V>
237 unmap f
238 unmap <C-F>
239 unmap z
240 unmap <Esc><Space>
241 unmap F
242 unmap d
243 unmap <C-D>
244 unmap <CR>
245 unmap <C-N>
246 unmap e
247 unmap <C-E>
248 unmap j
249 unmap <C-J>
250 unmap b
251 unmap <C-B>
252 unmap w
253 unmap <Esc>v
254 unmap u
255 unmap <C-U>
256 unmap k
257 unmap y
258 unmap <C-Y>
259 unmap <C-P>
260 unmap <C-K>
261 unmap r
262 unmap <C-R>
263 unmap R
264 unmap g
265 unmap <
266 unmap <Esc><
267 unmap G
268 unmap >
269 unmap <Esc>>
270 unmap %
271 unmap p
272 unmap n
273 unmap N
274 unmap q
275 unmap v
276 unmap /
277 unmap ?
Bram Moolenaare968e362014-05-13 20:23:24 +0200278 unmap <Up>
279 unmap <Down>
280 unmap <PageDown>
281 unmap <kPageDown>
282 unmap <PageUp>
283 unmap <kPageUp>
284 unmap <S-Down>
285 unmap <S-Up>
286 unmap <Home>
287 unmap <kHome>
288 unmap <End>
289 unmap <kEnd>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290endfun
291
292" vim: sw=2