public
Description: My configuration files feat. hardcore Vim scripts
Homepage: http://kana.github.com/config/vim/
Clone URL: git://github.com/kana/config.git
config / vim / dot.vimrc
661b53ea » kana 2008-02-15 vim/dot.vimrc: 1 " My .vimrc
8fdfe5f1 » kana 2007-10-10 Notes: New section. 2 " Notes "{{{1
3 "
4 " * This file consists of "sections".
5 "
ee2fb8d1 » kana 2008-05-04 vim: vimrc: Update some ite... 6 " - The name of each section should be single word.
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 7 "
8fdfe5f1 » kana 2007-10-10 Notes: New section. 8 " * Each section consists of zero or more "subsections".
9 "
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 10 " - There is no rule for the name of each subsection.
11 "
b6850cdf » kana 2007-10-10 Divide MISC. section into P... 12 " * The last subsection in a section should be named as "Misc.".
8fdfe5f1 » kana 2007-10-10 Notes: New section. 13 "
14 " * Whenever new subsection is inserted,
ee2fb8d1 » kana 2008-05-04 vim: vimrc: Update some ite... 15 " it should be inserted just before "Misc." subsection.
8fdfe5f1 » kana 2007-10-10 Notes: New section. 16 "
17 " * If a setting can be categorized into two or more sections,
18 " it should be put into the most bottom section in this file.
19 "
20 " For example, key mappings for a specific plugin should be put into the
ee2fb8d1 » kana 2008-05-04 vim: vimrc: Update some ite... 21 " "Plugins" section.
8fdfe5f1 » kana 2007-10-10 Notes: New section. 22 "
23 "
24 " Coding Rule
25 "
26 " * Separate sections with 8 blank lines.
27 "
28 " * Separate subsections with 4 blank lines.
29 "
ee2fb8d1 » kana 2008-05-04 vim: vimrc: Update some ite... 30 " * Indentation: See the modelines at the bottom of this file.
31 "
32 " * Character Encoding: Use UTF-8 for this file and other files such as
33 " plugins, but this file must contain only unibyte (i.e. 7-bit ASCII)
34 " characters.
35 "
36 " Because changing 'encoding' does not affect the character encoding of
37 " existing buffers, so that each multibyte character will be treated as
38 " multiple single byte characters at the first time of loading this file.
39 " For example, if this file contains a character U+3042 (Japanese Hiragana
40 " "A"), it will be converted into 3 independent bytes -- E3, 81 and 82.
8fdfe5f1 » kana 2007-10-10 Notes: New section. 41 "
42 " * Limit all lines to a maximum of 79 characters.
43 "
44 " * Separate {lhs} and {rhs} of key mappings with 2 spaces.
45 "
46 " * Separate {cmd} and {rep} of :command definitions with 2 spaces.
47 "
a74ad62d » kana 2008-08-24 vim: vimrc: New coding rule... 48 " * Sort arguments for :command such as -nargs=* and others by alphabetically
49 " order.
50 "
e63e08e5 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 51 " * Write the full name for each command -- don't abbreviate it.
52 " For example, write "nnoremap", not "nn".
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 53 "
8fdfe5f1 » kana 2007-10-10 Notes: New section. 54 " * Key Notation:
55 "
56 " - Control-keys: Write as , neither nor .
57 "
58 " - Carriage return: Write as , neither nor .
59 "
60 " - Other characters: Write as same as :help key-notation
bc03e7c8 » kana 2008-01-12 vim/dot.vimrc: 61 "
62 " * Line continuation:
63 "
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 64 " - At the middle of key mappings, abbreviations and other proler places:
65 " Write "\" at the previous column of the start of the {rhs}.
bc03e7c8 » kana 2008-01-12 vim/dot.vimrc: 66 "
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 67 " - Others: Write "\" at the same column of the beggining of the command.
bc03e7c8 » kana 2008-01-12 vim/dot.vimrc: 68 "
69 " - Examples:
70 "
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 71 " execute "echo"
72 " \ "foo"
73 " \ "baz"
bc03e7c8 » kana 2008-01-12 vim/dot.vimrc: 74 "
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 75 " map xyzzy :if has('cryptv')
76 " \| X
77 " \|endif
ab470bba » kana 2008-05-04 vim: vimrc: Update "Notes" ... 78 "
79 " * Don't align columns. So don't write like the following code:
80 "
81 " map foo bar
82 " noremap bar baz
83 "
84 " instead, write like the following code:
85 "
86 " map foo bar
87 " noremap bar baz
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 88 "
89 " * Naming:
90 "
91 " - Functions: Don't use upper case characters if possible.
92 "
93 " - Functions: Use "cmd_{command}" for a {command} such as:
94 " command! -nargs=* {command} call s:cmd_{command}()
95 "
96 " - Functions: Use "complete_{type}" for :command-completion-custom or
97 " :command-completion-customlist functions.
98 "
99 " - Functions: Use "on_{event}_{mod}" for a handler of :autocmd {event}.
26dcd217 » kana 2008-12-11 vim: vimrc: Normalize a nam... 100 "
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 101 " - Use "tabpage" instead of "tab_page" or "tab page" or "tab".
0cc2491f » kana 2009-05-22 vimrc: Add :g/re/y - yank m... 102 "
103 " * Register usage:
104 "
105 " - "g for :global.
106 "
107 " - Don't overwrite other named registers.
8fdfe5f1 » kana 2007-10-10 Notes: New section. 108
109
110
111
112
113
114
115
88559863 » kana 2007-10-10 Rearrange SETTINGS WHICH AR... 116 " Basic "{{{1
117 " Absolute "{{{2
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 118
a4fe3709 » kana 2009-05-13 vim: vimrc: Don't reset 'co... 119 if !exists('s:loaded_my_vimrc')
120 " Don't reset twice on reloading - 'compatible' has SO many side effects.
121 set nocompatible " to use many extensions of Vim.
122 endif
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 123
124
f6426230 » kana 2008-02-24 Vim: vimrc: Improve the pla... 125 function! s:SID_PREFIX()
126 return matchstr(expand(''), '\d\+_')
127 endfunction
128
129
88559863 » kana 2007-10-10 Rearrange SETTINGS WHICH AR... 130
131
132 " Encoding "{{{2
133
195a83f9 » kana 2007-02-16 Fix the layout. 134 " To deal with Japanese language.
2b61c23d » kana 2009-04-28 sh: ENV_* - Rename "cygwin"... 135 if $ENV_WORKING ==# 'summer'
eafef300 » kana 2007-09-10 dot.vimrc: 136 set encoding=japan
2ba08681 » kana 2009-04-27 vim: vimrc: Change the poli... 137 else
138 set encoding=utf-8
eafef300 » kana 2007-09-10 dot.vimrc: 139 endif
c7010d9a » kana 2009-01-12 vim: vimrc: Misc. improveme... 140
141 if has('iconv')
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 142 let s:enc_euc = 'euc-jp'
143 let s:enc_jis = 'iso-2022-jp'
144
145 " Does iconv support JIS X 0213 ?
692abb34 » kana 2008-01-12 vim/dot.vimrc: 146 if iconv("\x87\x64\x87\x6a", 'cp932', 'euc-jisx0213') ==# "\xad\xc5\xad\xcb"
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 147 let s:enc_euc = 'euc-jisx0213,euc-jp'
148 let s:enc_jis = 'iso-2022-jp-3'
149 endif
150
151 " Make fileencodings
eafef300 » kana 2007-09-10 dot.vimrc: 152 let &fileencodings = 'ucs-bom'
e2da61a6 » kana 2007-09-10 Fix character encoding sett... 153 if &encoding !=# 'utf-8'
154 let &fileencodings = &fileencodings . ',' . 'ucs-2le'
155 let &fileencodings = &fileencodings . ',' . 'ucs-2'
156 endif
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 157 let &fileencodings = &fileencodings . ',' . s:enc_jis
158
eafef300 » kana 2007-09-10 dot.vimrc: 159 if &encoding ==# 'utf-8'
160 let &fileencodings = &fileencodings . ',' . s:enc_euc
161 let &fileencodings = &fileencodings . ',' . 'cp932'
162 elseif &encoding =~# '^euc-\%(jp\|jisx0213\)$'
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 163 let &encoding = s:enc_euc
eafef300 » kana 2007-09-10 dot.vimrc: 164 let &fileencodings = &fileencodings . ',' . 'utf-8'
165 let &fileencodings = &fileencodings . ',' . 'cp932'
166 else " cp932
167 let &fileencodings = &fileencodings . ',' . 'utf-8'
168 let &fileencodings = &fileencodings . ',' . s:enc_euc
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 169 endif
eafef300 » kana 2007-09-10 dot.vimrc: 170 let &fileencodings = &fileencodings . ',' . &encoding
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 171
172 unlet s:enc_euc
173 unlet s:enc_jis
174 endif
175
176
2b61c23d » kana 2009-04-28 sh: ENV_* - Rename "cygwin"... 177 if $ENV_ACCESS ==# 'summer'
b765a4dc » kana 2007-05-05 Set proper 'termencoding' o... 178 set termencoding=cp932
1e8f2dd7 » kana 2007-11-17 Basic / Encoding: 179 else " fallback
180 set termencoding= " same as 'encoding'
b765a4dc » kana 2007-05-05 Set proper 'termencoding' o... 181 endif
182
183
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 184
185
88559863 » kana 2007-10-10 Rearrange SETTINGS WHICH AR... 186 " Options "{{{2
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 187
71bda839 » kana 2009-06-14 vim: vimrc: Improve color-a... 188 if (1 < &t_Co || has('gui')) && has('syntax')
d8fe4914 » kana 2007-04-05 BASIC SETTINGS: 189 if &term ==# 'rxvt-cygwin-native'
190 set t_Co=256
191 endif
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 192 syntax enable
4231e025 » kana 2008-08-26 vim: vimrc: Don't override ... 193 if !exists('g:colors_name') " Don't override colorscheme on reloading.
5bb5ea73 » kana 2008-08-26 vim: vimrc: Use "desert" as... 194 colorscheme desert
4231e025 » kana 2008-08-26 vim: vimrc: Don't override ... 195 set background=dark
196 endif
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 197 endif
198
199 filetype plugin indent on
200
201
1d8429d4 » kana 2007-10-04 Vim: .vimrc: BASIC SETTINGS: 202 set ambiwidth=double
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 203 set autoindent
204 set backspace=indent,eol,start
205 set backup
206 set backupcopy&
207 set backupdir=.,~/tmp
ec95550e » kana 2007-07-05 BASIC SETTINGS: 208 set backupskip&
2149f670 » kana 2007-07-09 BASIC SETTINGS ('backupskip'): 209 set backupskip+=svn-commit.tmp,svn-commit.[0-9]*.tmp
5e26906f » kana 2007-02-15 Set cinoptions. 210 set cinoptions=:0,t0,(0,W1s
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 211 set directory=.,~/tmp
212 set noequalalways
086e70b4 » kana 2007-09-21 BASIC SETTINGS: 213 set formatoptions=tcroqnlM1
e50ead64 » kana 2007-11-22 Basic / Options: 214 set formatlistpat&
7612ae3c » kana 2007-12-25 vim/dot.vimrc: 215 let &formatlistpat .= '\|^\s*[*+-]\s*'
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 216 set history=100
217 set hlsearch
ade507da » kana 2008-01-21 vim/dot.vimrc: 218 nohlsearch " To avoid (re)highlighting the last search pattern
219 " whenever $MYVIMRC is (re)loaded.
03a19ef8 » kana 2009-04-26 vim: vimrc: Improve on 'gre... 220 " set grepprg=... " See s:toggle_grepprg().
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 221 set incsearch
780ddd3f » kana 2007-10-25 Basic / Options: 222 set laststatus=2 " always show status lines.
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 223 set mouse=
224 set ruler
225 set showcmd
226 set showmode
4b1c3791 » kana 2009-09-26 vim: vimrc: Reduce 'updatet... 227 set updatetime=4000
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 228 set title
eafef300 » kana 2007-09-10 dot.vimrc: 229 set titlestring=Vim:\ %f\ %h%r%m
ac175a16 » kana 2008-11-08 vim: vimrc: Use arpeggio to... 230 set ttimeoutlen=50 " Reduce annoying delay for key codes, especially ...
4099e419 » kana 2007-09-23 r726@colinux: kana | 2007... 231 set wildmenu
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 232 set viminfo=<50,'10,h,r/a,n~/.viminfo
233
929617dd » kana 2007-10-21 Basic / Options: 234 " default 'statusline' with 'fileencoding'.
235 let &statusline = ''
a251119f » kana 2008-01-14 vim/dot.vimrc: 236 let &statusline .= '%<%f %h%m%r%w'
929617dd » kana 2007-10-21 Basic / Options: 237 let &statusline .= '%='
c3b76d8a » kana 2008-02-21 bash: New alias g=git 238 "" temporary disabled.
741790d7 » kana 2008-02-24 Vim: vimrc: Improve perform... 239 "let &statusline .= '(%{' . s:SID_PREFIX() . 'vcs_branch_name(getcwd())}) '
c7010d9a » kana 2009-01-12 vim: vimrc: Misc. improveme... 240 let &statusline .= '[%{&l:fileencoding == "" ? &encoding : &l:fileencoding}]'
929617dd » kana 2007-10-21 Basic / Options: 241 let &statusline .= ' %-14.(%l,%c%V%) %P'
242
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 243 function! s:my_tabline() "{{{
d87dedaf » kana 2007-11-22 Basic / Options: 244 let s = ''
245
246 for i in range(1, tabpagenr('$'))
08e68c23 » kana 2008-01-23 vim/dot.vimrc: 247 let bufnrs = tabpagebuflist(i)
248 let curbufnr = bufnrs[tabpagewinnr(i) - 1] " first window, first appears
249
55bd4e49 » kana 2008-02-11 vim/dot.vimrc: 250 let no = (i <= 10 ? i-1 : '#') " display 0-origin tabpagenr.
08e68c23 » kana 2008-01-23 vim/dot.vimrc: 251 let mod = len(filter(bufnrs, 'getbufvar(v:val, "&modified")')) ? '+' : ' '
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 252 let title = s:gettabvar(i, 'title')
253 let title = len(title) ? title : fnamemodify(s:gettabvar(i, 'cwd'), ':t')
08e68c23 » kana 2008-01-23 vim/dot.vimrc: 254 let title = len(title) ? title : fnamemodify(bufname(curbufnr),':t')
255 let title = len(title) ? title : '[No Name]'
d87dedaf » kana 2007-11-22 Basic / Options: 256
257 let s .= '%'.i.'T'
258 let s .= '%#' . (i == tabpagenr() ? 'TabLineSel' : 'TabLine') . '#'
73ac2940 » kana 2008-01-22 vim/dot.vimrc: 259 let s .= no
260 let s .= mod
261 let s .= title
d87dedaf » kana 2007-11-22 Basic / Options: 262 let s .= '%#TabLineFill#'
263 let s .= ' '
264 endfor
265
266 let s .= '%#TabLineFill#%T'
b28b2a8f » kana 2008-02-24 Vim: vimrc: Leave memo to s... 267 let s .= '%=%#TabLine#'
c365129d » kana 2008-02-25 Vim: vimrc: Revise 'tabline... 268 let s .= '| '
269 let s .= '%999X'
a6745698 » kana 2008-02-24 Vim: vimrc: Show VCS branch... 270 let branch_name = s:vcs_branch_name(getcwd())
c365129d » kana 2008-02-25 Vim: vimrc: Revise 'tabline... 271 let s .= (branch_name != '' ? branch_name : '?')
272 let s .= '%X'
d87dedaf » kana 2007-11-22 Basic / Options: 273 return s
274 endfunction "}}}
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 275 let &tabline = '%!' . s:SID_PREFIX() . 'my_tabline()'
d87dedaf » kana 2007-11-22 Basic / Options: 276
48be31de » kana 2007-07-26 Revise some comments and co... 277 " To automatically detect the width and the height of the terminal,
195a83f9 » kana 2007-02-16 Fix the layout. 278 " the followings must not be set.
279 "
0b630120 » kana 2007-02-16 Add text object for C funct... 280 " set columns=80
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 281 " set lines=25
282
283
50c990ef » kana 2008-06-29 vim: vimrc: Fix coding styl... 284 let mapleader = ','
285 let maplocalleader = '.'
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 286
287
112db63e » kana 2008-10-24 vim: vimrc: Refactor - layo... 288
289
290 " Misc. "{{{2
291
0d470d94 » kana 2007-10-10 Basic / Options: 292 " Use this group for any autocmd defined in this file.
293 augroup MyAutoCmd
294 autocmd!
295 augroup END
296
297
fc408a4d » kana 2009-06-14 vim: vimrc: Use altercmd in... 298 call altercmd#load()
ac175a16 » kana 2008-11-08 vim: vimrc: Use arpeggio to... 299 call arpeggio#load()
82a12043 » kana 2008-04-26 vim: idwintab: New 300 call idwintab#load()
301
302
b3e2b9b0 » kana 2007-01-15 Import vim config from the ... 303
304
195a83f9 » kana 2007-02-16 Fix the layout. 305
306
307
308
eb470fc7 » kana 2008-05-04 vim: vimrc: New section "Sy... 309 " Syntax "{{{1
310 " User-defined commands to extend Vim script syntax.
c457fd1c » kana 2008-05-10 vim: vimrc: Syntax: Place c... 311 " - Place commands which are for interactive use at the bottom.
eb470fc7 » kana 2008-05-04 vim: vimrc: New section "Sy... 312 " FIXME: syntax highlighting
313 " FIXME: completion
0a89677f » kana 2008-05-04 vim: vimrc: Syntax: Add s:s... 314 " Stuffs "{{{2
315
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 316 let s:FALSE = 0
317 let s:TRUE = !s:FALSE
318
319
0a89677f » kana 2008-05-04 vim: vimrc: Syntax: Add s:s... 320 function! s:separate_list(list, regexp)
321 let i = 0
322 while i < len(a:list) && a:list[i] =~# a:regexp
323 let i += 1
324 endwhile
325 return [(0 < i ? a:list[:i-1] : []), a:list[(i):]]
326 endfunction
327
328
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 329 function! s:contains_p(list, regexp)
330 for item in a:list
331 if item =~# a:regexp
332 return s:TRUE
333 endif
334 endfor
335 return s:FALSE
336 endfunction
337
338
0a89677f » kana 2008-05-04 vim: vimrc: Syntax: Add s:s... 339
340
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 341 " :map wrappers "{{{2
342 " Allmap - :map in all modes "{{{3
e6b322e9 » kana 2008-05-05 vim: vimrc: Syntax: Place :... 343
344 command! -nargs=+ Allmap
345 \ execute 'map'
346 \ | execute 'map!'
347
348 command! -nargs=+ Allnoremap
349 \ execute 'noremap'
350 \ | execute 'noremap!'
351
352 command! -nargs=+ Allunmap
78e0cf35 » kana 2008-09-09 vim: vimrc: Remove :silent!... 353 \ execute 'unmap'
354 \ | execute 'unmap!'
e6b322e9 » kana 2008-05-05 vim: vimrc: Syntax: Place :... 355
356
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 357 " Cmap - wrapper of :map to easily execute commands "{{{3
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 358 "
359 " :Cmap {lhs} {script}
360 " Other variants:
1ac8b1b5 » kana 2008-09-09 vim: vimrc: Add "all" and "... 361 " Cmap!, Ccmap, Cimap, Clmap, Cnmap, Comap, Csmap, Cvmap, Cxmap,
362 " Callmap, Cobjmap.
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 363 "
364 " {lhs}
4b200539 » kana 2008-05-05 vim: vimrc: Update :Cmap / ... 365 " Same as :map. As additional :map-arguments, and are
366 " available.
367 "
368 "
369 " Whenever {script} are executed, count effect (e.g. typing "3:" will be
370 " treated as ":.,+2") is ignored unless is specified.
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 371 "
4b200539 » kana 2008-05-05 vim: vimrc: Update :Cmap / ... 372 "
373 " If is specified, {script} is just inserted in the
374 " Command-line, but it's not executed.
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 375 "
376 " {script}
377 " A script which is executed whenever key sequence {lhs} are typed.
378
025451f2 » kana 2008-09-09 vim: vimrc: Remove :Fcomman... 379 command! -bang -nargs=* Cmap call s:cmd_Cmap('', '', [])
380 command! -nargs=* Ccmap call s:cmd_Cmap('c', '', [])
381 command! -nargs=* Cimap call s:cmd_Cmap('i', '', [])
382 command! -nargs=* Clmap call s:cmd_Cmap('l', '', [])
383 command! -nargs=* Cnmap call s:cmd_Cmap('n', '', [])
384 command! -nargs=* Comap call s:cmd_Cmap('o', '', [])
385 command! -nargs=* Csmap call s:cmd_Cmap('s', '', [])
386 command! -nargs=* Cvmap call s:cmd_Cmap('v', '', [])
387 command! -nargs=* Cxmap call s:cmd_Cmap('x', '', [])
1ac8b1b5 » kana 2008-09-09 vim: vimrc: Add "all" and "... 388 command! -nargs=* Callmap call s:cmd_Cmap('All', '', [])
389 command! -nargs=* Cobjmap call s:cmd_Cmap('Obj', '', [])
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 390 function! s:cmd_Cmap(prefix, suffix, args)
391 " FIXME: This parsing may not be compatible with the original one.
392 let [options, rest] = s:separate_list(a:args,
4b200539 » kana 2008-05-05 vim: vimrc: Update :Cmap / ... 393 \ '^\c<\(buffer\|expr\|script\|silent\|special\|unique\|count\|noexec\)>$')
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 394 if len(rest) < 2
395 throw 'Insufficient number of arguments: ' . string(rest)
396 endif
397 let lhs = rest[0]
398 let script = rest[1:]
4b200539 » kana 2008-05-05 vim: vimrc: Update :Cmap / ... 399 let count_p = s:contains_p(options, '^\c$')
400 let noexec_p = s:contains_p(options, '^\c$')
401 call filter(options, 'v:val !~# ''^\c<\(count\|noexec\)>$''')
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 402
403 execute a:prefix.'noremap'.a:suffix join(options) lhs
20ff02ca » kana 2008-05-10 vim: vimrc: Fix lines with ... 404 \ ':'.(count_p ? '' : '') . join(script) . (noexec_p ? '' : '')
2d161c4d » kana 2008-05-05 vim: vimrc: Syntax: Add :Cm... 405 endfunction
406
407
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 408 " Fmap - wrapper of :map to easily call a function "{{{3
d469a3fe » kana 2008-09-09 vim: vimrc: Refactor - the ... 409 "
410 " :Fmap {lhs} {expression}
411 " Other variants:
1ac8b1b5 » kana 2008-09-09 vim: vimrc: Add "all" and "... 412 " Fmap!, Fcmap, Fimap, Flmap, Fnmap, Fomap, Fsmap, Fvmap, Fxmap,
413 " Fallmap, Fobjmap.
d469a3fe » kana 2008-09-09 vim: vimrc: Refactor - the ... 414 "
415 " {lhs}
416 " Same as :map.
417 "
418 " {expression}
419 " An expression to call a function (without :call). This expression is
420 " executed whenever key sequence {lhs} are typed.
421
422 command! -bang -nargs=* Fmap call s:cmd_Fmap('', '', [])
423 command! -nargs=* Fcmap call s:cmd_Fmap('c', '', [])
424 command! -nargs=* Fimap call s:cmd_Fmap('i', '', [])
425 command! -nargs=* Flmap call s:cmd_Fmap('l', '', [])
426 command! -nargs=* Fnmap call s:cmd_Fmap('n', '', [])
427 command! -nargs=* Fomap call s:cmd_Fmap('o', '', [])
428 command! -nargs=* Fsmap call s:cmd_Fmap('s', '', [])
429 command! -nargs=* Fvmap call s:cmd_Fmap('v', '', [])
430 command! -nargs=* Fxmap call s:cmd_Fmap('x', '', [])
1ac8b1b5 » kana 2008-09-09 vim: vimrc: Add "all" and "... 431 command! -nargs=* Fallmap call s:cmd_Fmap('All', '', [])
432 command! -nargs=* Fobjmap call s:cmd_Fmap('Obj', '', [])
d469a3fe » kana 2008-09-09 vim: vimrc: Refactor - the ... 433 function! s:cmd_Fmap(prefix, suffix, args)
434 " FIXME: This parsing may not be compatible with the original one.
435 let [options, rest] = s:separate_list(a:args,
436 \ '^\c<\(buffer\|expr\|script\|silent\|special\|unique\)>$')
437 if len(rest) < 2
438 throw 'Insufficient number of arguments: ' . string(rest)
439 endif
440 let lhs = rest[0]
441 let rhs = rest[1:]
442
443 execute a:prefix.'noremap'.a:suffix join(options) lhs
444 \ ':call' join(rhs) ''
445 endfunction
446
447
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 448 " Objmap - :map for text objects "{{{3
7bbd97b0 » kana 2008-10-24 vim: vimrc: Add :Operatorma... 449 "
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 450 " Keys for text objects should be mapped in Visual mode and Operator-pending
451 " mode. The following commands are just wrappers to avoid DRY violation.
7bbd97b0 » kana 2008-10-24 vim: vimrc: Add :Operatorma... 452
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 453 command! -nargs=+ Objmap
454 \ execute 'omap'
455 \ | execute 'vmap'
7bbd97b0 » kana 2008-10-24 vim: vimrc: Add :Operatorma... 456
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 457 command! -nargs=+ Objnoremap
458 \ execute 'onoremap'
459 \ | execute 'vnoremap'
7bbd97b0 » kana 2008-10-24 vim: vimrc: Add :Operatorma... 460
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 461 command! -nargs=+ Objunmap
462 \ execute 'ounmap'
463 \ | execute 'vunmap'
7bbd97b0 » kana 2008-10-24 vim: vimrc: Add :Operatorma... 464
465
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 466 " Operatormap - :map for oeprators "{{{3
d469a3fe » kana 2008-09-09 vim: vimrc: Refactor - the ... 467 "
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 468 " Keys for operators should be mapped in Normal mode and Visual mode. The
469 " following commands are just wrappers to avoid DRY violation.
0b247147 » kana 2009-05-22 vimrc: Fix use and notes on... 470 "
471 " FIXME: How about mapping to g@ in Operator-pending mode
472 " to use {operator}{operator} pattern?
d469a3fe » kana 2008-09-09 vim: vimrc: Refactor - the ... 473
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 474 command! -nargs=+ Operatormap
475 \ execute 'nmap'
476 \ | execute 'vmap'
477
478 command! -nargs=+ Operatornoremap
479 \ execute 'nnoremap'
480 \ | execute 'vnoremap'
481
482 command! -nargs=+ Operatorunmap
483 \ execute 'nunmap'
484 \ | execute 'vunmap'
d469a3fe » kana 2008-09-09 vim: vimrc: Refactor - the ... 485
486
487
488
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 489 " CD - alternative :cd with more user-friendly completion "{{{2
490
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 491 command! -complete=customlist,s:complete_cdpath -nargs=+ CD TabpageCD
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 492 function! s:complete_cdpath(arglead, cmdline, cursorpos)
493 return split(globpath(&cdpath,
494 \ join(split(a:cmdline, '\s', s:TRUE)[1:], ' ') . '*/'),
495 \ "\n")
496 endfunction
497
fc408a4d » kana 2009-06-14 vim: vimrc: Use altercmd in... 498 AlterCommand cd CD
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 499
500
501
502
7f80f75b » kana 2008-05-08 vim: vimrc: Syntax: Add :He... 503 " Hecho, Hechon, Hechomsg - various :echo with highlight specification "{{{2
504
025451f2 » kana 2008-09-09 vim: vimrc: Remove :Fcomman... 505 command! -bar -nargs=+ Hecho call s:cmd_Hecho('echo', [])
506 command! -bar -nargs=+ Hechon call s:cmd_Hecho('echon', [])
507 command! -bar -nargs=+ Hechomsg call s:cmd_Hecho('echomsg', [])
7f80f75b » kana 2008-05-08 vim: vimrc: Syntax: Add :He... 508 function! s:cmd_Hecho(echo_command, args)
509 let highlight_name = a:args[0]
510 let messages = a:args[1:]
511
512 execute 'echohl' highlight_name
513 execute a:echo_command join(messages)
514 echohl None
515 endfunction
516
517
518
519
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 520 " KeyboardLayout - declare differences of logical and physical layouts "{{{2
521 "
522 " :KeyboardLayout {physical-key} {logical-key}
523 "
524 " Declare that whenever Vim gets a character {logical-key}, the
525 " corresponding physical key is {physical-key}. This declaration is useful
526 " to define a mapping based on physical keyboard layout.
527 "
528 " Example: Map the physical key {X} to {rhs}:
529 " noremap (physical-key-{X}) {rhs}
530
531 command! -nargs=+ KeyboardLayout call s:cmd_KeyboardLayout()
532 function! s:cmd_KeyboardLayout(physical_key, logical_key)
533 let indirect_key = '(physical-key-' . a:physical_key . ')'
534 execute 'Allmap' a:logical_key indirect_key
535 execute 'Allnoremap' indirect_key a:logical_key
536 endfunction
537
538
539
540
c369f45c » kana 2008-05-05 vim: vimrc: Syntax: Place :... 541 " OnFileType - wrapper of :autocmd FileType for compound 'filetype' "{{{2
46d31b9f » kana 2008-02-15 vim/dot.vimrc: 542 "
661b53ea » kana 2008-02-15 vim/dot.vimrc: 543 " To write a bit of customization per 'filetype', an easy way is to write some
3cb0ddfc » kana 2008-03-07 vim: vimrc: OnFileType: Upd... 544 " ":autocmd"s like "autocmd FileType c". But it doesn't match to compound
661b53ea » kana 2008-02-15 vim/dot.vimrc: 545 " 'filetype' such as "c.doxygen". So the pattern should be
3cb0ddfc » kana 2008-03-07 vim: vimrc: OnFileType: Upd... 546 " "{c,*.c,c.*,*.c.*}", but it's hard to read and to write. :OnFileType is
547 " a wrapper for ":autocmd FileType" to support to write such customization.
661b53ea » kana 2008-02-15 vim/dot.vimrc: 548 "
549 " Note: If a:filetype contains one of the following characters:
550 " * ? { } [ ]
551 " a:filetype will be treated as-is to write customization for compound
552 " 'filetype' with :OnFileType.
3cb0ddfc » kana 2008-03-07 vim: vimrc: OnFileType: Upd... 553 "
661b53ea » kana 2008-02-15 vim/dot.vimrc: 554 " Note: If a:filetype contains one or more ",", :OnFileType will be called for
555 " each ","-separated filetype in a:filetype.
46d31b9f » kana 2008-02-15 vim/dot.vimrc: 556 "
557 " FIXME: syntax highlighting and completion.
3cb0ddfc » kana 2008-03-07 vim: vimrc: OnFileType: Upd... 558 "
559 " BUGS: This doesn't work for most cases because of the limit of the maximum
560 " number of arguments to a function.
46d31b9f » kana 2008-02-15 vim/dot.vimrc: 561
f6ad0c11 » kana 2008-06-29 vim: vimrc, ku: Fix unneces... 562 command! -nargs=+ OnFileType call s:cmd_OnFileType()
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 563 function! s:cmd_OnFileType(group, filetype, ...)
46d31b9f » kana 2008-02-15 vim/dot.vimrc: 564 let group = (a:group == '-' ? '' : a:group)
661b53ea » kana 2008-02-15 vim/dot.vimrc: 565 let commands = join(a:000)
566
567 let SPECIAL_CHARS = '[*?{}[\]]'
568 if a:filetype !~ SPECIAL_CHARS && a:filetype =~ ','
569 for ft in split(a:filetype, ',')
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 570 call s:cmd_OnFileType(group, ft, commands)
661b53ea » kana 2008-02-15 vim/dot.vimrc: 571 endfor
572 return
573 endif
574 let filetype = (a:filetype =~ SPECIAL_CHARS
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 575 \ ? a:filetype
576 \ : substitute('{x,x.*,*.x,*.x.*}', 'x', a:filetype, 'g'))
46d31b9f » kana 2008-02-15 vim/dot.vimrc: 577
578 execute 'autocmd' group 'FileType' filetype commands
579 endfunction
580
581
582
583
c457fd1c » kana 2008-05-10 vim: vimrc: Syntax: Place c... 584 " Qexecute - variant of :execute with some extensions "{{{2
585 "
586 " Like :execute but all arguments are treated as single string like .
587 " As an extension, "[count]" will be expanded to the currently given count.
588
a74ad62d » kana 2008-08-24 vim: vimrc: New coding rule... 589 command! -complete=command -nargs=* Qexecute call s:cmd_Qexecute()
c457fd1c » kana 2008-05-10 vim: vimrc: Syntax: Place c... 590 function! s:cmd_Qexecute(script)
591 execute substitute(a:script, '\[count\]', s:count(), 'g')
592 endfunction
593
594 function! s:count(...)
595 if v:count == v:count1 " is count given?
596 return v:count
597 else " count isn't given. (the default '' is useful for special value)
598 return a:0 == 0 ? '' : a:1
599 endif
600 endfunction
601
602
603
604
605 " Source - wrapper of :source with echo. "{{{2
606 " FIXME: better name.
607
608 command! -bar -nargs=1 Source
609 \ echo 'Sourcing ...' expand()
610 \ | source
611
612
613
614
9d585281 » kana 2008-10-25 vim: vimrc: Refine on :Susp... 615 " SuspendWithAutomticCD "{{{2
616 " Assumption: Use GNU screen.
617 " Assumption: There is a window with the title "another".
618
a58c2fb9 » kana 2008-10-25 vim: vimrc: Refactor - rena... 619 if !exists('s:GNU_SCREEN_AVAILABLE_P')
9d585281 » kana 2008-10-25 vim: vimrc: Refine on :Susp... 620 " Check the existence of $WINDOW to avoid using GNU screen in Vim on
621 " a remote machine (for example, "screen -t remote ssh example.com").
a58c2fb9 » kana 2008-10-25 vim: vimrc: Refactor - rena... 622 let s:GNU_SCREEN_AVAILABLE_P = len($WINDOW) != 0
9d585281 » kana 2008-10-25 vim: vimrc: Refine on :Susp... 623 endif
624
625 command! -bar -nargs=0 SuspendWithAutomticCD
626 \ call s:cmd_SuspendWithAutomticCD()
627 function! s:cmd_SuspendWithAutomticCD()
a58c2fb9 » kana 2008-10-25 vim: vimrc: Refactor - rena... 628 if s:GNU_SCREEN_AVAILABLE_P
9d585281 » kana 2008-10-25 vim: vimrc: Refine on :Susp... 629 " \015 =
630 " To avoid adding the cd script into the command-line history,
631 " there are extra leading whitespaces in the cd script.
632 silent execute '!screen -X eval'
633 \ '''select another'''
634 \ '''stuff " cd \"'.getcwd().'\" \#\#,vim-auto-cd\015"'''
635 redraw!
a58c2fb9 » kana 2008-10-25 vim: vimrc: Refactor - rena... 636 let s:GNU_SCREEN_AVAILABLE_P = (v:shell_error == 0)
9d585281 » kana 2008-10-25 vim: vimrc: Refine on :Susp... 637 endif
638
a58c2fb9 » kana 2008-10-25 vim: vimrc: Refactor - rena... 639 if !s:GNU_SCREEN_AVAILABLE_P
9d585281 » kana 2008-10-25 vim: vimrc: Refine on :Susp... 640 suspend
641 endif
642 endfunction
643
644
645
646
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 647 " TabpageCD - wrapper of :cd to keep cwd for each tabpage "{{{2
c369f45c » kana 2008-05-05 vim: vimrc: Syntax: Place :... 648
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 649 command! -nargs=? TabpageCD
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 650 \ execute 'cd' fnameescape()
651 \ | let t:cwd = getcwd()
c369f45c » kana 2008-05-05 vim: vimrc: Syntax: Place :... 652
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 653 autocmd MyAutoCmd TabEnter *
654 \ if !exists('t:cwd')
655 \ | let t:cwd = getcwd()
656 \ | endif
657 \ | execute 'cd' fnameescape(t:cwd)
c369f45c » kana 2008-05-05 vim: vimrc: Syntax: Place :... 658
659
660
661
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 662 " TabpageTitle - name the current tabpage "{{{2
c369f45c » kana 2008-05-05 vim: vimrc: Syntax: Place :... 663
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 664 command! -bar -nargs=* TabpageTitle
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 665 \ if == ''
666 \ | let t:title = input("Set tabpage's title to: ",'')
667 \ | else
668 \ | let t:title =
669 \ | endif
c369f45c » kana 2008-05-05 vim: vimrc: Syntax: Place :... 670
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 671
672
673
2ab6e640 » kana 2008-05-10 vim: vimrc: Syntax: Place :... 674 " UsualDays - set up the layout of my usual days "{{{2
675
676 command! -bar -nargs=0 UsualDays call s:cmd_UsualDays()
677 function! s:cmd_UsualDays()
678 normal! 'T
679 execute 'CD' fnamemodify(expand('%'), ':p:h')
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 680 TabpageTitle meta
2ab6e640 » kana 2008-05-10 vim: vimrc: Syntax: Place :... 681
682 tabnew
683 normal! 'V
684 execute 'CD' fnamemodify(expand('%'), ':p:h:h')
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 685 TabpageTitle config
2ab6e640 » kana 2008-05-10 vim: vimrc: Syntax: Place :... 686 endfunction
687
688
689
690
0ad2eb2d » kana 2008-10-24 vim: vimrc: Refactor - layo... 691 " Utf8 and others - :edit with specified 'fileencoding' "{{{2
692
693 command! -bang -bar -complete=file -nargs=? Cp932
694 \ edit ++enc=cp932
695 command! -bang -bar -complete=file -nargs=? Eucjp
696 \ edit ++enc=euc-jp
697 command! -bang -bar -complete=file -nargs=? Iso2022jp
698 \ edit ++enc=iso-2022-jp
699 command! -bang -bar -complete=file -nargs=? Utf8
700 \ edit ++enc=utf-8
701
702 command! -bang -bar -complete=file -nargs=? Jis Iso2022jp
703 command! -bang -bar -complete=file -nargs=? Sjis Cp932
704
705
706
707
4ec72cc8 » kana 2008-05-10 vim: vimrc: Syntax: Place C... 708
709
710
711
712 " Utilities "{{{1
27a67454 » kana 2009-09-14 vim: vimrc: Add :Grep and :... 713 " :grep wrappers "{{{2
714 "
715 " To edit {pattern} easily via Command-line mode history,
716 " define :Grep as follows:
717 "
718 " :Grep {file} ... {pattern} ==> :grep /{pattern}/ {file} ...
719 "
720 " :Lgrep is a :lgrep wrapper like :Grep.
721
c6b8fc16 » kana 2009-11-08 vim: vimrc: Fix a typo 722 command! -bar -complete=file -nargs=+ Grep call s:grep('grep', [])
8208536f » kana 2009-09-24 vim: vimrc: Refactor :Grep/... 723 command! -bar -complete=file -nargs=+ Lgrep call s:grep('lgrep', [])
27a67454 » kana 2009-09-14 vim: vimrc: Add :Grep and :... 724
8208536f » kana 2009-09-24 vim: vimrc: Refactor :Grep/... 725 function! s:grep(command, args)
4150041a » kana 2009-11-08 vim: vimrc: Fix wrong expre... 726 execute a:command '/'.a:args[-1].'/' join(a:args[:-2])
8208536f » kana 2009-09-24 vim: vimrc: Refactor :Grep/... 727 endfunction
27a67454 » kana 2009-09-14 vim: vimrc: Add :Grep and :... 728
729
730
731
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 732 " Help-related stuffs "{{{2
733
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 734 function! s:helpbufwinnr()
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 735 let wn = 1
736 while wn <= winnr('$')
737 let bn = winbufnr(wn)
738 if getbufvar(bn, '&buftype') == 'help'
739 return [bn, wn]
740 endif
741 let wn = wn + 1
742 endwhile
743 return [-1, 0]
744 endfunction
745
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 746 function! s:close_help_window()
747 let [help_bufnr, help_winnr] = s:helpbufwinnr()
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 748 if help_bufnr == -1
749 return
750 endif
751
752 let current_winnr = winnr()
753 execute help_winnr 'wincmd w'
754 execute 'wincmd c'
755 if current_winnr < help_winnr
756 execute current_winnr 'wincmd w'
757 elseif help_winnr < current_winnr
758 execute (current_winnr-1) 'wincmd w'
759 else
760 " NOP
761 endif
762 endfunction
763
764
765
766
767 " High-level key sequences "{{{2
768
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 769 function! s:keys_to_complete()
06ae6e4d » kana 2008-01-13 vim/dot.vimrc: 770 if &l:filetype ==# 'vim'
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 771 return "\\"
06ae6e4d » kana 2008-01-13 vim/dot.vimrc: 772 elseif strlen(&l:omnifunc)
773 return "\\"
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 774 else
775 return "\"
776 endif
777 endfunction
778
779
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 780 function! s:keys_to_escape_command_line_mode_if_empty(key)
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 781 if getcmdline() == ''
782 return "\"
783 else
784 return a:key
785 end
786 endfunction
787
788
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 789 function! s:keys_to_insert_one_character()
7f80f75b » kana 2008-05-08 vim: vimrc: Syntax: Add :He... 790 Hecho ModeMsg '-- INSERT (one char) --'
8d4d8c22 » kana 2007-10-17 Key Mappings / The <Space>: 791 return nr2char(getchar()) . "\"
792 endfunction
793
794
3865d6e5 » kana 2009-09-26 vim: vimrc: Revise "gc" to ... 795 function! s:keys_to_select_the_last_changed_text()
796 " It is not possible to determine whether the last operation to change text
797 " is linewise or not, so guess the wise of the last operation from the range
798 " of '[ and '], like wise of a register content set by setreg() without
799 " {option}.
800
801 let col_begin = col("'[")
802 let col_end = col("']")
803 let length_end = len(getline("']"))
804
805 let maybe_linewise_p = (col_begin == 1
806 \ && (col_end == length_end
807 \ || (length_end == 0 && col_end == 1)))
808 return '`[' . (maybe_linewise_p ? 'V' : 'v') . '`]'
809 endfunction
810
811
155b2296 » kana 2009-09-26 vim: vimrc: Refactor - sort... 812 function! s:keys_to_stop_insert_mode_completion()
813 if pumvisible()
814 return "\"
815 else
816 return "\\"
817 endif
818 endfunction
819
820
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 821
822
823 " Jump sections "{{{2
824
825 " for normal mode. a:pattern is '/regexp' or '?regexp'.
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 826 function! s:jump_section_n(pattern)
ead9ab7b » kana 2008-10-24 vim: vimrc: Refactor - s:ju... 827 let pattern = a:pattern[1:]
828 let forward_p = a:pattern[0] == '/'
829 let flags = forward_p ? 'W' : 'Wb'
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 830
831 mark '
832 let i = 0
833 while i < v:count1
834 if search(pattern, flags) == 0
ead9ab7b » kana 2008-10-24 vim: vimrc: Refactor - s:ju... 835 if forward_p
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 836 normal! G
ead9ab7b » kana 2008-10-24 vim: vimrc: Refactor - s:ju... 837 else
838 normal! gg
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 839 endif
840 break
841 endif
842 let i = i + 1
843 endwhile
844 endfunction
845
846
847 " for visual mode. a:motion is '[[', '[]', ']]' or ']['.
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 848 function! s:jump_section_v(motion)
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 849 execute 'normal!' "gv\"
850 execute 'normal' v:count1 . a:motion
851 let line = line('.')
852 let col = col('.')
853
854 normal! gv
855 call cursor(line, col)
856 endfunction
857
858
859 " for operator-pending mode. a:motion is '[[', '[]', ']]' or ']['.
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 860 function! s:jump_section_o(motion)
60783901 » kana 2007-10-10 Revise UTILITY FUNCTIONS & ... 861 execute 'normal' v:count1 . a:motion
862 endfunction
863
864
865
866
468f5aea » kana 2009-04-26 vim: vimrc: Refactor - sort... 867 " Sum numbers "{{{2
868
869 command! -bang -bar -nargs=* -range Sum
870 \ ,call s:cmd_Sum(0, )
871 function! s:cmd_Sum(banged_p, ...) range
872 let field_number = (1 <= a:0 ? a:1 : 1)
873 let field_separator = (2 <= a:0 ? a:2 : 0)
874 execute a:firstline ',' a:lastline '!awk'
875 \ (field_separator is 0 ? '' : '-F'.field_separator)
876 \ "'"
877 \ 'BEGIN {x = 0}'
878 \ '{x = x + $'.field_number '}'
879 \ '{if (\!' a:banged_p ') print $0}'
880 \ 'END {print x}'
881 \ "'"
882 endfunction
883
884
885
886
293c926e » kana 2008-10-24 vim: vimrc: Refactor - layo... 887 " Toggle options "{{{2
888
889 function! s:toggle_bell()
890 if &visualbell
891 set novisualbell t_vb&
892 echo 'bell on'
893 else
894 set visualbell t_vb=
895 echo 'bell off'
896 endif
897 endfunction
898
03a19ef8 » kana 2009-04-26 vim: vimrc: Improve on 'gre... 899 function! s:toggle_grepprg()
900 " Toggle, more precisely, rotate the value of 'grepprg'.
901 " If 'grepprg' has a value not listed in VALUES,
902 " treat VALUES[0] as the default/fallback value,
903 " and set 'grepprg' to the value.
904 let VALUES = ['git grep -n', 'internal']
905 let i = (index(VALUES, &grepprg) + 1) % len(VALUES)
906
907 let grepprg = &grepprg
908 let &grepprg = VALUES[i]
909 echo "'grepprg' =" &grepprg '(was' grepprg.')'
910 endfunction
911 if !exists('s:loaded_my_vimrc')
912 " Set 'grepprg' to my default value. In this scope, 'grepprg' has Vim's
913 " default value and it differs from what I want to use. So that calling
914 " s:toggle_grepprg() at here does set 'grepprg' to my default value.
915 silent call s:toggle_grepprg()
f2d0f658 » kana 2009-09-14 vim: vimrc: Fix default 'gr... 916
917 if !has('unix')
918 " For non-*nix environments, git is not usually available. To avoid error
919 " and manual toggling, set 'grepprg' to my alternative value.
920 silent call s:toggle_grepprg()
921 endif
03a19ef8 » kana 2009-04-26 vim: vimrc: Improve on 'gre... 922 endif
923
293c926e » kana 2008-10-24 vim: vimrc: Refactor - layo... 924 function! s:toggle_option(option_name)
925 execute 'setlocal' a:option_name.'!'
926 execute 'setlocal' a:option_name.'?'
927 endfunction
928
929
930
931
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 932 " Window-related stuffs "{{{2
933
934 " Are the windows :split'ed and :vsplit'ed?
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 935 function! s:windows_jumbled_p()
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 936 " Calculate the terminal height by some values other than 'lines'.
937 " Don't consider about :vsplit.
938 let calculated_height = &cmdheight
939 let winid = winnr('$')
940 while 0 < winid
941 let calculated_height += 1 " statusline
942 let calculated_height += winheight(winid)
943 let winid = winid - 1
944 endwhile
945 if &laststatus == 0
946 let calculated_height -= 1
947 elseif &laststatus == 1 && winnr('$') == 1
948 let calculated_height -= 1
949 else " &laststatus == 2
950 " nothing to do
951 endif
952
953 " Calculate the terminal width by some values other than 'columns'.
954 " Don't consider about :split.
955 let calculated_width = 0
956 let winid = winnr('$')
957 while 0 < winid
958 let calculated_width += 1 " VertSplit
959 let calculated_width += winwidth(winid)
960 let winid = winid - 1
961 endwhile
962 let calculated_width -= 1
963
964 " If the windows are only :split'ed, &lines == calculated_height.
965 " If the windows are only :vsplit'ed, &columns == calculated_width.
966 " If there is only one window, both pairs are same.
967 " If the windows are :split'ed and :vsplit'ed, both pairs are different.
968 return (&lines != calculated_height) && (&columns != calculated_width)
969 endfunction
970
971
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 972 function! s:move_window_then_equalize_if_necessary(direction)
973 let jumbled_beforep = s:windows_jumbled_p()
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 974 execute 'wincmd' a:direction
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 975 let jumbled_afterp = s:windows_jumbled_p()
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 976
977 if jumbled_beforep || jumbled_afterp
978 wincmd =
979 endif
980 endfunction
981
982
26dcd217 » kana 2008-12-11 vim: vimrc: Normalize a nam... 983 function! s:move_window_into_tabpage(target_tabpagenr)
55d71fe9 » kana 2008-01-12 vim/dot.vimrc: 984 " Move the current window into a:target_tabpagenr.
26dcd217 » kana 2008-12-11 vim: vimrc: Normalize a nam... 985 " If a:target_tabpagenr is 0, move into new tabpage.
55bd4e49 » kana 2008-02-11 vim/dot.vimrc: 986 if a:target_tabpagenr < 0 " ignore invalid number.
987 return
988 endif
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 989 let original_tabnr = tabpagenr()
990 let target_bufnr = bufnr('')
55d71fe9 » kana 2008-01-12 vim/dot.vimrc: 991 let window_view = winsaveview()
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 992
55d71fe9 » kana 2008-01-12 vim/dot.vimrc: 993 if a:target_tabpagenr == 0
994 tabnew
f4ac71ac » kana 2008-02-12 vim/dot.vimrc: 995 tabmove " Move new tabpage at the last.
55d71fe9 » kana 2008-01-12 vim/dot.vimrc: 996 execute target_bufnr 'buffer'
997 let target_tabpagenr = tabpagenr()
998 else
999 execute a:target_tabpagenr 'tabnext'
1000 let target_tabpagenr = a:target_tabpagenr
8bf39459 » kana 2008-12-11 vim: vimrc: Fix not to crea... 1001 " FIXME: be customizable?
1002 execute 'topleft' target_bufnr 'sbuffer'
55d71fe9 » kana 2008-01-12 vim/dot.vimrc: 1003 endif
1004 call winrestview(window_view)
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 1005
1006 execute original_tabnr 'tabnext'
1007 if 1 < winnr('$')
1008 close
1009 else
1010 enew
1011 endif
1012
55d71fe9 » kana 2008-01-12 vim/dot.vimrc: 1013 execute target_tabpagenr 'tabnext'
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 1014 endfunction
1015
1016
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1017 function! s:scroll_other_window(scroll_command)
93c494f8 » kana 2008-08-25 vim: vimrc: Fix the conditi... 1018 if winnr('#') == 0 || winnr('#') == winnr()
1019 " Do nothing if there is not proper previous window.
1020 " Note that winnr('#') sometime returns the number for the current window
1021 " instead of 0. So the latter condition is necessary.
1022 "
1023 " Example session to reproduce the winnr('#') problem:
1024 " tabnew
1025 " wincmd v
1026 " wincmd h
1027 " wincmd l
1028 " wincmd h
1029 " echo winnr('#') " ==> 2
1030 " wincmd l
1031 " wincmd c
1032 " echo winnr('#') " ==> 1 -- although it should be 0.
1f72c0f4 » kana 2008-08-25 vim: vimrc: Change message ... 1033 Hecho WarningMsg 'No window to scroll'
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 1034 else
2904d744 » kana 2008-08-25 vim: vimrc: Use :wincmd in ... 1035 wincmd p
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1036 execute 'normal!' (s:count() . a:scroll_command)
2904d744 » kana 2008-08-25 vim: vimrc: Use :wincmd in ... 1037 wincmd p
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 1038 endif
1039 endfunction
1040
1041
f5cfce1c » kana 2008-09-28 vim: vimrc: Fix options for... 1042 command! -bar -nargs=0 SplitNicely call s:split_nicely()
4e180a22 » kana 2008-09-28 vim: vimrc: Add :SplitNicely 1043 function! s:split_nicely()
1044 if 80*2 * 15/16 <= winwidth(0) " FIXME: threshold customization
1045 vsplit
1046 else
1047 split
1048 endif
1049 endfunction
1050
1051
1b973ca0 » kana 2008-01-12 vim/dot.vimrc: 1052
1053
b6638a87 » kana 2008-02-24 Vim: vimrc: Improve the pla... 1054 " VCS branch name "{{{2
1055 " Returns the name of the current branch of the given directory.
1056 " BUGS: git is only supported.
741790d7 » kana 2008-02-24 Vim: vimrc: Improve perform... 1057 let s:_vcs_branch_name_cache = {} " dir_path = [branch_name, key_file_mtime]
1058
1059
1060 function! s:vcs_branch_name(dir)
1061 let cache_entry = get(s:_vcs_branch_name_cache, a:dir, 0)
1062 if cache_entry is 0
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1063 \ || cache_entry[1] < getftime(s:_vcs_branch_name_key_file(a:dir))
741790d7 » kana 2008-02-24 Vim: vimrc: Improve perform... 1064 unlet cache_entry
1065 let cache_entry = s:_vcs_branch_name(a:dir)
1066 let s:_vcs_branch_name_cache[a:dir] = cache_entry
1067 endif
1068
1069 return cache_entry[0]
1070 endfunction
1071
1072
1073 function! s:_vcs_branch_name_key_file(dir)
1074 return a:dir . '/.git/HEAD'
1075 endfunction
1076
1077
1078 function! s:_vcs_branch_name(dir)
1079 let head_file = s:_vcs_branch_name_key_file(a:dir)
1080 let branch_name = ''
b6638a87 » kana 2008-02-24 Vim: vimrc: Improve the pla... 1081
1082 if filereadable(head_file)
1083 let ref_info = s:first_line(head_file)
1084 if ref_info =~ '^\x\{40}$'
e1fbc681 » kana 2008-02-25 Vim: vimrc: Fix the style o... 1085 let remote_refs_dir = a:dir . '/.git/refs/remotes/'
1086 let remote_branches = split(glob(remote_refs_dir . '**'), "\n")
b6638a87 » kana 2008-02-24 Vim: vimrc: Improve the pla... 1087 call filter(remote_branches, 's:first_line(v:val) ==# ref_info')
1088 if 1 <= len(remote_branches)
e1fbc681 » kana 2008-02-25 Vim: vimrc: Fix the style o... 1089 let branch_name = 'remote: '. remote_branches[0][len(remote_refs_dir):]
b6638a87 » kana 2008-02-24 Vim: vimrc: Improve the pla... 1090 endif
1091 else
1092 let branch_name = matchlist(ref_info, '^ref: refs/heads/\(\S\+\)$')[1]
1093 if branch_name == ''
1094 let branch_name = ref_info
1095 endif
1096 endif
1097 endif
1098
741790d7 » kana 2008-02-24 Vim: vimrc: Improve perform... 1099 return [branch_name, getftime(head_file)]
b6638a87 » kana 2008-02-24 Vim: vimrc: Improve the pla... 1100 endfunction
1101
1102
1103
1104
6828f990 » kana 2008-10-27 vim: vimrc: Add s:count_sum... 1105 function! s:count_sum_of_fields() "{{{2
1106 '<,'>!awk 'BEGIN{c=0} {c+=$1} END{print c}'
1107 let _ = getline('.')
1108 undo
1109 '>put =_
1110 endfunction
1111
1112
1113
1114
293c926e » kana 2008-10-24 vim: vimrc: Refactor - layo... 1115 function! s:extend_highlight(target_group, original_group, new_settings) "{{{2
49f2d127 » kana 2007-01-21 Add utility function s:Exte... 1116 redir => resp
1117 silent execute 'highlight' a:original_group
1118 redir END
1119 if resp =~# 'xxx cleared'
1120 let original_settings = ''
1121 elseif resp =~# 'xxx links to'
414cc3df » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1122 return s:extend_highlight(
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1123 \ a:target_group,
1124 \ substitute(resp, '\_.*xxx links to\s\+\(\S\+\)', '\1', ''),
1125 \ a:new_settings
1126 \ )
49f2d127 » kana 2007-01-21 Add utility function s:Exte... 1127 else " xxx {key}={arg} ...
b244da18 » kana 2007-01-23 s:ExtendHighlight: Fix some... 1128 let t = substitute(resp,'\_.*xxx\(\(\_s\+[^= \t]\+=[^= \t]\+\)*\)','\1','')
1129 let original_settings = substitute(t, '\_s\+', ' ', 'g')
49f2d127 » kana 2007-01-21 Add utility function s:Exte... 1130 endif
1131
b244da18 » kana 2007-01-23 s:ExtendHighlight: Fix some... 1132 silent execute 'highlight' a:target_group 'NONE'
eaf21d90 » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1133 \ '|' 'highlight' a:target_group original_settings
1134 \ '|' 'highlight' a:target_group a:new_settings
49f2d127 » kana 2007-01-21 Add utility function s:Exte... 1135 endfunction
1136
1137
293c926e » kana 2008-10-24 vim: vimrc: Refactor - layo... 1138
1139
1140 function! s:first_line(file) "{{{2
1141 let lines = readfile(a:file, '', 1)
1142 return 1 <= len(lines) ? lines[0] : ''
1143 endfunction
1144
1145
1146
1147
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 1148 function! s:gettabvar(tabpagenr, varname) "{{{2
293c926e » kana 2008-10-24 vim: vimrc: Refactor - layo... 1149 " Wrapper for non standard (my own) built-in function gettabvar().
aebdf5f3 » kana 2008-12-11 vim: vimrc: Don't use "tab"... 1150 return exists('*gettabvar') ? gettabvar(a:tabpagenr, a:varname) : ''
293c926e » kana 2008-10-24 vim: vimrc: Refactor - layo... 1151 endfunction
1152
1153
1154
1155
1156 function! s:join_here(...) "{{{2
1157 " like join (J), but move the next line into the cursor position.
1158
ebab41d2 » kana 2007-10-04 Vim: .vimrc: 1159 let adjust_spacesp = a:0 ? a:1 : 1
88ff7005 » kana 2007-09-21 s:JoinHere: 1160 let pos = getpos('.')
ebab41d2 » kana 2007-10-04 Vim: .vimrc: 1161 let r = @"
88ff7005 » kana 2007-09-21 s:JoinHere: 1162
6b7b0c04 » kana 2007-12-26 vim/dot.vimrc: 1163 if line('.') == line('$')
7f80f75b » kana 2008-05-08 vim: vimrc: Syntax: Add :He... 1164 Hecho ErrorMsg 'Unable to join at the bottom line.'
6b7b0c04 » kana 2007-12-26 vim/dot.vimrc: 1165 return
1166 endif
1167
ebab41d2 » kana 2007-10-04 Vim: .vimrc: 1168 if adjust_spacesp " adjust spaces between texts being joined as same as J.
1169 normal! D
1170 let l = @"
88ff7005 » kana 2007-09-21 s:JoinHere: 1171
ebab41d2 » kana 2007-10-04 Vim: .vimrc: 1172 normal! J
1173
1174 call append(line('.'), '')
1175 call setreg('"', l, 'c')
1176 normal! jpkJ
1177 else " don't adjust spaces like gJ.
1178 call setreg('"', getline(line('.') + 1), 'c')
1179 normal! ""Pjdd
1180 endif
1181
1182 let @" = r
88ff7005 » kana 2007-09-21 s:JoinHere: 1183 call setpos('.', pos)
1184 endfunction
1185
1186
73ac2940 » kana 2008-01-22 vim/dot.vimrc: 1187
1188
668bc915 » kana 2009-09-04 vim: vimrc: Use operator-user 1189 function! s:operator_adjust_window_height(motion_wiseness) "{{{2
1190 execute (line("']") - line("'[") + 1) 'wincmd' '_'
1191 normal! `[zt
1192 endfunction
1193
1194
1195
1196
293c926e » kana 2008-10-24 vim: vimrc: Refactor - layo... 1197 function! s:set_short_indent() "{{{2
1198 setlocal expandtab softtabstop=2 shiftwidth=2
c3b76d8a » kana 2008-02-21 bash: New alias g=git 1199 endfunction
1200
1201
195a83f9 » kana 2007-02-16 Fix the layout. 1202
1203
1204
1205
1206
1207
ee2fb8d1 » kana 2008-05-04 vim: vimrc: Update some ite... 1208 " Mappings "{{{1
792cc7d6 » kana 2007-10-10 Add some FIXMEs. 1209 " FIXME: some mappings are not countable.
41fad213 » kana 2008-04-17 vim: vimrc: Fix the layout ... 1210 " Physical/Logical keyboard layout declaration "{{{2
1211
35475d81 » kana 2009-04-28 sh: ENV_* - Rename "colinux... 1212 if $ENV_WORKING != 'summer' && $ENV_WORKING !=# 'winter'
7ddaf8fe » kana 2009-04-28 sh: New host "chocolate" / ... 1213 " Semicolon and Return are swapped by KeyRemap4MacBook, Mayu or Kinesis on
1214 " some environments.
eee737d5 » kana 2008-05-05 vim: vimrc: Syntax: Place :... 1215 KeyboardLayout ;
1216 KeyboardLayout :
1217 KeyboardLayout ;
1218 KeyboardLayout :
41fad213 » kana 2008-04-17 vim: vimrc: Fix the layout ... 1219 else
eee737d5 » kana 2008-05-05 vim: vimrc: Syntax: Place :... 1220 KeyboardLayout ; ;
1221 KeyboardLayout : :
1222 KeyboardLayout
1223 KeyboardLayout
41fad213 » kana 2008-04-17 vim: vimrc: Fix the layout ... 1224 endif
1225
1226
79b462c7 » kana 2008-06-01 vim: vimrc: Mappings: Add e... 1227
1228
1229 " Lazy man's hacks on the Semicolon key "{{{2
1230 "
ac1bb3d9 » kana 2008-05-04 vim: vimrc: Mappings: Fix t... 1231 " - Don't want to press Shift to enter the Command-line mode.
1232 " - Don't want to press far Return key to input .
79b462c7 » kana 2008-06-01 vim: vimrc: Mappings: Add e... 1233 "
ac1bb3d9 » kana 2008-05-04 vim: vimrc: Mappings: Fix t... 1234 " Note: To override these definitions by other mappings, these must be written
1235 " before them.
79b462c7 » kana 2008-06-01 vim: vimrc: Mappings: Add e... 1236
ac1bb3d9 » kana 2008-05-04 vim: vimrc: Mappings: Fix t... 1237 noremap (physical-key-;) :
1238 noremap (physical-key-:) ;
1239 noremap (physical-key-)
1240 noremap (physical-key-)
1241 noremap! (physical-key-;)
1242 noremap! (physical-key-:)
1243 noremap! (physical-key-) ;
1244 noremap! (physical-key-) :
1245
8c8ca86b » kana 2009-01-17 vim: vimrc: Add synonyms fo... 1246 " Synonyms for the far Return key.
1247 map [Space] (physical-key-)
1248 map [Space]; (physical-key-)
1249
79b462c7 » kana 2008-06-01 vim: vimrc: Mappings: Add e... 1250 " Experimental: to input semicolon/colon without the far Semicolon key.
9bb3b6ac » kana 2008-12-06 vim: vimrc: Fix unnecessary... 1251 noremap! , ;
1252 noremap! . :
1253 noremap! / ;
1254 noremap! ? :
79b462c7 » kana 2008-06-01 vim: vimrc: Mappings: Add e... 1255
ac1bb3d9 » kana 2008-05-04 vim: vimrc: Mappings: Fix t... 1256
41fad213 » kana 2008-04-17 vim: vimrc: Fix the layout ... 1257
1258
b44c6353 » kana 2007-10-10 Key Mappings / Tag-related ... 1259 " Tag jumping "{{{2
f2552813 » kana 2007-10-10 Key Mappings / Tag jumping: 1260 " FIXME: the target window of :split/:vsplit version.
7ae3d3dd » kana 2007-03-03 Key mappings (Tag-related h... 1261 " Fallback "{{{3
1262
1263 " ``T'' is also disabled for consistency.
ab470bba » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1264 noremap t
1265 noremap T
7ae3d3dd » kana 2007-03-03 Key mappings (Tag-related h... 1266
1267 " Alternatives for the original actions.
ab470bba » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1268 noremap [Space]t t
1269 noremap [Space]T T
7ae3d3dd » kana 2007-03-03 Key mappings (Tag-related h... 1270
1271
b44c6353 » kana 2007-10-10 Key Mappings / Tag-related ... 1272 " Basic "{{{3
1273
ab470bba » kana 2008-05-04 vim: vimrc: Update "Notes" ... 1274 nnoremap tt
1275 vnoremap tt
4b200539 » kana 2008-05-05 vim: vimrc: Update :Cmap / ... 1276 Cnmap tj tag
1277 Cnmap tk pop
1278 Cnmap tl tags
1279 Cnmap tn tnext
1280 Cnmap tp tprevious
1281 Cnmap tP tfirst
1282 Cnmap tN tlast
7ae3d3dd » kana 2007-03-03 Key mappings (Tag-related h... 1283
c8c91080 » kana 2008-01-12 vim/dot.vimrc: 1284 " additions, like Web browsers
ba6366d1 » kana 2008-04-11 vim: vimrc: Fix all {lhs} t... 1285 nmap (physical-key-) tt
1286 vmap (physical-key-) tt
7ae3d3dd » kana 2007-03-03 Key mappings (Tag-related h... 1287
c8c91080 » kana 2008-01-12 vim/dot.vimrc: 1288 " addition, interactive use.