xref: /vim-8.2.3635/runtime/macros/less.vim (revision 8ee2d36e)
1" Vim script to work like "less"
2" Maintainer:	Bram Moolenaar <[email protected]>
3" Last Change:	2017 Mar 31
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" Allow the user to define a function, which can set options specifically for
52" this script.
53if exists('*LessInitFunc')
54  call LessInitFunc()
55endif
56
57" Used after each command: put cursor at end and display position
58if &wrap
59  noremap <SID>L L0:redraw<CR>:file<CR>
60  au VimEnter * normal! L0
61else
62  noremap <SID>L Lg0:redraw<CR>:file<CR>
63  au VimEnter * normal! Lg0
64endif
65
66" When reading from stdin don't consider the file modified.
67au VimEnter * set nomod
68
69" Can't modify the text
70set noma
71
72" Give help
73noremap h :call <SID>Help()<CR>
74map H h
75fun! s:Help()
76  echo "<Space>   One page forward          b         One page backward"
77  echo "d         Half a page forward       u         Half a page backward"
78  echo "<Enter>   One line forward          k         One line backward"
79  echo "G         End of file               g         Start of file"
80  echo "N%        percentage in file"
81  echo "\n"
82  echo "/pattern  Search for pattern        ?pattern  Search backward for pattern"
83  echo "n         next pattern match        N         Previous pattern match"
84  if &foldmethod != "manual"
85  echo "\n"
86    echo "zR        open all folds            zm        increase fold level"
87  endif
88  echo "\n"
89  echo ":n<Enter> Next file                 :p<Enter> Previous file"
90  echo "\n"
91  echo "q         Quit                      v         Edit file"
92  let i = input("Hit Enter to continue")
93endfun
94
95" Scroll one page forward
96noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
97map <C-V> <Space>
98map f <Space>
99map <C-F> <Space>
100map <PageDown> <Space>
101map <kPageDown> <Space>
102map <S-Down> <Space>
103" If 'foldmethod' was changed keep the "z" commands, e.g. "zR" to open all
104" folds.
105if &foldmethod == "manual"
106  map z <Space>
107endif
108map <Esc><Space> <Space>
109fun! s:NextPage()
110  if line(".") == line("$")
111    if argidx() + 1 >= argc()
112      " Don't quit at the end of the last file
113      return
114    endif
115    next
116    1
117  else
118    exe "normal! \<C-F>"
119  endif
120endfun
121
122" Re-read file and page forward "tail -f"
123map F :e<CR>G<SID>L:sleep 1<CR>F
124
125" Scroll half a page forward
126noremap <script> d <C-D><SID>L
127map <C-D> d
128
129" Scroll one line forward
130noremap <script> <CR> <C-E><SID>L
131map <C-N> <CR>
132map e <CR>
133map <C-E> <CR>
134map j <CR>
135map <C-J> <CR>
136map <Down> <CR>
137
138" Scroll one page backward
139noremap <script> b <C-B><SID>L
140map <C-B> b
141map <PageUp> b
142map <kPageUp> b
143map <S-Up> b
144map w b
145map <Esc>v b
146
147" Scroll half a page backward
148noremap <script> u <C-U><SID>L
149noremap <script> <C-U> <C-U><SID>L
150
151" Scroll one line backward
152noremap <script> k <C-Y><SID>L
153map y k
154map <C-Y> k
155map <C-P> k
156map <C-K> k
157map <Up> k
158
159" Redraw
160noremap <script> r <C-L><SID>L
161noremap <script> <C-R> <C-L><SID>L
162noremap <script> R <C-L><SID>L
163
164" Start of file
165noremap <script> g gg<SID>L
166map < g
167map <Esc>< g
168map <Home> g
169map <kHome> g
170
171" End of file
172noremap <script> G G<SID>L
173map > G
174map <Esc>> G
175map <End> G
176map <kEnd> G
177
178" Go to percentage
179noremap <script> % %<SID>L
180map p %
181
182" Search
183noremap <script> / H$:call <SID>Forward()<CR>/
184if &wrap
185  noremap <script> ? H0:call <SID>Backward()<CR>?
186else
187  noremap <script> ? Hg0:call <SID>Backward()<CR>?
188endif
189
190fun! s:Forward()
191  " Searching forward
192  noremap <script> n H$nzt<SID>L
193  if &wrap
194    noremap <script> N H0Nzt<SID>L
195  else
196    noremap <script> N Hg0Nzt<SID>L
197  endif
198  cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
199endfun
200
201fun! s:Backward()
202  " Searching backward
203  if &wrap
204    noremap <script> n H0nzt<SID>L
205  else
206    noremap <script> n Hg0nzt<SID>L
207  endif
208  noremap <script> N H$Nzt<SID>L
209  cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
210endfun
211
212call s:Forward()
213cunmap <CR>
214
215" Quitting
216noremap q :q<CR>
217
218" Switch to editing (switch off less mode)
219map v :silent call <SID>End()<CR>
220fun! s:End()
221  set ma
222  if exists('s:lz')
223    let &lz = s:lz
224  endif
225  unmap h
226  unmap H
227  unmap <Space>
228  unmap <C-V>
229  unmap f
230  unmap <C-F>
231  unmap z
232  unmap <Esc><Space>
233  unmap F
234  unmap d
235  unmap <C-D>
236  unmap <CR>
237  unmap <C-N>
238  unmap e
239  unmap <C-E>
240  unmap j
241  unmap <C-J>
242  unmap b
243  unmap <C-B>
244  unmap w
245  unmap <Esc>v
246  unmap u
247  unmap <C-U>
248  unmap k
249  unmap y
250  unmap <C-Y>
251  unmap <C-P>
252  unmap <C-K>
253  unmap r
254  unmap <C-R>
255  unmap R
256  unmap g
257  unmap <
258  unmap <Esc><
259  unmap G
260  unmap >
261  unmap <Esc>>
262  unmap %
263  unmap p
264  unmap n
265  unmap N
266  unmap q
267  unmap v
268  unmap /
269  unmap ?
270  unmap <Up>
271  unmap <Down>
272  unmap <PageDown>
273  unmap <kPageDown>
274  unmap <PageUp>
275  unmap <kPageUp>
276  unmap <S-Down>
277  unmap <S-Up>
278  unmap <Home>
279  unmap <kHome>
280  unmap <End>
281  unmap <kEnd>
282endfun
283
284" vim: sw=2
285