Sample Header Ad - 728x90

Multiple selection with fzf and vim having new lines in path/file names

3 votes
1 answer
219 views
I have a Bash function that follows the [fzf](https://github.com/junegunn/fzf/tree/master) key-bindings in [key-bindings.bash](https://raw.githubusercontent.com/junegunn/fzf/refs/heads/master/shell/key-bindings.bash) .
# shellcheck disable=SC2206
__fzf_edit_file__() {
  builtin local -a opts
  builtin local edit_file file

  opts=(
   "+m"
    --read0
    "--reverse"
    "--header-first"
    "--scheme='path'"
    '--preview-window=right:80%:wrap'
    "--height" ${FZF_TMUX_HEIGHT:-80%}
    "--walker='file,dir,follow,hidden'"
    "--preview 'bat --color=always --wrap never {}'"
    "--header='Key: [CTRL-F] Select, preview and edit file(s) in [${PWD}]'."
    --bind='ctrl-z:ignore,ctrl-a:toggle-all,ctrl-s:toggle-preview,ctrl-d:preview-down,ctrl-u:preview-up'
  )
  # shellcheck disable=SC2091
   builtin exec {edit_file}CTRL+F

 • Use arrow keys to move up and down or CTRL+J, CTRL+K

 • CTRL+D to scroll the file contents (or mouse scrolling, if allowed)
 
 • CTRL+S to toggle preview window.

 • Enter/return key to select
 
 • CTRL+A  to toggle all from fzf, but that is disabled now because of the +m
 
 • TAB  to select multiple files, also disable because of the +m

 • ESC to exit.

---

That works as expected; the issue is when selecting multiple (with the TAB key) file/path names
with newlines embedded. The +m option allows only a single selection. It used to be -m and mapfile
instead of read to allow multiple file selections, but that causes a problem.
vim opens a new file that contains all the file or path names, including embedded newlines.
How can I fix that issue? I checked vim; doesn't appear to support null delimited input like -z.
As far as I know, vim can handle spaces/tabs/newlines in path/file names with:
files=(*.txt) vim -- "${files[@]}"
---
The version that works for multiple files selection, but fails with newlines.


- Remove the --read0
-  Change the +m to -m

And use mapfile
LC_ALL=C IFS= builtin mapfile -u"$edit_file" file ((${#file[*]})) && { vim -- "${file[@]%?}" } ``` ---
Asked by Jetchisel (1544 rep)
May 7, 2025, 09:05 PM
Last activity: May 18, 2025, 01:18 AM