IT

Vim에서 여러 버퍼를 삭제하는 방법은 무엇입니까?

lottoking 2020. 8. 3. 17:27
반응형

Vim에서 여러 버퍼를 삭제하는 방법은 무엇입니까?


Vim에서 여러 파일을 버퍼로 열었다 고 가정합니다. 파일은이 *.cpp, *.h그리고 일부입니다 *.xml. 로 모든 XML 파일을 닫고 싶습니다 :bd *.xml. 그러나 Vim에서는이를 허용하지 않습니다 (E93 : 둘 이상의 일치 ...).

제출할 수있는 방법이 있습니까?

추신 : 나는 :bd file1 file2 file3작동 하는 것을 알고 있습니다. 그래서 어떻게 든 평가할 *.xmlfile1.xml file2.xml file3.xml있습니까?


<C-a>모든 경기를 완료 하는 사용할 수 있습니다 . 따라서 :bd *.xml다음을 입력 한 다음 <C-a>vim은에 대한 명령을 완료합니다 :bd file1.xml file2.xml file3.xml.


:3,5bd[elete]   

3에서 5까지의 버퍼 범위를 삭제합니다.


대신 사용할 수도 있습니다.

    :.,$-bd[elete]    " to delete buffers from the current one to last but one
    :%bd[elete]       " to delete all buffers

사용할 수 있습니다.

:exe 'bd '. join(filter(map(copy(range(1, bufnr('$'))), 'bufname(v:val)'), 'v:val =~ "\.xml$"'), ' ')

명령에 쉽게 추가 할 수 있습니다.

function! s:BDExt(ext)
  let buffers = filter(range(1, bufnr('$')), 'buflisted(v:val) && bufname(v:val) =~ "\.'.a:ext.'$"')
  if empty(buffers) |throw "no *.".a:ext." buffer" | endif
  exe 'bd '.join(buffers, ' ')
endfunction

command! -nargs=1 BDExt :call s:BDExt(<f-args>)

아래에서 제공합니다. 예를 들어 "txt"는 필요에 따라 "xml"로 변경하십시오. 수정 된 버퍼는 삭제되지 않습니다. 버퍼를 삭제하려는 \ bd를 누르십시오.

map <Leader>bd :bufdo call <SID>DeleteBufferByExtension("txt")

function!  <SID>DeleteBufferByExtension(strExt)
   if (matchstr(bufname("%"), ".".a:strExt."$") == ".".a:strExt )
      if (! &modified)
         bd
      endif
   endif
endfunction

[편집] : bufdo없이 동일 (Luc Hermitte가 요청 한대로 아래 주석 참조)

map <Leader>bd :call <SID>DeleteBufferByExtension("txt")

function!  <SID>DeleteBufferByExtension(strExt)
   let s:bufNr = bufnr("$")
   while s:bufNr > 0
       if buflisted(s:bufNr)
           if (matchstr(bufname(s:bufNr), ".".a:strExt."$") == ".".a:strExt )
              if getbufvar(s:bufNr, '&modified') == 0
                 execute "bd ".s:bufNr
              endif
           endif
       endif
       let s:bufNr = s:bufNr-1
   endwhile
endfunction


나도이 기능이 항상 필요했습니다. 이것이 내 vimrc에있는 솔루션입니다.

function! GetBufferList()
    return filter(range(1,bufnr('$')), 'buflisted(v:val)')
endfunction

function! GetMatchingBuffers(pattern)
    return filter(GetBufferList(), 'bufname(v:val) =~ a:pattern')
endfunction

function! WipeMatchingBuffers(pattern)
    let l:matchList = GetMatchingBuffers(a:pattern)

    let l:count = len(l:matchList)
    if l:count < 1
        echo 'No buffers found matching pattern ' . a:pattern
        return
    endif

    if l:count == 1
        let l:suffix = ''
    else
        let l:suffix = 's'
    endif

    exec 'bw ' . join(l:matchList, ' ')

    echo 'Wiped ' . l:count . ' buffer' . l:suffix . '.'
endfunction

command! -nargs=1 BW call WipeMatchingBuffers('<args>')

이제 할 수 있습니다 :BW regex(예 : :BW \.cpp$경로 이름에서 해당 패턴과 일치하는 모든 일치하는 버퍼를 지).

삭제보다는 닦아하려는 경우, 당신은 물론 대체 할 수 exec 'bw ' . join(l:matchList, ' ')exec 'bd ' . join(l:matchList, ' ')


아주 간단하게 : :bd[elete]명령을 사용하십시오 . 예를 들어 :bd[elete] buf#1 buf#5 buf#3버퍼 1, 3 및 5를 삭제합니다.


TABVim 7.4.282 부터 모든 파일을 자동 완성하는
사용 <c-a>하는 파일 하나만 자동 완성 합니다.

당신은 단지 사용할 수 있습니다 :

bd filetype

그런 다음 <c-a>지정된 파일 형식의 모든 열린 파일을 쉽게 완성 하는 사용 하십시오.

예를 들어 1.xml, 2.xml, 3.xml 및 4.xml이 있으면 다음을 수행 할 수 있습니다.

bd xml

그런 다음 <c-a>

vim은 다음과 같이 자동 완성됩니다.

bd 1.xml 2.xml 3.xml 4.xml

Enter를 눌러 명령을 완료 할 수 있습니다.

위에서 언급 한 파일 중 하나를 변경 한 경우 다음을 수행해야합니다.

bd! xml

참고 URL : https://stackoverflow.com/questions/3155461/how-to-delete-multiple-buffers-in-vim

반응형