Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make --with-nth strip the last delimiter #4274

Open
junegunn opened this issue Feb 23, 2025 · 2 comments
Open

Make --with-nth strip the last delimiter #4274

junegunn opened this issue Feb 23, 2025 · 2 comments

Comments

@junegunn
Copy link
Owner

junegunn commented Feb 23, 2025

#1323 (comment)

Currently, --with-nth keeps the last trailing delimiter.

echo foo,bar,baz | fzf --delimiter , --with-nth 2,1
  # bar,foo,

This behavior is usually not desirable, but now you can work around it by using a template.

Note

When you use a template, the trailing delimiter is stripped from each expression, giving you more control over the output.

echo foo,bar,baz | fzf --delimiter , --with-nth '{2},{1}'
  # bar,foo

Should we strip the last trailing delimiter by default? It means breaking backward compatibility, but you can replicate the old behavior using templates.

# Please keep the last comma!
echo foo,bar,baz | fzf --delimiter , --with-nth '{2},{1},'
  # bar,foo,

However, when rearranging markdown table columns, keeping the last delimiter is desirable because it contains whitespaces for alignment, and you can't replicate the previous behavior using templates.

fzf --reverse --delimiter ' *\| *' --with-nth ..3 << 'EOF'
| Package Manager | Linux Distribution      | Command                            |
| --------------- | ----------------------- | ---------------------------------- |
| APK             | Alpine Linux            | `sudo apk add fzf`                 |
| APT             | Debian 9+/Ubuntu 19.10+ | `sudo apt install fzf`             |
EOF

# This doesn't work
fzf --reverse --delimiter ' *\| *' --with-nth '{..3} |' << 'EOF'
| Package Manager | Linux Distribution      | Command                            |
| --------------- | ----------------------- | ---------------------------------- |
| APK             | Alpine Linux            | `sudo apk add fzf`                 |
| APT             | Debian 9+/Ubuntu 19.10+ | `sudo apt install fzf`             |
EOF
@vimpostor
Copy link

vimpostor commented Feb 23, 2025

Thanks for clearing this up. For me using the template {1} instead is an acceptable solution, and I agree with you it may do more harm if we break backwards compatibility, even if it was a better default. So maybe it's best to keep it as it is right now.

The only ugly thing is now that --with-nth and --accept-nth behave differently without templates.

vimpostor added a commit to vimpostor/vim-clue that referenced this issue Feb 23, 2025
The delimiter wasn't strictly visible before, because back in
00022ad we deliberately chose "\t" to
workaround the weird default in fzf, however it's still unnecessary to
include that whitespace in the popup.

Nowadays [0] we can simply use templates (i.e. "{1}" instead of "1") to
avoid adding the delimiter.

The concern raised in 983e744 (which
would be fixable by using the new --accept-nth option [1]) has become
invalid since 46d41c3, because now we
actually need both fields, so keeping the delimiter is important for
destructuring the fields from the response.

[0] junegunn/fzf#4274
[1] junegunn/fzf#1323
@junegunn
Copy link
Owner Author

junegunn commented Mar 3, 2025

The only ugly thing is now that --with-nth and --accept-nth behave differently without templates.

Fair enough. The inconsistency can surprise the users. My thought was there are cases, albeit niche, where it's desirable to keep the last delimiter when using --with-nth (e.g. the table example above), but no one would want it in the context of --accept-nth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants