Install vim74
[msysgit.git] / share / vim / vim74 / macros / less.vim
blob6ae5ebc63ba01edabfb19a31ef5a24103445124f
1 " Vim script to work like "less"
2 " Maintainer:   Bram Moolenaar <Bram@vim.org>
3 " Last Change:  2012 May 18
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
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
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
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
60 " When reading from stdin don't consider the file modified.
61 au VimEnter * set nomod
63 " Can't modify the text
64 set noma
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
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>
90 map z <Space>
91 map <Esc><Space> <Space>
92 fun! s:NextPage()
93   if line(".") == line("$")
94     if argidx() + 1 >= argc()
95       " Don't quit at the end of the last file
96       return
97     endif
98     next
99     1
100   else
101     exe "normal! \<C-F>"
102   endif
103 endfun
105 " Re-read file and page forward "tail -f"
106 map F :e<CR>G<SID>L:sleep 1<CR>F
108 " Scroll half a page forward
109 noremap <script> d <C-D><SID>L
110 map <C-D> d
112 " Scroll one line forward
113 noremap <script> <CR> <C-E><SID>L
114 map <C-N> <CR>
115 map e <CR>
116 map <C-E> <CR>
117 map j <CR>
118 map <C-J> <CR>
120 " Scroll one page backward
121 noremap <script> b <C-B><SID>L
122 map <C-B> b
123 map w b
124 map <Esc>v b
126 " Scroll half a page backward
127 noremap <script> u <C-U><SID>L
128 noremap <script> <C-U> <C-U><SID>L
130 " Scroll one line backward
131 noremap <script> k <C-Y><SID>L
132 map y k
133 map <C-Y> k
134 map <C-P> k
135 map <C-K> k
137 " Redraw
138 noremap <script> r <C-L><SID>L
139 noremap <script> <C-R> <C-L><SID>L
140 noremap <script> R <C-L><SID>L
142 " Start of file
143 noremap <script> g gg<SID>L
144 map < g
145 map <Esc>< g
147 " End of file
148 noremap <script> G G<SID>L
149 map > G
150 map <Esc>> G
152 " Go to percentage
153 noremap <script> % %<SID>L
154 map p %
156 " Search
157 noremap <script> / H$:call <SID>Forward()<CR>/
158 if &wrap
159   noremap <script> ? H0:call <SID>Backward()<CR>?
160 else
161   noremap <script> ? Hg0:call <SID>Backward()<CR>?
162 endif
164 fun! s:Forward()
165   " Searching forward
166   noremap <script> n H$nzt<SID>L
167   if &wrap
168     noremap <script> N H0Nzt<SID>L
169   else
170     noremap <script> N Hg0Nzt<SID>L
171   endif
172   cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
173 endfun
175 fun! s:Backward()
176   " Searching backward
177   if &wrap
178     noremap <script> n H0nzt<SID>L
179   else
180     noremap <script> n Hg0nzt<SID>L
181   endif
182   noremap <script> N H$Nzt<SID>L
183   cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
184 endfun
186 call s:Forward()
188 " Quitting
189 noremap q :q<CR>
191 " Switch to editing (switch off less mode)
192 map v :silent call <SID>End()<CR>
193 fun! s:End()
194   set ma
195   if exists('s:lz')
196     let &lz = s:lz
197   endif
198   unmap h
199   unmap H
200   unmap <Space>
201   unmap <C-V>
202   unmap f
203   unmap <C-F>
204   unmap z
205   unmap <Esc><Space>
206   unmap F
207   unmap d
208   unmap <C-D>
209   unmap <CR>
210   unmap <C-N>
211   unmap e
212   unmap <C-E>
213   unmap j
214   unmap <C-J>
215   unmap b
216   unmap <C-B>
217   unmap w
218   unmap <Esc>v
219   unmap u
220   unmap <C-U>
221   unmap k
222   unmap y
223   unmap <C-Y>
224   unmap <C-P>
225   unmap <C-K>
226   unmap r
227   unmap <C-R>
228   unmap R
229   unmap g
230   unmap <
231   unmap <Esc><
232   unmap G
233   unmap >
234   unmap <Esc>>
235   unmap %
236   unmap p
237   unmap n
238   unmap N
239   unmap q
240   unmap v
241   unmap /
242   unmap ?
243 endfun
245 " vim: sw=2