IT박스

vim에서 명령 앨리어싱

itboxs 2020. 6. 19. 19:56
반응형

vim에서 명령 앨리어싱


Vim 내가 프로그래밍 할 때 선호하는 텍스트 편집기이므로 항상 성가신 문제가 발생합니다.

종종 버퍼를 빠르게 저장하고 다른 기타 작업을 계속해야 할 때 나는 전형적인 작업을 수행합니다.

:w

그러나, 시간의 50 % 이상인 것처럼 보이는 나는 항상 그것을 활용합니다 :w. 당연히, vim W은 잘못된 명령 이기 때문에 소리를 지 릅니다.

E492: Not an editor command: W

내 질문은 어떻게 vim에서 하나의 별칭 콜론 명령을 사용할 수 있는지 입니다. 특히에 별칭 W을 지정 하는 방법을 예시 해 주시겠습니까 w?

를 특정 명령매핑 하는 프로세스를 알고 있습니다. 불행히도, 그것은 내가 찾고있는 것이 아닙니다.


완성을 그대로 두려면

cnoreabbrev W w

, W는 명령 행에서으로 대체 w되지만 단어 문자가 앞에 오지 않고 앞에 오는 경우에만으로 :W<CR>바뀌 므로 으로 대체 :w<CR>되지만 :Write그렇지 않습니다. (예를 들어 명령 :saveas W Z이로 대체 될 수 있으므로 예상하지 못한 명령을 포함하여 일치하는 모든 명령에 영향을 미치 :saveas w Z므로주의하십시오.)

최신 정보

다음은 내가 지금 쓰는 방법입니다 .

cnoreabbrev <expr> W ((getcmdtype() is# ':' && getcmdline() is# 'W')?('w'):('W'))

기능으로서 :

fun! SetupCommandAlias(from, to)
  exec 'cnoreabbrev <expr> '.a:from
        \ .' ((getcmdtype() is# ":" && getcmdline() is# "'.a:from.'")'
        \ .'? ("'.a:to.'") : ("'.a:from.'"))'
endfun
call SetupCommandAlias("W","w")

명령 유형이 :이고 명령이 W이므로 확인하는 것보다 안전 cnoreabbrev W w합니다.


보충 검색을 통해 누군가가 나와 거의 같은 질문한 것을 발견했습니다 .

:command <AliasName> <string of command to be aliased>

트릭을 할 것입니다.

로, 양해하여 주시기 바랍니다 Richo는 지적, 사용자 명령은 대문자로 시작해야합니다.


;키를 매핑 :하는 것이 더 나은 솔루션이며 다른 명령을 입력하는 데 생산성이 높아질 것입니다.

nnoremap ; :
vnoremap ; :

가장 좋은 해결책은 명령 모음의 시작 부분에서만 발생하는 약어를 처리하기위한 사용자 지정 함수작성하는 것 입니다.

이를 위해 vimrc 파일 또는 다른 곳에 다음을 추가하십시오.

" cabs - less stupidity                                                      {{{
fu! Single_quote(str)
  return "'" . substitute(copy(a:str), "'", "''", 'g') . "'"
endfu
fu! Cabbrev(key, value)
  exe printf('cabbrev <expr> %s (getcmdtype() == ":" && getcmdpos() <= %d) ? %s : %s',
    \ a:key, 1+len(a:key), Single_quote(a:value), Single_quote(a:key))
endfu
"}}}

 

" use this custom function for cabbrevations. This makes sure that they only
" apply in the beginning of a command. Else we might end up with stuff like
"   :%s/\vfoo/\v/\vbar/
" if we happen to move backwards in the pattern.

" For example:
call Cabbrev('W', 'w')

몇 가지 소스 자료에서 유용한 약어 나는이 물건을 발견 :

call Cabbrev('/',   '/\v')
call Cabbrev('?',   '?\v')

call Cabbrev('s/',  's/\v')
call Cabbrev('%s/', '%s/\v')

call Cabbrev('s#',  's#\v')
call Cabbrev('%s#', '%s#\v')

call Cabbrev('s@',  's@\v')
call Cabbrev('%s@', '%s@\v')

call Cabbrev('s!',  's!\v')
call Cabbrev('%s!', '%s!\v')

call Cabbrev('s%',  's%\v')
call Cabbrev('%s%', '%s%\v')

call Cabbrev("'<,'>s/", "'<,'>s/\v")
call Cabbrev("'<,'>s#", "'<,'>s#\v")
call Cabbrev("'<,'>s@", "'<,'>s@\v")
call Cabbrev("'<,'>s!", "'<,'>s!\v")

기능 키 (F1..F12) 중 하나를 : w? 그런 다음 이것을 .vimrc에 넣으십시오.

noremap  <f1> :w<return>
inoremap <f1> <c-o>:w<return>

(삽입 모드에서 ctrl-o는 일시적으로 일반 모드로 전환됩니다).


Suppose you want to add alias for tabnew command in gvim. you can simply type the following command in your .vimrc file (if not in home folder than create one)

cabbrev t tabnew

Safest and easiest is a plugin such as cmdalias.vim or my recent update vim-alias of it that take into account

  • preceding blanks or modifiers such as :sil(ent)(!) or :redi(r),
  • range modifiers such as '<,'> for the current visual selection,
  • escape special characters such as quotes, and
  • check if the chosen alias is a valid command line abbreviation.

참고URL : https://stackoverflow.com/questions/3878692/aliasing-a-command-in-vim

반응형