VIM输入模式键盘映射教程
2010-11-21 22:02
232 查看
http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_1)
To execute Vim normal mode commands from an insert mode map, you have to go from insert mode to normal mode. But after executing the map, you may want to restore the mode back to insert mode. To do this, you can use the <CTRL-O> insert-mode key which temporarily goes to normal-mode for one normal mode command and then comes back to insert mode. For example, to call the Vim function MyVimFunc() from insert mode, you can use the following map command:
One caveat with using the <C-O> command is that if the cursor is after the last character in a line in insert mode, then <C-O> moves the cursor one character to the left after executing the map. If you don't want this, then you can use the <C-/><C-O> command, which doesn't move the cursor. But now the cursor may be placed on a character beyond the end of a line. The above map command is modified to use the <C-/><C-O> key:
Both the <C-O> and <C-/><C-O> commands create a new undo point, i.e. you can undo the text inserted before and after typing these commands separately.
Another alternative for going from insert mode to normal mode is to use the <Esc> key. But it is preferable to use the <C-O> or <C-/><C-O> command for this.
If you press <Esc> in normal mode to make sure you are in normal mode, then you will hear the error beep sound. Instead, you can use the CTRL-/ CTRL-N command to go to normal mode. If you are already in normal mode, this command will not result in the error bell. This command can be used from a map to go to normal mode.
After executing the normal mode commands from an insert mode map, if the cursor position was moved by the map and no new text was inserted by the commands invoked, then you can use the gi command to restart the insert mode from the previous position where the insert mode was last stopped.
You can insert the result of a Vim expression in insert mode using the <C-R>= command. For example, the following command creates an insert mode map command that inserts the current directory:
If you don't want to insert anything then you can return an empty string from the expression. For example, you can invoke a function from the insert mode map to perform some operation but return an empty string from the function.
The <C-R>= command doesn't create a new undo point. You can also call Vim functions using the <C-R>= command:
When Vim parses a string in a map command, the /<...> sequence of characters is replaced by the corresponding control character. For example, let us say in insert mode you want the down arrow key to execute <C-N> when the insert complete popup menu is displayed. Otherwise, you want the down arrow key to move the cursor one line down. You can try the following command (which doesn't work):
When parsing the above command, Vim replaces <C-N> and <Down> with the corresponding control characters. When you press the down arrow in insert mode, as there are control characters in the expression now, the command will fail.
To fix this, you should escape the '<' character, so that Vim will not replace '/<C-N>' with the control character when parsing the command. The following command works:
With the above command, Vim will use the control character only when the map is invoked and not when the above command is parsed.
例如:映射c-v为paste键
inoremap <C-V> <C-R>='<c-r>+'<CR>
其中<c-r>+表示剪贴板寄存器具体可以使用:help <c-r>查看
To execute Vim normal mode commands from an insert mode map, you have to go from insert mode to normal mode. But after executing the map, you may want to restore the mode back to insert mode. To do this, you can use the <CTRL-O> insert-mode key which temporarily goes to normal-mode for one normal mode command and then comes back to insert mode. For example, to call the Vim function MyVimFunc() from insert mode, you can use the following map command:
:inoremap <F5> <C-O>:call MyVimFunc()<CR>
One caveat with using the <C-O> command is that if the cursor is after the last character in a line in insert mode, then <C-O> moves the cursor one character to the left after executing the map. If you don't want this, then you can use the <C-/><C-O> command, which doesn't move the cursor. But now the cursor may be placed on a character beyond the end of a line. The above map command is modified to use the <C-/><C-O> key:
:inoremap <F5> <C-/><C-O>:call MyVimFunc()<CR>
Both the <C-O> and <C-/><C-O> commands create a new undo point, i.e. you can undo the text inserted before and after typing these commands separately.
Another alternative for going from insert mode to normal mode is to use the <Esc> key. But it is preferable to use the <C-O> or <C-/><C-O> command for this.
If you press <Esc> in normal mode to make sure you are in normal mode, then you will hear the error beep sound. Instead, you can use the CTRL-/ CTRL-N command to go to normal mode. If you are already in normal mode, this command will not result in the error bell. This command can be used from a map to go to normal mode.
After executing the normal mode commands from an insert mode map, if the cursor position was moved by the map and no new text was inserted by the commands invoked, then you can use the gi command to restart the insert mode from the previous position where the insert mode was last stopped.
You can insert the result of a Vim expression in insert mode using the <C-R>= command. For example, the following command creates an insert mode map command that inserts the current directory:
:inoremap <F2> <C-R>=expand('%:p:h')<CR>
If you don't want to insert anything then you can return an empty string from the expression. For example, you can invoke a function from the insert mode map to perform some operation but return an empty string from the function.
The <C-R>= command doesn't create a new undo point. You can also call Vim functions using the <C-R>= command:
:inoremap <F2> <C-R>=MyVimFunc()<CR>
When Vim parses a string in a map command, the /<...> sequence of characters is replaced by the corresponding control character. For example, let us say in insert mode you want the down arrow key to execute <C-N> when the insert complete popup menu is displayed. Otherwise, you want the down arrow key to move the cursor one line down. You can try the following command (which doesn't work):
:inoremap <Down> <C-R>=pumvisible() ? '/<C-N>' : '/<Down>'<CR>
When parsing the above command, Vim replaces <C-N> and <Down> with the corresponding control characters. When you press the down arrow in insert mode, as there are control characters in the expression now, the command will fail.
To fix this, you should escape the '<' character, so that Vim will not replace '/<C-N>' with the control character when parsing the command. The following command works:
:inoremap <Down> <C-R>=pumvisible() ? '/<lt>C-N>' : '/<lt>Down>'<CR>
With the above command, Vim will use the control character only when the map is invoked and not when the above command is parsed.
例如:映射c-v为paste键
inoremap <C-V> <C-R>='<c-r>+'<CR>
其中<c-r>+表示剪贴板寄存器具体可以使用:help <c-r>查看
相关文章推荐
- Vim技能修炼教程(7) - 可视模式
- Android键盘映射和键盘输入
- 在Putty/winSCP连接拒绝及Vim(插入模式)下,无法输入,没有反应。
- Android:键盘挡住输入框解决办法 ---- 软输入法模式选项:
- 终于解决了ubuntu下gnome-terminal中使用vim时在Insert模式下功能键键映射的问题
- XNA开发实用教程——键盘输入事件
- 在全屏模式在Silverlight 4 支持键盘输入
- vim学习手册-2输入模式
- 【Unity3D入门教程】鼠标和键盘输入与控制
- VIM键盘快捷键映射
- android PinYinIME横屏模式禁止全屏输入 & 调整横屏模式下键盘高度
- VisualC++6.0模式键盘映射模式下的常用快捷键
- VIM命令模式与输入模式切换
- vim 快捷键映射和模式
- vim 命令模式与输入模式切换
- MAC系统下修改键盘映射适应vim编辑
- putty上vim小键盘不能输入数字
- VIM键盘映射 (Map)~转载
- (问题)android PinYinIME横屏模式禁止全屏输入 & 调整横屏模式下键盘高度
- 解决Xshell里vim模式下小键盘只输出字母不能输出数字