blob: 95a4127961a9c424f7555ffbed2e4f17bb8d8ff6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim script to work like "less"
Christian Brabandte978b452023-08-13 10:33:05 +02002" Maintainer: The Vim Project <https://github.com/vim/vim>
3" Last Change: 2023 Aug 10
4" Former Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
6" Avoid loading this file twice, allow the user to define his own script.
7if exists("loaded_less")
8 finish
9endif
10let 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.
14if 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
37endif
38
Bram Moolenaar4072ba52020-12-23 13:56:35 +010039" we don't want 'compatible' here
40if &cp
41 set nocp
42endif
43
44" enable syntax highlighting if not done already
45if !get(g:, 'syntax_on', 0)
46 syntax enable
47endif
48
Bram Moolenaar071d4272004-06-13 20:20:40 +000049set so=0
50set hlsearch
51set incsearch
52nohlsearch
53" Don't remember file names and positions
54set viminfo=
55set nows
56" Inhibit screen updates while searching
57let s:lz = &lz
58set lz
59
Bram Moolenaare392eb42015-11-19 20:38:09 +010060" Allow the user to define a function, which can set options specifically for
61" this script.
62if exists('*LessInitFunc')
63 call LessInitFunc()
64endif
65
Bram Moolenaar071d4272004-06-13 20:20:40 +000066" Used after each command: put cursor at end and display position
67if &wrap
68 noremap <SID>L L0:redraw<CR>:file<CR>
69 au VimEnter * normal! L0
70else
71 noremap <SID>L Lg0:redraw<CR>:file<CR>
72 au VimEnter * normal! Lg0
73endif
74
75" When reading from stdin don't consider the file modified.
76au VimEnter * set nomod
77
Bram Moolenaar388a5d42020-05-26 21:20:45 +020078" Can't modify the text or write the file.
79set nomodifiable readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81" Give help
82noremap h :call <SID>Help()<CR>
83map H h
84fun! s:Help()
85 echo "<Space> One page forward b One page backward"
86 echo "d Half a page forward u Half a page backward"
87 echo "<Enter> One line forward k One line backward"
88 echo "G End of file g Start of file"
89 echo "N% percentage in file"
90 echo "\n"
91 echo "/pattern Search for pattern ?pattern Search backward for pattern"
92 echo "n next pattern match N Previous pattern match"
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020093 if &foldmethod != "manual"
94 echo "\n"
95 echo "zR open all folds zm increase fold level"
96 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 echo "\n"
98 echo ":n<Enter> Next file :p<Enter> Previous file"
99 echo "\n"
100 echo "q Quit v Edit file"
101 let i = input("Hit Enter to continue")
102endfun
103
104" Scroll one page forward
105noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
106map <C-V> <Space>
107map f <Space>
108map <C-F> <Space>
Bram Moolenaare968e362014-05-13 20:23:24 +0200109map <PageDown> <Space>
110map <kPageDown> <Space>
111map <S-Down> <Space>
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200112" If 'foldmethod' was changed keep the "z" commands, e.g. "zR" to open all
113" folds.
114if &foldmethod == "manual"
115 map z <Space>
116endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117map <Esc><Space> <Space>
118fun! s:NextPage()
119 if line(".") == line("$")
120 if argidx() + 1 >= argc()
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200121 " Don't quit at the end of the last file
122 return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 endif
124 next
125 1
126 else
127 exe "normal! \<C-F>"
128 endif
129endfun
130
131" Re-read file and page forward "tail -f"
132map F :e<CR>G<SID>L:sleep 1<CR>F
133
134" Scroll half a page forward
135noremap <script> d <C-D><SID>L
136map <C-D> d
137
138" Scroll one line forward
139noremap <script> <CR> <C-E><SID>L
140map <C-N> <CR>
141map e <CR>
142map <C-E> <CR>
143map j <CR>
144map <C-J> <CR>
Bram Moolenaare968e362014-05-13 20:23:24 +0200145map <Down> <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147" Scroll one page backward
148noremap <script> b <C-B><SID>L
149map <C-B> b
Bram Moolenaare968e362014-05-13 20:23:24 +0200150map <PageUp> b
151map <kPageUp> b
152map <S-Up> b
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153map w b
154map <Esc>v b
155
156" Scroll half a page backward
157noremap <script> u <C-U><SID>L
158noremap <script> <C-U> <C-U><SID>L
159
160" Scroll one line backward
161noremap <script> k <C-Y><SID>L
162map y k
163map <C-Y> k
164map <C-P> k
165map <C-K> k
Bram Moolenaare968e362014-05-13 20:23:24 +0200166map <Up> k
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168" Redraw
169noremap <script> r <C-L><SID>L
170noremap <script> <C-R> <C-L><SID>L
171noremap <script> R <C-L><SID>L
172
173" Start of file
174noremap <script> g gg<SID>L
175map < g
176map <Esc>< g
Bram Moolenaare968e362014-05-13 20:23:24 +0200177map <Home> g
178map <kHome> g
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
180" End of file
181noremap <script> G G<SID>L
182map > G
183map <Esc>> G
Bram Moolenaare968e362014-05-13 20:23:24 +0200184map <End> G
185map <kEnd> G
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
187" Go to percentage
188noremap <script> % %<SID>L
189map p %
190
191" Search
192noremap <script> / H$:call <SID>Forward()<CR>/
193if &wrap
194 noremap <script> ? H0:call <SID>Backward()<CR>?
195else
196 noremap <script> ? Hg0:call <SID>Backward()<CR>?
197endif
198
199fun! s:Forward()
200 " Searching forward
201 noremap <script> n H$nzt<SID>L
202 if &wrap
203 noremap <script> N H0Nzt<SID>L
204 else
205 noremap <script> N Hg0Nzt<SID>L
206 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000207 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208endfun
209
210fun! s:Backward()
211 " Searching backward
212 if &wrap
213 noremap <script> n H0nzt<SID>L
214 else
215 noremap <script> n Hg0nzt<SID>L
216 endif
217 noremap <script> N H$Nzt<SID>L
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000218 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219endfun
220
221call s:Forward()
Bram Moolenaare968e362014-05-13 20:23:24 +0200222cunmap <CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
224" Quitting
225noremap q :q<CR>
226
227" Switch to editing (switch off less mode)
228map v :silent call <SID>End()<CR>
229fun! s:End()
230 set ma
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000231 if exists('s:lz')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 let &lz = s:lz
233 endif
234 unmap h
235 unmap H
236 unmap <Space>
237 unmap <C-V>
238 unmap f
239 unmap <C-F>
240 unmap z
241 unmap <Esc><Space>
242 unmap F
243 unmap d
244 unmap <C-D>
245 unmap <CR>
246 unmap <C-N>
247 unmap e
248 unmap <C-E>
249 unmap j
250 unmap <C-J>
251 unmap b
252 unmap <C-B>
253 unmap w
254 unmap <Esc>v
255 unmap u
256 unmap <C-U>
257 unmap k
258 unmap y
259 unmap <C-Y>
260 unmap <C-P>
261 unmap <C-K>
262 unmap r
263 unmap <C-R>
264 unmap R
265 unmap g
266 unmap <
267 unmap <Esc><
268 unmap G
269 unmap >
270 unmap <Esc>>
271 unmap %
272 unmap p
273 unmap n
274 unmap N
275 unmap q
276 unmap v
277 unmap /
278 unmap ?
Bram Moolenaare968e362014-05-13 20:23:24 +0200279 unmap <Up>
280 unmap <Down>
281 unmap <PageDown>
282 unmap <kPageDown>
283 unmap <PageUp>
284 unmap <kPageUp>
285 unmap <S-Down>
286 unmap <S-Up>
287 unmap <Home>
288 unmap <kHome>
289 unmap <End>
290 unmap <kEnd>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291endfun
292
293" vim: sw=2