首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改vim命令执行优先级?

如何更改vim命令执行优先级?
EN

Stack Overflow用户
提问于 2020-09-07 09:18:29
回答 1查看 647关注 0票数 1

我做了:wincmd h<CR><leader>h的捷径,而mapleader对我来说就是<Space>

但是当我做<leader>h的时候,我觉得执行这个动作的时间很长,

所以我通过<leader>h:map <leader>h搜索了有关:map <leader>h的命令

我发现我有以<Space>h开头的服务器命令,如下所示

代码语言:javascript
复制
n  <Space>hp    @<Plug>(GitGutterPreviewHunk)
n  <Space>hu    @<Plug>(GitGutterUndoHunk)
n  <Space>hs    @<Plug>(GitGutterStageHunk)
x  <Space>hs    @<Plug>(GitGutterStageHunk)
n  <Space>h    * :wincmd h<CR>

我认为由于地图的优先级,<Space>h是最后执行的,

我想将执行优先级<Space>h更改为第一位。

因为对于<Space>h来说,:windcmd h是最简单的捷径。

如何改变地图的优先级顺序?

====编辑====

我认为vim从h中等待下一个字符,那么如何消除<Space>h的延迟?

  1. <nowait>不工作(我不知道为什么)
  2. 插件的重新映射不起作用

==== edit2 ==== (它仍然没有工作思想采用第一个答案)

init.vim

代码语言:javascript
复制
""Source file
source $HOME/.config/nvim/ascii_art.vim

""Plugin-install
call plug#begin('~/.vim/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'tweekmonster/gofmt.vim'
Plug 'tpope/vim-fugitive'
Plug 'vim-utils/vim-man'
Plug 'mbbill/undotree'
Plug 'sheerun/vim-polyglot'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'stsewd/fzf-checkout.vim'
Plug 'vuciv/vim-bujo'
Plug 'tpope/vim-dispatch'
Plug 'theprimeagen/vim-be-good', {'do': './install.sh'}
Plug 'vim-airline/vim-airline'
Plug 'gruvbox-community/gruvbox'
Plug 'vifm/vifm.vim'
"Theme
Plug 'colepeters/spacemacs-theme.vim'
Plug 'sainnhe/gruvbox-material'
Plug 'phanviet/vim-monokai-pro'
Plug 'flazz/vim-colorschemes'
Plug 'chriskempson/base16-vim'
" fuzzy find files
Plug 'ctrlpvim/ctrlp.vim' 
" git gutter
Plug 'airblade/vim-gitgutter'
Plug 'scrooloose/nerdcommenter'
"NerdTree plugins (NerdTree makes vim too slow, so now it unstalled)
"Plug 'scrooloose/nerdtree'
"Plug 'Xuyuanp/nerdtree-git-plugin'
"Plug 'tiagofumo/vim-nerdtree-syntax-highlight'
"Plug 'ryanoasis/vim-devicons'
Plug 'mattn/emmet-vim'
Plug 'vimwiki/vimwiki' "take note
Plug 'mhinz/vim-startify' "easy way to save and load session
call plug#end()

""General-setting
language en_US
syntax on
set number
set relativenumber
set guicursor=
set relativenumber
set nohlsearch
set hidden
set noerrorbells
set tabstop=4 softtabstop=4
set shiftwidth=4 "tab key spaces, = set sw
set expandtab "instead of tab, insert spaces
set smartindent
set nu
set nowrap
set smartcase
set noswapfile
set undofile
set incsearch
set termguicolors
set scrolloff=8 "Scroll offset
set noshowmode
set cmdheight=2
set updatetime=50
set shortmess+=c
set cursorline
set hlsearch "highlight searched words
"NOTE :eol is end of the line
set listchars=eol:↲,tab:→\ ,trail:~,extends:>,precedes:<,space:␣ 
set nolist "set list => $ is end of the line
"Fold setting: when auto save and load fold
set foldcolumn=2 
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent! loadview 

highlight ColorColumn ctermbg=0 guibg=lightgrey

""Color Theme
colorscheme gruvbox 
"cursorline termguicolors
set background=dark
let g:gruvbox_contrast='soft'

let loaded_matchparen = 1
let mapleader = " "

""Status-line
set statusline=
set statusline+=%#IncSearch#
set statusline+=\ %y
set statusline+=\ %r
set statusline+=%#CursorLineNr#
set statusline+=\ %F
set statusline+=%= "Right side settings
set statusline+=%#Search#
set statusline+=\ %l/%L
set statusline+=\ [%c]

""Key-bindings
nnoremap <leader>gc :GCheckout<CR>
nnoremap <leader>ghw :h <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>prw :CocSearch <C-R>=expand("<cword>")<CR><CR>
nnoremap <leader>pw :Rg <C-R>=expand("<cword>")<CR><CR>
nnoremap <nowait> <leader>j :wincmd j<CR>
nnoremap <nowait> <leader>k :wincmd k<CR>
nnoremap <nowait> <leader>l :wincmd l<CR>
"bug report:: :wincmd h is mapped with below mapping, 
"> but can not use shortcuts for  GigGutterPreviewHunk, GitGutterUndoHunk, GitGutterStageHunk
autocmd filetype * nnoremap <nowait> <buffer> <leader>h :wincmd h<CR>
"nnoremap <nowait> <leader>h :wincmd h<CR>
nnoremap <leader>u :UndotreeShow<CR>
"nnoremap <leader>pv :wincmd v<bar> :Ex <bar> :vertical resize 30<CR>
nnoremap <leader>pv :call ToggleNetrw() <bar> :vertical resize 30<CR>
nnoremap <Leader>ps :Rg<SPACE>
nnoremap <C-p> :GFiles<CR>
nnoremap <Leader>pf :Files<CR>
nnoremap <Leader><CR> :<c-u>call <sid>Goto_definition() <CR>
nnoremap <F4> :so ~/.config/nvim/init.vim<CR>
nnoremap <Left> :vertical resize -2<CR>
nnoremap <Right> :vertical resize +2<CR>
nnoremap <Up> :resize +2<CR>
nnoremap <Down> :resize -2<CR>
nnoremap <Leader>rp :resize 100<CR>
nnoremap <Leader>ee oif err != nil {<CR>log.Fatalf("%+v\n", err)<CR>}<CR><esc>kkI<esc>
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv
vnoremap X "_d
"built-in mapping disable
nnoremap Q <Nop>
" vim TODO
nmap <Leader>tu <Plug>BujoChecknormal
nmap <Leader>th <Plug>BujoAddnormal
let g:bujo#todo_file_path = $HOME . "/.cache/bujo"
" GoTo code navigation.
nmap <leader>gd <Plug>(coc-definition)
nmap <leader>gy <Plug>(coc-type-definition)
nmap <leader>gi <Plug>(coc-implementation)
nmap <leader>gr <Plug>(coc-references)
nmap <leader>rr <Plug>(coc-rename)
nmap <leader>g[ <Plug>(coc-diagnostic-prev)
nmap <leader>g] <Plug>(coc-diagnostic-next)
nmap <silent> <leader>gp <Plug>(coc-diagnostic-prev-error)
nmap <silent> <leader>gn <Plug>(coc-diagnostic-next-error)
nnoremap <leader>cr :CocRestart
nnoremap <silent> K :call <SID>show_documentation()<CR>
"quickly enter new line in normal mode
nnoremap <Leader>o o<Esc>

nmap ghs <Plug>(GitGutterStageHunk)
xmap ghs <Plug>(GitGutterStageHunk)
nmap ghu <Plug>(GitGutterUndoHunk)
nmap ghp <Plug>(GitGutterPreviewHunk)

"Emmet key
let g:user_emmet_mode="n"
let g:user_emmet_leader_key="," "default leader_key is <c-y> with trailing ,(comma)

" coc config
let g:coc_global_extensions = [
  \ 'coc-snippets',
  \ 'coc-pairs',
  \ 'coc-tsserver',
  \ 'coc-eslint',
  \ 'coc-prettier',
  \ 'coc-json',
  \ 'coc-marketplace',
  \ ]
"coc snippet
" Use <C-l> for trigger snippet expand.
imap <C-l> <Plug>(coc-snippets-expand)
" Use <C-j> for select text for visual placeholder of snippet.
vmap <C-j> <Plug>(coc-snippets-select)
" Use <C-j> for jump to next placeholder, it's default of coc.nvim
let g:coc_snippet_next = '<c-j>'
" Use <C-k> for jump to previous placeholder, it's default of coc.nvim
let g:coc_snippet_prev = '<c-k>'
" Use <C-j> for both expand and jump (make expand higher priority.)
imap <C-j> <Plug>(coc-snippets-expand-jump)


inoremap jk <ESC>
vmap ++ <plug>NERDCommenterToggle
nmap ++ <plug>NERDCommenterToggle

let g:NERDTreeIgnore = ['^node_modules$']

" ctrlp
let g:ctrlp_user_command = ['.git/', 'git --git-dir=%s/.git ls-files -oc --exclude-standard']

" Highlight currently open buffer in NERDTree
autocmd BufEnter * call SyncTree()

" Remap for rename current word
nmap <F2> <Plug>(coc-rename)


"Functions
"show documetation function
function! s:show_documentation()
  if (index(['vim','help'], &filetype) >= 0)
    execute 'h '.expand('<cword>')
  else
    call CocAction('doHover')
  endif
endfunction
"sync open file with NERDTree, Check if NERDTree is open or active
function! IsNERDTreeOpen()        
  return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction 
" Call NERDTreeFind iff NERDTree is active, current window contains a modifiable, file, and we're not in vimdiff
function! SyncTree()
  if &modifiable && IsNERDTreeOpen() && strlen(expand('%')) > 0 && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction 
" autocomplete functions for neoclide/coc.nvim
"---------------------------------------------------------------------------
" Use tab for trigger completion with characters ahead and navigate.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction
" Use <c-space> to trigger completion.
if has('nvim')
  inoremap <silent><expr> <c-space> coc#refresh()
else
  inoremap <silent><expr> <c-@> coc#refresh()
endif
" Use <cr> to confirm completion, `<C-g>u` means break undo chain at current
" position. Coc only does snippet and additional edit on confirm.
" <cr> could be remapped by other vim plugin, try `:verbose imap <CR>`.
if exists('*complete_info')
 inoremap <expr> <cr> complete_info()["selected"] != "-1" ? "\<C-y>" : "\<C-g>u\<CR>"
else
 inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
endif
"open implementation to the right split view(custom function)
"buggy: If you quit after invoke this function, this function will not work(it will quit your only window)
let s:first_open=1
function! s:Goto_definition() abort 
    if s:first_open
        rightbelow vs "same as :vertical split and :wincmd l
        normal K 
        wincmd h
        let s:first_open=0
    else 
        wincmd l "If right split window exists then quit
        q
        rightbelow vs
        normal K
        wincmd h
    endif
endfunction
"---------------------------------------------------------------------------

"" netrw settings
let g:netrw_liststyle=3 "Tree style
let g:netrw_banner = 0 "Remove banner
let g:netrw_browse_split = 4 "Open file in previous window
let g:netrw_altv = 1 "Open vertical split window to the right side
let g:netrw_preview=1 "Preview in right side 
set nocompatible "Limit search for your project
set path+=** "Search all subdirectories with recursively
set wildmenu "Show multifiles on one line when you :find
"Root directory:make current directory to root directory
nnoremap <leader>` :Ntree<CR> 
let g:NetrwIsOpen=0
function! ToggleNetrw()
    if g:NetrwIsOpen
        let i = bufnr("$")
        while (i >= 1)
            if (getbufvar(i, "&filetype") == "netrw")
                silent exe "bwipeout " . i 
            endif
            let i-=1
        endwhile
        let g:NetrwIsOpen=0
    else
        let g:NetrwIsOpen=1
        silent Lexplore
    endif
endfunction
let g:netrw_list_hide= netrw_gitignore#Hide()

"=================Unorganized settings==================
"jsx comment 
let g:NERDCustomDelimiters={
    \ 'javascript': { 'left': '//', 'right': '', 'leftAlt': '{/*', 'rightAlt': '*/}' },
\}
"vimwiki setup
"let g:vimwiki_list = [{'path': '~/vimwiki/',
                      "\ 'syntax': 'markdown', 'ext': '.md'}]

===编辑结果(现在正在工作) ===

对于:map <leader>h,现在显示如下所示

我所做的

  1. 从答案中添加4个片段
  2. :PlugUpdate => I已经过时了。
  3. 删除<nowait>
  4. :echo hasmapto('<Plug>(GitGutterStageHunk)', 'n')输出1

当达到2步时,它没有工作,但是在删除nowait之后,它会执行一些干扰命令,但我不知道确切的原因。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-07 15:20:06

<Leader>h作为前缀的映射将导致这种干扰。在执行<Leader>h映射之前,Vim将等待超时或附加键按,因为它希望检查是否要按一个较长的映射.

解决这个问题的最简单方法是为命令设置备用映射。如果您为vimrc中的那些映射设置了不同的映射,vim本身就不会创建它的映射,从而导致干扰。

vim-gitgutter自述建议使用g作为替代前缀:

要为这些映射设置自己的映射,例如,如果您更喜欢g-based映射: nmap ghs (GitGutterStageHunk) nmap ghu (GitGutterUndoHunk)

因此,继续为破坏您的<Leader>h的四个键绑定创建映射。将这四行添加到vimrc中:

代码语言:javascript
复制
nmap ghs <Plug>(GitGutterStageHunk)
xmap ghs <Plug>(GitGutterStageHunk)
nmap ghu <Plug>(GitGutterUndoHunk)
nmap ghp <Plug>(GitGutterPreviewHunk)

xmap (在视觉模式下进行分期)在技术上不是必需的,您可以在不受干扰的情况下继续使用<Leader>hs。但另一方面,您可能希望在正常模式和视觉模式之间保持这些一致性,因此我建议也重新映射该模式。

<noway>不能解决这个问题的原因是,<nowait>只在隐藏全局映射时只在<buffer>-local映射上工作。当所有映射都是全局的时,它就不起作用了。(在这种情况下,Vim更倾向于强迫您解决冲突,而不是使最长的映射始终不可访问。)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63774721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档