VIM自定义键绑定黑客

时间:2011-02-13 05:55:59

标签: vim

您使用哪些自定义VIM键绑定来提高工作效率? 这是我最喜欢的两个。

inoremap jj <Esc>

" Clear screen clears search highlighting.
nnoremap <C-L> :nohl<CR><C-L>

6 个答案:

答案 0 :(得分:4)

Windows和缓冲区

" Window splitting

nmap <silent> <leader>sh :leftabove vnew<cr>
nmap <silent> <leader>sl :rightbelow vnew<cr>
nmap <silent> <leader>sk :leftabove new<cr>
nmap <silent> <leader>sj :rightbelow new<cr>
nmap <silent> <leader>swh :topleft vnew<cr>
nmap <silent> <leader>swl :botright vnew<cr>
nmap <silent> <leader>swk :topleft new<cr>
nmap <silent> <leader>swj :botright new<cr>

" Scroll the window next to the current one
"   (especially useful for two-window split)
nmap <silent> <leader>j <c-w>w<c-d><c-w>W
nmap <silent> <leader>k <c-w>w<c-u><c-w>W

修改

" Toggle search highlighting
nmap <silent> <leader>/ :set hlsearch!<cr>
" Toggle paste mode
"   (prefer this over 'pastetoggle' to echo current state)
nmap <leader>p :setlocal paste! paste?<cr>

" Select the last edited/pasted text
nmap gv `[v`]

" Keep lines that do (or do not) contain last search term
nmap <leader>v/ :v/<c-r>//d<cr>gg
nmap <leader>g/ :g/<c-r>//d<cr>gg

" Email (de-)quotation
nmap <leader>q vip:s/^/> /<cr>
vmap <leader>q :s/^/> /<cr>
nmap <leader>Q vip:s/^> //<cr>
vmap <leader>Q :s/^> //<cr>

文件

" Save and restore session
nmap <leader>ss :wa<cr>:mksession! $HOME/.vim/sessions/
nmap <leader>rs :wa<cr>:source $HOME/.vim/sessions/

" Write buffer through sudo
cnoreabbrev w!! w !sudo tee % >/dev/null

" Change current directory to the directory of the file in buffer
nmap <silent> <leader>cd :cd %:p:h<cr>:pwd<cr>

" Open file located in the same directory as the current one
nmap <leader>e :e <c-r>=expand('%:p:h').'/'<cr>

答案 1 :(得分:3)

Grep光标下的单词

使用以下grepprg选项,它将递归搜索到当前目录,不包括某些特定文件。

" Quick Grep
noremap <Leader>g :grep<space><C-r><C-w><CR>:copen<CR><CR><C-W>b

set grepprg=grep\ -nH
\\--include='*.c'
\\--include='*.cpp'
\\--include='*.h'
\\--exclude-dir='.svn'
\\--exclude='*.svn-base'
\\--exclude-dir='OBJ'
\\--exclude='symTbl.c'
\\ $*
\\ -R\ .

它会抓住光标下的单词,然后打开Quickfix窗口并将光标移动到底部窗口(应该是grep结果列表)

这可能是我最常使用的快捷方式之一,它可以节省大量的输入!

在窗口之间快速移动

noremap <C-j> <C-W>j
noremap <C-k> <C-W>k
noremap <C-h> <C-W>h
noremap <C-l> <C-W>l

当您的屏幕水平和垂直分割时,移动它是非常直观和方便的。

答案 2 :(得分:2)

以下命令在命令模式下将;重新映射到:,这样可以避免在输入:wq等命令时浪费宝贵的毫秒数来保持和释放Shift键:

" Remap ";" to ":"
map ; :
noremap ;; ;

如果您需要输入实际的;,只需按两次。

答案 3 :(得分:2)

插入模式

" <esc> in normal mode clears highlight
nnoremap <silent> <esc> :noh<cr><esc>

命令行编辑

" copy an entire word from the line above instead of just one
inoremap <expr> <c-y> matchstr(getline(line('.')-1), '\%' . 
\ virtcol('.') . 'v\%(\k\+\\|.\)')

" Insert Directory of current buffer and open completion
cnoremap <expr> <c-k> getcmdline()[getcmdpos()-2] == " " ? 
\ expand("%:h") .  "/\<c-d>" : "\<c-d>"

答案 4 :(得分:1)

let mapleader=","

" omnicompletion : words
inoremap <leader>, <C-x><C-o>

" omnicompletion : filepaths
inoremap <leader>: <C-x><C-f>

" omnicompletion : lines
inoremap <leader>= <C-x><C-l>

" toggle TagList
nnoremap <leader>l :TlistToggle<CR>

" toggle NERDTree
nnoremap <leader>n :NERDTreeToggle<CR>

" I like vertically aligned assignation operators
nnoremap <leader>a :Tabularize<Space>

" with | marking the cursor
" it turns this 
" function foo(){|}
" into this
" function foo(){
"   |
" }
inoremap <C-Return> <CR><CR><C-o>k<Tab>

" push the current ligne up and down
nnoremap <M-D-Up>   ddKp
nnoremap <M-D-Down> ddP  

" swap word under the cursor with previous word on the left
" from the Vim wiki
nnoremap <M-D-Left>  "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o><C-l>

" swap word under the cursor with next word on the right
" from the Vim wiki
nnoremap <M-D-Right> "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o>/\w\+\_W\+<CR><C-l>

" and I have lusty-explorers "modes" mapped to:
" "files"   <leader>f
" "buffers" <leader>b
" "grep"    <leader>g

答案 5 :(得分:0)

用于向上/向下导航显示在显示屏上多行的长行: Alt + 箭头键移动屏幕线而不是文件行。

map <A-Down> gj
map <A-Up> gk
imap <A-Up> <ESC>gki
imap <A-Down> <ESC>gji