man fzf)Today, using rg and fzf, I can select/filter files based on contents of the file. Once I select them, when I open, the file's seek position is at the beginning. Is it possible to open the file with seek position at the matched file content line number? Some thing similar to: "vim +linenumber selected_file"
I am using the below example to filter the files:
ff or ff <folder>.#
[[ -n $1 ]] && cd $1 # go to provided folder or noop
RG_DEFAULT_COMMAND="rg -i -l --ignore-file ~/.ignore --hidden"
selected=$(
FZF_DEFAULT_COMMAND="$RG_DEFAULT_COMMAND --files" fzf \
-m \
-e \
--ansi \
--phony \
--reverse \
--preview-window=:hidden \
--bind '?:toggle-preview' \
--bind "ctrl-a:select-all" \
--bind "change:reload:$RG_DEFAULT_COMMAND {q} || true" \
--preview "rg -i --pretty --context 2 {q} {}" | cut -d":" -f1,2
)
[[ -n $selected ]] && vim $selected # open multiple files in editor
Hey this is is less a fzf thing and more of a vim question however but there should be a way to do it I’ll have a look once i I’m home but you might want to try opening your editor with a bind which will allow you to execute by {+} instead of defaulting to should command
Hey so this should work for you
http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html
Fzf —bind=“f9:ececute:vim -c ‘pattern’ temp.txt”
Fzf —bind=“F9:execute:vim -c 'execute "normal /pattern\n"' filename’”
Fzf —bind=“F9:execute:vim -s <(printf 'Query\n') tmp.txt”
Thanks a lot for the suggestion and help. Let me try.
hey so I was taking those from the vim help files however I tested them and there's something screwy with them being run from fzf but im working on it :) * this is when i try to run {q} as a pattern and {} as a file with the --phony flag.
are you using vim or neovim and are you looking to edit the files in place or just view specific content
So apparently my problem was trying to use ‘vim’ with a highly neovim setup but still the vim command I got to work was —bind=“key:execute:vim -c ‘%g/query/echo” {}” the reason for echo is I found that the vim command was reading the first / as a continuation of a substitution I wanted to get it working with {q} but couldn’t get it to work so I’ll probably end up setting it as a variable in a script but it only matches the first matching line but it should work this time.
are you using vim or neovim and are you looking to edit the files in place or just view specific content
I am using vim. I was trying to use this to view specific content while browsing through the code and do search.
I found an alternative to get this done: Change the options to rg little bit and do some processing of the results as shown below:
[[ -n $1 ]] && cd $1 # go to provided folder or noop
RG_DEFAULT_COMMAND="rg -i --no-heading --line-number --color always --ignore- file ~/.ignore --hidden"
selected=$(
FZF_DEFAULT_COMMAND="$RG_DEFAULT_COMMAND --files" fzf \
-m \
-e \
--ansi \
--phony \
--reverse \
--preview-window=:hidden \
--bind '?:toggle-preview' \
--bind "ctrl-a:select-all" \
--bind "ctrl-t:execute-silent:(vim {+})" \
--bind "change:reload:$RG_DEFAULT_COMMAND {q} || true" \
--preview "rg -i --pretty --context 2 {q} {}" | cut -d":" -f1,2 | awk -F":" '{ printf("%s +%s\n",$1,$2) }'
)
[[ -n $selected ]] && vim $selected # open multiple files in editor
So apparently my problem was trying to use ‘vim’ with a highly neovim setup but still the vim command I got to work was —bind=“key:execute:vim -c ‘%g/query/echo” {}” the reason for echo is I found that the vim command was reading the first / as a continuation of a substitution I wanted to get it working with {q} but couldn’t get it to work so I’ll probably end up setting it as a variable in a script but it only matches the first matching line but it should work this time.
Let me try this as well.
Yea the reason I tend not to use fzf default cmd is because it only triggers if there is no standard I put which makes it less versatile/ usable in more complex scripts :”
I t
So apparently my problem was trying to use ‘vim’ with a highly neovim setup but still the vim command I got to work was —bind=“key:execute:vim -c ‘%g/query/echo” {}” the reason for echo is I found that the vim command was reading the first / as a continuation of a substitution I wanted to get it working with {q} but couldn’t get it to work so I’ll probably end up setting it as a variable in a script but it only matches the first matching line but it should work this time.
Let me try this as well.
--bind "ctrl-g:execute:vim -c "%g/query/echo" {}"
I tried the above suggestion. It works if I press
However, If I trigger it using "ctrl-g" bindkey, vim complains about: "Vim: Warning: Output is not to a terminal"
I t
So apparently my problem was trying to use ‘vim’ with a highly neovim setup but still the vim command I got to work was —bind=“key:execute:vim -c ‘%g/query/echo” {}” the reason for echo is I found that the vim command was reading the first / as a continuation of a substitution I wanted to get it working with {q} but couldn’t get it to work so I’ll probably end up setting it as a variable in a script but it only matches the first matching line but it should work this time.
Let me try this as well.
--bind "ctrl-g:execute:vim -c "%g/query/echo" {}"
I tried the above suggestion. It works if I press .
However, If I trigger it using "ctrl-g" bindkey, vim complains about: "Vim: Warning: Output is not to a terminal"
I meant "I tried the above suggestion. It works if I press enter"
Oh that’s easy to fix and harmless it’s just letting you know it’s running from script just add the flag —not-a-term to your vim command
--bind "ctrl-g:execute:vim --not-a-term -c "%g/query/echo" {}"
If I press ctrl-g now, it shows blank.
Need to put a : inside the “” before the %g that’s my bad sry
The c flag lets you execute vim commands as if you were In vim :%g/foo/echo should be correct
It’s a pretty great command that you chat even technically call vim plugins from the terminal (though to different degrees of success
Also for what you have there a double quote within another double quote actually unquotes you’ll want single quote the ‘:g/choice/echo’ and keep the doubles on the outside also I noticed your using mac what version of bash are you running(by default Osx uses bash 3.2)
here you go a better solution (i hope)
#!/bin/bash
__query="$1"
echo "$__query"
fzvo () {
__query "$@"
fzf --preview='cat {}' \
--bind="f9:execute:vim --not-a-term -c ':g/${__query}/#' {}"
}
fzvo "$@" < <(fd -uu -t f -d 2)
In case your Vim doesn't support --not-a-term option, you can redirect /dev/tty like so:
fzf --bind="f9:execute:vim {} < /dev/tty"
Nice didnt think of that 👍
Most helpful comment
here you go a better solution (i hope)