在我看来,当需要改变 and 的行为时<CR>
,<BS>
这意味着有问题,或者迟早会出错,因为有很多边缘情况。
我发现的最大问题之一是我无法猜测光标是在函数内的第一列还是第二列,重要的是要知道能够正确处理制表符和退格键。但在这里你有一个开始。我没有彻底测试它,因为它是一团糟。我不推荐它,我认为myki的方法要好得多。
将此注释良好的代码添加到您的vimrc
文件中并进行测试。
"" When pressed 'return' in insert mode:
"" <Esc>: Exit from Insert Mode to Normal Mode.
"" o: Add a new line below the current one and set Insert Mode.
"" call <Esc>: Write literal 'call ' and exit from Insert Mode.
"" A: Set cursor at end of line and enter Insert Mode to begin writting.
inoremap <cr> <Esc>ocall <Esc>A
function! SpecialTab()
"" Get cursor position.
let cursor_pos = getpos('.')
"" If line is empty, insert a tab, update current position and finish
let line_len = strlen( getline( line('.') ) )
if empty( getline( line('.') ) ) || line_len == 1
s/\%#/\t/
let cursor_pos[2] += 1
call setpos( '.', cursor_pos )
return
endif
"" Search for a line beginning with 'call', omitting spaces. If found
"" insert a tab at the beginning of line.
if match( getline( line('.') ), "\s*call" ) != -1
s/^/\t/
else
"" Insert a normal tab in current cursor position. I cannot use
"" the regular <Tab> command because function would entry in a
"" infinite recursion due to the mapping.
s/\%#\(.\)/\1\t/
endif
"" Set cursor column in the character it was before adding tab character.
let cursor_pos[2] += 2
call setpos( '.', cursor_pos )
endfunction
"" Map the tab character.
inoremap <Tab> <Esc>:call SpecialTab()<cr>:startinsert<cr>
function! SpecialBackspace()
"" Do nothing if line is empty.
if empty( getline( line('.') ) )
return
endif
"" Get cursor position.
let cursor_pos = getpos( '.' )
"" If cursor is not in first column press 'delete' button and done.
if col('.') > 1
execute "normal \<Del>"
return
endif
"" Search for 'call' string. If found delete it and set cursor in
"" previous position.
if match( getline( line('.') ), "\s*call" ) != -1
s/call//
let cursor_pos[2] = 1
call setpos( '.', cursor_pos )
return
endif
"" A line with one character is a special case. I delete the complete
"" line.
let line_len = strlen( getline( line('.') ) )
if line_len == 1
s/^.*$//
return
endif
"" If cursor is in first column, delete character. Messy behavior, I
"" think :-/
if col('.') == 1
s/^.//
endif
endfunction
"" Map the 'backspace' character.
inoremap <BS> <Esc>:call SpecialBackspace()<cr>:startinsert<cr>