7
|
1 " Vim script to work like "less"
|
|
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
|
5908
|
3 " Last Change: 2014 May 13
|
7
|
4
|
|
5 " Avoid loading this file twice, allow the user to define his own script.
|
|
6 if exists("loaded_less")
|
|
7 finish
|
|
8 endif
|
|
9 let 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.
|
|
13 if 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
|
|
36 endif
|
|
37
|
|
38 set nocp
|
|
39 syntax on
|
|
40 set so=0
|
|
41 set hlsearch
|
|
42 set incsearch
|
|
43 nohlsearch
|
|
44 " Don't remember file names and positions
|
|
45 set viminfo=
|
|
46 set nows
|
|
47 " Inhibit screen updates while searching
|
|
48 let s:lz = &lz
|
|
49 set lz
|
|
50
|
|
51 " Used after each command: put cursor at end and display position
|
|
52 if &wrap
|
|
53 noremap <SID>L L0:redraw<CR>:file<CR>
|
|
54 au VimEnter * normal! L0
|
|
55 else
|
|
56 noremap <SID>L Lg0:redraw<CR>:file<CR>
|
|
57 au VimEnter * normal! Lg0
|
|
58 endif
|
|
59
|
|
60 " When reading from stdin don't consider the file modified.
|
|
61 au VimEnter * set nomod
|
|
62
|
|
63 " Can't modify the text
|
|
64 set noma
|
|
65
|
|
66 " Give help
|
|
67 noremap h :call <SID>Help()<CR>
|
|
68 map H h
|
|
69 fun! 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")
|
|
83 endfun
|
|
84
|
|
85 " Scroll one page forward
|
|
86 noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
|
|
87 map <C-V> <Space>
|
|
88 map f <Space>
|
|
89 map <C-F> <Space>
|
5908
|
90 map <PageDown> <Space>
|
|
91 map <kPageDown> <Space>
|
|
92 map <S-Down> <Space>
|
7
|
93 map z <Space>
|
|
94 map <Esc><Space> <Space>
|
|
95 fun! s:NextPage()
|
|
96 if line(".") == line("$")
|
|
97 if argidx() + 1 >= argc()
|
3513
|
98 " Don't quit at the end of the last file
|
|
99 return
|
7
|
100 endif
|
|
101 next
|
|
102 1
|
|
103 else
|
|
104 exe "normal! \<C-F>"
|
|
105 endif
|
|
106 endfun
|
|
107
|
|
108 " Re-read file and page forward "tail -f"
|
|
109 map F :e<CR>G<SID>L:sleep 1<CR>F
|
|
110
|
|
111 " Scroll half a page forward
|
|
112 noremap <script> d <C-D><SID>L
|
|
113 map <C-D> d
|
|
114
|
|
115 " Scroll one line forward
|
|
116 noremap <script> <CR> <C-E><SID>L
|
|
117 map <C-N> <CR>
|
|
118 map e <CR>
|
|
119 map <C-E> <CR>
|
|
120 map j <CR>
|
|
121 map <C-J> <CR>
|
5908
|
122 map <Down> <CR>
|
7
|
123
|
|
124 " Scroll one page backward
|
|
125 noremap <script> b <C-B><SID>L
|
|
126 map <C-B> b
|
5908
|
127 map <PageUp> b
|
|
128 map <kPageUp> b
|
|
129 map <S-Up> b
|
7
|
130 map w b
|
|
131 map <Esc>v b
|
|
132
|
|
133 " Scroll half a page backward
|
|
134 noremap <script> u <C-U><SID>L
|
|
135 noremap <script> <C-U> <C-U><SID>L
|
|
136
|
|
137 " Scroll one line backward
|
|
138 noremap <script> k <C-Y><SID>L
|
|
139 map y k
|
|
140 map <C-Y> k
|
|
141 map <C-P> k
|
|
142 map <C-K> k
|
5908
|
143 map <Up> k
|
7
|
144
|
|
145 " Redraw
|
|
146 noremap <script> r <C-L><SID>L
|
|
147 noremap <script> <C-R> <C-L><SID>L
|
|
148 noremap <script> R <C-L><SID>L
|
|
149
|
|
150 " Start of file
|
|
151 noremap <script> g gg<SID>L
|
|
152 map < g
|
|
153 map <Esc>< g
|
5908
|
154 map <Home> g
|
|
155 map <kHome> g
|
7
|
156
|
|
157 " End of file
|
|
158 noremap <script> G G<SID>L
|
|
159 map > G
|
|
160 map <Esc>> G
|
5908
|
161 map <End> G
|
|
162 map <kEnd> G
|
7
|
163
|
|
164 " Go to percentage
|
|
165 noremap <script> % %<SID>L
|
|
166 map p %
|
|
167
|
|
168 " Search
|
|
169 noremap <script> / H$:call <SID>Forward()<CR>/
|
|
170 if &wrap
|
|
171 noremap <script> ? H0:call <SID>Backward()<CR>?
|
|
172 else
|
|
173 noremap <script> ? Hg0:call <SID>Backward()<CR>?
|
|
174 endif
|
|
175
|
|
176 fun! 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
|
1125
|
184 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
|
7
|
185 endfun
|
|
186
|
|
187 fun! 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
|
1125
|
195 cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
|
7
|
196 endfun
|
|
197
|
|
198 call s:Forward()
|
5908
|
199 cunmap <CR>
|
7
|
200
|
|
201 " Quitting
|
|
202 noremap q :q<CR>
|
|
203
|
|
204 " Switch to editing (switch off less mode)
|
|
205 map v :silent call <SID>End()<CR>
|
|
206 fun! s:End()
|
|
207 set ma
|
874
|
208 if exists('s:lz')
|
7
|
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 ?
|
5908
|
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>
|
7
|
268 endfun
|
|
269
|
|
270 " vim: sw=2
|