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