xref: /vim-8.2.3635/runtime/macros/less.vim (revision cf2d8dee)
1" Vim script to work like "less"
2" Maintainer:	Bram Moolenaar <[email protected]>
3" Last Change:	2015 Nov 15
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  echo "\n"
85  echo ":n<Enter> Next file                 :p<Enter> Previous file"
86  echo "\n"
87  echo "q         Quit                      v         Edit file"
88  let i = input("Hit Enter to continue")
89endfun
90
91" Scroll one page forward
92noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
93map <C-V> <Space>
94map f <Space>
95map <C-F> <Space>
96map <PageDown> <Space>
97map <kPageDown> <Space>
98map <S-Down> <Space>
99map z <Space>
100map <Esc><Space> <Space>
101fun! s:NextPage()
102  if line(".") == line("$")
103    if argidx() + 1 >= argc()
104      " Don't quit at the end of the last file
105      return
106    endif
107    next
108    1
109  else
110    exe "normal! \<C-F>"
111  endif
112endfun
113
114" Re-read file and page forward "tail -f"
115map F :e<CR>G<SID>L:sleep 1<CR>F
116
117" Scroll half a page forward
118noremap <script> d <C-D><SID>L
119map <C-D> d
120
121" Scroll one line forward
122noremap <script> <CR> <C-E><SID>L
123map <C-N> <CR>
124map e <CR>
125map <C-E> <CR>
126map j <CR>
127map <C-J> <CR>
128map <Down> <CR>
129
130" Scroll one page backward
131noremap <script> b <C-B><SID>L
132map <C-B> b
133map <PageUp> b
134map <kPageUp> b
135map <S-Up> b
136map w b
137map <Esc>v b
138
139" Scroll half a page backward
140noremap <script> u <C-U><SID>L
141noremap <script> <C-U> <C-U><SID>L
142
143" Scroll one line backward
144noremap <script> k <C-Y><SID>L
145map y k
146map <C-Y> k
147map <C-P> k
148map <C-K> k
149map <Up> k
150
151" Redraw
152noremap <script> r <C-L><SID>L
153noremap <script> <C-R> <C-L><SID>L
154noremap <script> R <C-L><SID>L
155
156" Start of file
157noremap <script> g gg<SID>L
158map < g
159map <Esc>< g
160map <Home> g
161map <kHome> g
162
163" End of file
164noremap <script> G G<SID>L
165map > G
166map <Esc>> G
167map <End> G
168map <kEnd> G
169
170" Go to percentage
171noremap <script> % %<SID>L
172map p %
173
174" Search
175noremap <script> / H$:call <SID>Forward()<CR>/
176if &wrap
177  noremap <script> ? H0:call <SID>Backward()<CR>?
178else
179  noremap <script> ? Hg0:call <SID>Backward()<CR>?
180endif
181
182fun! s:Forward()
183  " Searching forward
184  noremap <script> n H$nzt<SID>L
185  if &wrap
186    noremap <script> N H0Nzt<SID>L
187  else
188    noremap <script> N Hg0Nzt<SID>L
189  endif
190  cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
191endfun
192
193fun! s:Backward()
194  " Searching backward
195  if &wrap
196    noremap <script> n H0nzt<SID>L
197  else
198    noremap <script> n Hg0nzt<SID>L
199  endif
200  noremap <script> N H$Nzt<SID>L
201  cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
202endfun
203
204call s:Forward()
205cunmap <CR>
206
207" Quitting
208noremap q :q<CR>
209
210" Switch to editing (switch off less mode)
211map v :silent call <SID>End()<CR>
212fun! s:End()
213  set ma
214  if exists('s:lz')
215    let &lz = s:lz
216  endif
217  unmap h
218  unmap H
219  unmap <Space>
220  unmap <C-V>
221  unmap f
222  unmap <C-F>
223  unmap z
224  unmap <Esc><Space>
225  unmap F
226  unmap d
227  unmap <C-D>
228  unmap <CR>
229  unmap <C-N>
230  unmap e
231  unmap <C-E>
232  unmap j
233  unmap <C-J>
234  unmap b
235  unmap <C-B>
236  unmap w
237  unmap <Esc>v
238  unmap u
239  unmap <C-U>
240  unmap k
241  unmap y
242  unmap <C-Y>
243  unmap <C-P>
244  unmap <C-K>
245  unmap r
246  unmap <C-R>
247  unmap R
248  unmap g
249  unmap <
250  unmap <Esc><
251  unmap G
252  unmap >
253  unmap <Esc>>
254  unmap %
255  unmap p
256  unmap n
257  unmap N
258  unmap q
259  unmap v
260  unmap /
261  unmap ?
262  unmap <Up>
263  unmap <Down>
264  unmap <PageDown>
265  unmap <kPageDown>
266  unmap <PageUp>
267  unmap <kPageUp>
268  unmap <S-Down>
269  unmap <S-Up>
270  unmap <Home>
271  unmap <kHome>
272  unmap <End>
273  unmap <kEnd>
274endfun
275
276" vim: sw=2
277