类似FZF的方式快速进入一个目录?在 Vim FZF 中包含隐藏文件?
FZF-like way to quickly change into a directory? Include hidden files in Vim FZF?
我正在寻找一种方法来快速将我的 Vim 目录更改为我通过 FZF 类型的方式找到的目录。我知道它代表 Fuzzy File Searcher,而不是 Fuzzy Directory Searcher,但我确信有一个类似的工具或一个隐藏的功能可以使用并从如此快速的目录切换中受益。
示例:
我想去的目录:~/Notes/Class 1/04-24-2020
我想输入的内容:
:FZF class 1 04 24
提前致谢!
奖金问题:我如何将隐藏目录包含到其中?如果我想编辑隐藏在 .config 中的 init.vim 配置文件,我将如何传递包含它的参数?
编辑:奖金问题的答案写在下面。
找到 "including hidden files" 的答案。
在下面的示例中,我使用的是 fdfind。随意使用任何你喜欢的东西!
command! -nargs=? -complete=dir AF
\ call fzf#run(fzf#wrap(fzf#vim#with_preview({
\ 'source': 'fdfind --type f --hidden --follow --exclude .git --no-ignore . '.expand(<q-args>)
\ })))
上面的代码你会做得很好,但如果你正在寻找更丰富的配置添加,这里是:
" Terminal buffer options for fzf
autocmd! FileType fzf
autocmd FileType fzf set noshowmode noruler nonu
" nnoremap <silent> <Leader><Leader> :Files<CR>
nnoremap <silent> <expr> <Leader><Leader> (expand('%') =~ 'NERD_tree' ? "\<c-w>\<c-w>" : '').":Files\<cr>"
nnoremap <silent> <Leader>C :Colors<CR>
nnoremap <silent> <Leader><Enter> :Buffers<CR>
nnoremap <silent> <Leader>L :Lines<CR>
nnoremap <silent> <Leader>ag :Ag <C-R><C-W><CR>
nnoremap <silent> <Leader>AG :Ag <C-R><C-A><CR>
xnoremap <silent> <Leader>ag y:Ag <C-R>"<CR>
nnoremap <silent> <Leader>` :Marks<CR>
" nnoremap <silent> q: :History:<CR>
" nnoremap <silent> q/ :History/<CR>
" inoremap <expr> <c-x><c-t> fzf#complete('tmuxwords.rb --all-but-current --scroll 500 --min 5')
imap <c-x><c-k> <plug>(fzf-complete-word)
imap <c-x><c-f> <plug>(fzf-complete-path)
inoremap <expr> <c-x><c-d> fzf#vim#complete#path('blsd')
imap <c-x><c-j> <plug>(fzf-complete-file-ag)
imap <c-x><c-l> <plug>(fzf-complete-line)
function! s:plug_help_sink(line)
let dir = g:plugs[a:line].dir
for pat in ['doc/*.txt', 'README.md']
let match = get(split(globpath(dir, pat), "\n"), 0, '')
if len(match)
execute 'tabedit' match
return
endif
endfor
tabnew
execute 'Explore' dir
endfunction
command! PlugHelp call fzf#run(fzf#wrap({
\ 'source': sort(keys(g:plugs)),
\ 'sink': function('s:plug_help_sink')}))
function! RipgrepFzf(query, fullscreen)
let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
let initial_command = printf(command_fmt, shellescape(a:query))
let reload_command = printf(command_fmt, '{q}')
let options = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
if a:fullscreen
let options = fzf#vim#with_preview(options)
endif
call fzf#vim#grep(initial_command, 1, options, a:fullscreen)
endfunction
command! -nargs=* -bang RG call RipgrepFzf(<q-args>, <bang>0)
我 wrote a function 我保存在我的 .bashrc 中,你可以用它 select
通过 fzf 的任何文件,并将它们传递给您想要的任何程序。它
适用于 vlc、evince 等 GUI 程序和命令行工具
如 cd、cat、tail、head 等。您也可以循环返回您的
历史并找到命令,因为它在 fzf 完成它的事情后被扩展。
在您的情况下,您只需在终端中输入:
f cd
fzf 将启动,在您 select 您的目录之后,您将立即 cd 到那里。
我把函数放在下面,得到了灵感here
# Run command/application and choose paths/files with fzf.
# Always return control of the terminal to user (e.g. when opening GUIs).
# The full command that was used will appear in your history just like any
# other (N.B. to achieve this I write the shell's active history to
# ~/.bash_history)
#
# Usage:
# f cd (hit enter, choose path)
# f cat (hit enter, choose files)
# f vim (hit enter, choose files)
# f vlc (hit enter, choose files)
f() {
# if no arguments passed, just lauch fzf
if [ $# -eq 0 ]
then
fzf
return 0
fi
# Store the program
program=""
# Remove first argument off the list
shift
# Store any option flags
options="$@"
# Store the arguments from fzf
arguments=$(fzf --multi)
# If no arguments passed (e.g. if Esc pressed), return to terminal
if [ -z "${arguments}" ]; then
return 1
fi
# Sanitise the command by putting single quotes around each argument, also
# first put an extra single quote next to any pre-existing single quotes in
# the raw argument. Put them all on one line.
for arg in "${arguments[@]}"; do
arguments=$(echo "$arg" | sed "s/'/''/g; s/.*/'&'/g; s/\n//g")
done
# If the program is on the GUI list, add a '&'
if [[ "$program" =~ ^(nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
arguments="$arguments &"
fi
# Write the shell's active history to ~/.bash_history.
history -w
# Add the command with the sanitised arguments to .bash_history
echo $program $options $arguments >> ~/.bash_history
# Reload the ~/.bash_history into the shell's active history
history -r
# execute the last command in history
fc -s -1
}
我正在寻找一种方法来快速将我的 Vim 目录更改为我通过 FZF 类型的方式找到的目录。我知道它代表 Fuzzy File Searcher,而不是 Fuzzy Directory Searcher,但我确信有一个类似的工具或一个隐藏的功能可以使用并从如此快速的目录切换中受益。
示例: 我想去的目录:~/Notes/Class 1/04-24-2020
我想输入的内容:
:FZF class 1 04 24
提前致谢!
奖金问题:我如何将隐藏目录包含到其中?如果我想编辑隐藏在 .config 中的 init.vim 配置文件,我将如何传递包含它的参数?
编辑:奖金问题的答案写在下面。
找到 "including hidden files" 的答案。
在下面的示例中,我使用的是 fdfind。随意使用任何你喜欢的东西!
command! -nargs=? -complete=dir AF
\ call fzf#run(fzf#wrap(fzf#vim#with_preview({
\ 'source': 'fdfind --type f --hidden --follow --exclude .git --no-ignore . '.expand(<q-args>)
\ })))
上面的代码你会做得很好,但如果你正在寻找更丰富的配置添加,这里是:
" Terminal buffer options for fzf
autocmd! FileType fzf
autocmd FileType fzf set noshowmode noruler nonu
" nnoremap <silent> <Leader><Leader> :Files<CR>
nnoremap <silent> <expr> <Leader><Leader> (expand('%') =~ 'NERD_tree' ? "\<c-w>\<c-w>" : '').":Files\<cr>"
nnoremap <silent> <Leader>C :Colors<CR>
nnoremap <silent> <Leader><Enter> :Buffers<CR>
nnoremap <silent> <Leader>L :Lines<CR>
nnoremap <silent> <Leader>ag :Ag <C-R><C-W><CR>
nnoremap <silent> <Leader>AG :Ag <C-R><C-A><CR>
xnoremap <silent> <Leader>ag y:Ag <C-R>"<CR>
nnoremap <silent> <Leader>` :Marks<CR>
" nnoremap <silent> q: :History:<CR>
" nnoremap <silent> q/ :History/<CR>
" inoremap <expr> <c-x><c-t> fzf#complete('tmuxwords.rb --all-but-current --scroll 500 --min 5')
imap <c-x><c-k> <plug>(fzf-complete-word)
imap <c-x><c-f> <plug>(fzf-complete-path)
inoremap <expr> <c-x><c-d> fzf#vim#complete#path('blsd')
imap <c-x><c-j> <plug>(fzf-complete-file-ag)
imap <c-x><c-l> <plug>(fzf-complete-line)
function! s:plug_help_sink(line)
let dir = g:plugs[a:line].dir
for pat in ['doc/*.txt', 'README.md']
let match = get(split(globpath(dir, pat), "\n"), 0, '')
if len(match)
execute 'tabedit' match
return
endif
endfor
tabnew
execute 'Explore' dir
endfunction
command! PlugHelp call fzf#run(fzf#wrap({
\ 'source': sort(keys(g:plugs)),
\ 'sink': function('s:plug_help_sink')}))
function! RipgrepFzf(query, fullscreen)
let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case %s || true'
let initial_command = printf(command_fmt, shellescape(a:query))
let reload_command = printf(command_fmt, '{q}')
let options = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
if a:fullscreen
let options = fzf#vim#with_preview(options)
endif
call fzf#vim#grep(initial_command, 1, options, a:fullscreen)
endfunction
command! -nargs=* -bang RG call RipgrepFzf(<q-args>, <bang>0)
我 wrote a function 我保存在我的 .bashrc 中,你可以用它 select 通过 fzf 的任何文件,并将它们传递给您想要的任何程序。它 适用于 vlc、evince 等 GUI 程序和命令行工具 如 cd、cat、tail、head 等。您也可以循环返回您的 历史并找到命令,因为它在 fzf 完成它的事情后被扩展。
在您的情况下,您只需在终端中输入:
f cd
fzf 将启动,在您 select 您的目录之后,您将立即 cd 到那里。
我把函数放在下面,得到了灵感here
# Run command/application and choose paths/files with fzf.
# Always return control of the terminal to user (e.g. when opening GUIs).
# The full command that was used will appear in your history just like any
# other (N.B. to achieve this I write the shell's active history to
# ~/.bash_history)
#
# Usage:
# f cd (hit enter, choose path)
# f cat (hit enter, choose files)
# f vim (hit enter, choose files)
# f vlc (hit enter, choose files)
f() {
# if no arguments passed, just lauch fzf
if [ $# -eq 0 ]
then
fzf
return 0
fi
# Store the program
program=""
# Remove first argument off the list
shift
# Store any option flags
options="$@"
# Store the arguments from fzf
arguments=$(fzf --multi)
# If no arguments passed (e.g. if Esc pressed), return to terminal
if [ -z "${arguments}" ]; then
return 1
fi
# Sanitise the command by putting single quotes around each argument, also
# first put an extra single quote next to any pre-existing single quotes in
# the raw argument. Put them all on one line.
for arg in "${arguments[@]}"; do
arguments=$(echo "$arg" | sed "s/'/''/g; s/.*/'&'/g; s/\n//g")
done
# If the program is on the GUI list, add a '&'
if [[ "$program" =~ ^(nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
arguments="$arguments &"
fi
# Write the shell's active history to ~/.bash_history.
history -w
# Add the command with the sanitised arguments to .bash_history
echo $program $options $arguments >> ~/.bash_history
# Reload the ~/.bash_history into the shell's active history
history -r
# execute the last command in history
fc -s -1
}