Program Club

Vim 파일 탐색

proclub 2020. 10. 10. 12:29
반응형

Vim 파일 탐색


지난 몇 년 동안 TextMate를 사용한 후 vim을 배우기 위해 정말 열심히 노력하고 있습니다.

파일 내 탐색 중 일부를 메모리에 적용하기 시작했지만 여러 파일 사이를 탐색하는 데 어려움을 겪고 있습니다.

내 워크 플로에서는 몇 개의 파일 사이를 꽤 정기적으로 전환하는 것이 매우 일반적입니다 (분할 창 창이 너무 작아지는만큼의 파일).

나는 현재 NERDTree를 사용하고 있지만 디렉토리로 드릴 다운하는 것이 번거롭고 CTRL+ W h/ CTRL+ W l계속 사용하여 앞뒤로 이동합니다.

쉽게 전환 할 수있는 탭으로 더 잘할 수 있다고 생각하지만 다른 워크 플로를 사용해야 할 수도 있습니다.

또한 TextMate에서 CMD+ 와 같은 "파일로 이동 ..."바로 가기를 원합니다 T. 찾았 fuzzy_file_finder지만 vim이 Ruby 바인딩으로 빌드되어야하는데, 내가 작업 한 기본 설치의 경우가 아닙니다.

내가 vim으로 전환하고 싶은 주된 이유 다시 빌드 할 수 있지만 모든 플랫폼에서 쉽게 작업 할 수있는 하나의 편집기 환경을 가질 수 있기 때문입니다.

옵션은 압도적 인 어떤 아이디어라도 대단히 감사합니다!


:e탭 완성을위한 적절한 구성을 고려할 때 그다지 귀찮은 평범한 이전 통해 하위 디렉토리로 드릴 다운하는 것을 찾지 못했습니다 .

'wildmenu'명령 줄 위의 모델 라인에서 Vim이 완성 목록 (파일 이름)을 표시하도록 하는 옵션을 살펴보십시오 . 'wildmode'옵션을 변경하여 Vim이 수행 할 탭 완성 유형을 추가로 구성 할 수 있습니다.

개인적으로 나는 :set wildmode=full.

내 워크 플로는 다음과 같습니다.

  1. :cd 내 프로젝트의 최상위 디렉토리로.
  2. 파일 열기 foo/bar/baz:

    • 가장 간단한 시나리오 : :e f<tab>b<tab>b<tab><enter>.

    • 하나 개의 파일로 시작보다 더가있는 경우 b그 디렉토리 중 하나에 당신이해야 할 수도 있습니다 <left>하거나 <right>또는 다른 <tab>그들 사이에 이동 (또는 명확하게에 몇 글자를 입력) 할 수있는 키보드.

    • 최악의 경우 이름을 공유하는 파일과 디렉터리가 있으며 디렉터리로 드릴 다운해야합니다. 이 경우 디렉토리 이름을 탭으로 완성한 다음 입력 *<tab>하여 드릴 다운합니다.

  3. 2 개 또는 3 개의 창을 열고 필요에 따라 모든 창에서 파일을 엽니 다.
  4. 파일이 버퍼에서 열리면 버퍼를 죽이지 마십시오. 새 파일을 열 때 백그라운드에서 열어 둡니다. :e같은 창에 새 파일 있습니다.
  5. 그런 다음을 사용 :b <tab>하여 이미 백그라운드에서 열려있는 버퍼를 순환합니다. 입력 :b foo<tab>하면 일치하는 현재 열려있는 파일에 대해서만 일치 foo합니다.

또한 이러한 매핑을 사용하여 새 창을 더 쉽게 열고 창 사이를 이동할 수 있습니다. 자주하는 작업이기 때문입니다.

" Window movements; I do this often enough to warrant using up M-arrows on this"
nnoremap <M-Right> <C-W><Right>
nnoremap <M-Left> <C-W><Left>
nnoremap <M-Up> <C-W><Up>
nnoremap <M-Down> <C-W><Down>

" Open window below instead of above"
nnoremap <C-W>N :let sb=&sb<BAR>set sb<BAR>new<BAR>let &sb=sb<CR>

" Vertical equivalent of C-w-n and C-w-N"
nnoremap <C-w>v :vnew<CR>
nnoremap <C-w>V :let spr=&spr<BAR>set nospr<BAR>vnew<BAR>let &spr=spr<CR>

" I open new windows to warrant using up C-M-arrows on this"
nmap <C-M-Up> <C-w>n
nmap <C-M-Down> <C-w>N
nmap <C-M-Right> <C-w>v
nmap <C-M-Left> <C-w>V

Vim을 열고 몇 개의 창을 설정하고 그 안에 몇 개의 파일을 여는 데 몇 초가 걸립니다. 개인적으로 매우 유용한 타사 파일 검색 스크립트를 찾지 못했습니다.


파일 시스템을 검색하는 쉬운 방법은 다음 명령입니다.

:Sex

나는 이것을 구성하지 않는다 :)


:Sex, :Vex, :Tex그리고 :Ex당신이에서 뭔가 다른를 사용하려면 시스템에있는 파일을 (ploring) 전 대한 모든 유용한 명령:e

( S/V/TSplit / Vertical / Tab의 약자)


아직 찾지 못했다면 다음을 확인해보십시오.

  • 원래 퍼지 파인더 플러그인-IIRC에는 Ruby 바인딩 문제가 없습니다.
  • 프로젝트 플러그인-NERDTree와 유사
  • 버퍼 탐색기 플러그인-열린 버퍼 목록을 표시합니다.

또한 .vimrc에서 키 단축키를 다시 매핑하여 번거롭지 않게 만들 수 있습니다. 분할 창을 많이 사용합니다. 나는 다음이 그것들을 훨씬 쉽게 다룰 수 있음을 발견했습니다.

" set your own personal modifier key to something handy
let mapleader = "," 

" use ,v to make a new vertical split, ,s for horiz, ,x to close a split
noremap <leader>v <c-w>v<c-w>l
noremap <leader>s <c-w>s<c-w>j
noremap <leader>x <c-w>c

" use ctrl-h/j/k/l to switch between splits
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h

지금까지 LustyExplorer 가 최고 라고 생각 합니다.

이름의 일부만 입력하여 파일을 열 수 있습니다.

현재 파일의 디렉토리, 현재 pwd또는 버퍼 목록에서 파일을 열 수 있습니다 .

버퍼 목록에서 파일을 열면 디렉토리 트리를 탐색 할 필요가 없습니다. 불행히도 다른 두 모드의 경우 여전히 트리를 탐색해야합니다. 좋은 점은 첫 번째 캐릭터부터 시작할 필요가 없다는 것입니다.

당신이 가지고 있다면:

dir_a/
    ...
dir_b/
    file.js
    file.html

열려면 dir_b/file.html일반적으로 입력하여 찾을 수 있습니다.b<Tab>h


쉽게 전환 할 수있는 탭으로 더 잘할 수 있다고 생각하지만 다른 워크 플로를 사용해야 할 수도 있습니다.

Vim의 탭을 알고 계십니까? Vim의 자체 탭을 언급했는지 또는 TextMate를 고정했는지 확실하지 않습니다. 사용중인 Vim 버전은 확실하지 않지만 7.0 이상 (최근에 내가 보는 모든 곳에 설치되어있는 것 같음) 이후 탭이 있으며 훌륭합니다.

:tabe새 탭을 엽니 다 ( "탭 편집"의 약자, 예 :tabe들어 비어 있거나 :tabe path/to/file새 탭에서 파일을 여는 경우). gt/를 사용 하여 인접한 탭 사이를 이동할 수 gT있으며 다른 탭 관련 탐색 명령도 있습니다. Vim ( :help tabs) 에서 기대할 수 있습니다.

대규모 프로젝트에 대한 내 워크 플로에는 필요한 컨텍스트의 종류에 따라 각각 1 ~ 3 개의 창이있는 여러 탭이 포함되는 경향이 있습니다. 으로 세로로 분할 할 수 있기 때문에 이중 너비 터미널 창이있는 경우 더 잘 작동 :vs하므로 단일 탭에서 한 번에 4 개의 파일을 쉽게 표시 할 수 있습니다.

마지막 팁 : 나는 가끔 "switch buffer"명령 ( :sb <partial_buf_name>)을 사용하는데, 이것은 TM의 퍼지 발견과 비슷하다. 그것은 꽤 잘 작동하지만 이미 열린 버퍼에 대해서만 작동한다는 점이다. 한 번에 12 개 이상의 파일을 열 때 여전히 좋습니다. (NB :set switchbuf=usetab내 vimrc에서 이것이 탭에서 작동해야했지만 일단 설정되면 훌륭합니다.)


Check out the plugin ctrlp as an alternative to the plugin Command-T. It's better than this in that it is a 'native' Vim script and doesnt need Vim to be compiled with ruby (or other support) while it runs very fast and unobtrusive.


If the file that you want is already loaded into a buffer (which is likely if you are flipping between a handful of files regularly) then you can quickly switch to the buffer with the :b[uffer] command.

:b can either take a buffer number as a parameter, or (more usefully) a string that matches part of the path/filename. If there are multiple matches you can use tab to cycle through them.

If you want to split the window and open the buffer in the new window then use :sb name

If you want to open the buffer in a new tab then use :tab b name

You can also use the :ls command to see a list of currently loaded buffers.


It is surprising to see that :find command has not been mentioned in any of the answers. I usually work with rails projects where I need to drill down deep into the directory hierarchy. So for a file application.css, located in app/assets/stylesheets/ all I have to type is :find applica and then press tab to autocomplete and enter to open.


I use this function for finding files in subdirectories, I didn't write it though. It's from the vim wiki:

function! Find(name) 
    let l:list=system("find . -name '".a:name."' | grep -v \".svn/\" | perl -ne 'print \"$.\\t$_\"'") 
    let l:num=strlen(substitute(l:list, "[^\n]", "", "g")) 
    if l:num 0 
            echo "Not a number" 
            return 
        endif 
        if l:inputl:num 
            echo "Out of range" 
            return 
        endif 
        let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*") 
    else 
      let l:line=l:list 
    endif 
    let l:line=substitute(l:line, "^[^\t]*\t./", "", "") 
    execute ":e ".l:line 
endfunction 

command! -nargs=1 Find :call Find("") 

One thing I like is it has support for wildcards (*). It's also well behaved for multiple matches.


In addition to the wildmenu answers, I use BufExplorer plugin, and the following mappings to quickly jump buffers:

nmap <A-1> :b 1<CR>
nmap <A-2> :b 2<CR>
nmap <A-3> :b 3<CR>
...
nmap <A-0> :b 10<CR>
nmap <A-=> \bs

Or you can just replace the buffer explorer with the :ls command, which basically display the same thing.

The last one maps the Alt-= to show the buffer explorer.

I don't use Windows that much, as I generally have one or two windows only.

I also modified the statusline to include the buffer number %2n, so that the I always know which buffer is being edited. See :h statusline


do you know MacVim application? It is VIM polished for OS X, and one of its feature is support for tabs (CMD+T to open new tab), it have drawer, etc... (read this: http://zerokspot.com/weblog/2008/08/03/macvim-as-textmate-replacement/)

Try it!

Regards Michal


NerdTree is best for file navigation in vim.

The link below is a good introduction to nerdtree.

http://www.catonmat.net/blog/vim-plugins-nerdtree-vim/


Here's something fairly trivial that I stick in my .vimrc. It will load a file from any subdirectory.

" load from an unspecified subdirectory 
function! LoadFromSubdirectory(filespec) 
  let filename = glob("`find . -name '" . a:filespec . "' -print`") 
  exe ':e '.filename 
endfunction 
:command -nargs=1 Er :call LoadFromSubdirectory(<f-args>) 

So I can just say:

:Er Main.java

and it will search subdirectories, find Main.java and load it. It's not well behaved for multiple files of the same name, but it's quick and simple.

The other tool I use is VTreeExplorer, which provides a tree-based window of files/directories, and doesn't require any special build options from VIM (i.e. it's a vanilla plugin).


My workflow for finding files is the wildmenu, autocwd and :e.

Relevant parts in the .vimrc:

set wildmenu
set wildignore=*.dll,*.o,*.pyc,*.bak,*.exe,*.jpg,*.jpeg,*.png,*.gif
set wildmode=list:full
set autochdir
let g:netrw_list_hide='^\.,.\(pyc\|pyo\|o\)$'

And how to use it:

:e myf^D

That gives all the files in the current directory that start with myf. You can also <Tab> through them. Use return to open :)

^D will always give you all the matches. Because autocmd always goes to the folder of the current file it's easy to work with. If you are in foo/bar.py and you want to go to foo/baz.py you only do :e baz.py and you're there. That also works with buffers (:b foo^D lists all buffers starting with foo)

참고URL : https://stackoverflow.com/questions/1445992/vim-file-navigation

반응형