Fzf: Open file with Vim by specifying line number

Created on 17 Jun 2016  路  5Comments  路  Source: junegunn/fzf

  • Category

    • [ ] fzf binary

    • [ ] fzf-tmux script

    • [ ] Key bindings

    • [ ] Completion

    • [x] Vim

    • [ ] Neovim

    • [ ] Etc.

  • OS

    • [ ] Linux

    • [x] Mac OS X

    • [ ] Windows

    • [ ] Etc.

  • Shell

    • [ ] bash

    • [x] zsh

    • [ ] fish


Hi,

Is there any chance of making it possible to open files in Vim by specifying line (and maybe column) number?

For example, to suggest some_spec.rb file if I type some_spec.rb:30 and open it in line 30.

question

Most helpful comment

@edi9999 Did your suggestion work like a charm? Please, tell us more about that.

I achieved the goal doing:

fzf --bind "::execute(awk '{print FILENAME\":\"NR}' {} | fzf && killall fzf)"

That way I can program a function in the editor to handle this output.

BUT that killall fzf is pretty ugly.

Anyone has any idea to get rid of this?
cc: @junegunn

All 5 comments

So we have some_spec.rb in the list and you type in smspcrb:30 and enter to match the file and open it in Vim? I don't think it's possible since the moment you type :30 fzf will no longer match the file. And as fzf is just a Unix filter that does not know the context, I can't think of a good way to allow it. I would simply type in 30G once the file is open.

By the way, if you use fzf in Vim, you might also want to checkout fzf.vim project.

Or you could add a --bind with : to open a new fzf instance that lets you choose the line number

@edi9999 Did your suggestion work like a charm? Please, tell us more about that.

I achieved the goal doing:

fzf --bind "::execute(awk '{print FILENAME\":\"NR}' {} | fzf && killall fzf)"

That way I can program a function in the editor to handle this output.

BUT that killall fzf is pretty ugly.

Anyone has any idea to get rid of this?
cc: @junegunn

@romanoaugusto88 That looks interesting but I would write a shell function instead of trying to put everything inside --bind option.

#!/bin/bash
ol() {
  local lines file line
  mapfile -t lines < <(fzf --expect : --height 30% --reverse)
  [ ${#lines[@]} -ne 2 ] && return

  file=${lines[1]}
  if [ -z "${lines[0]}" ]; then
    vim "$file"
  else
    read -r -p "$file ($(wc -l "$file" | awk '{print $1}') line(s)): " line
    vim "$file" +$line
  fi
}

Alternatively you can combine abort action like follows:

fzf --bind "::execute(awk '{print FILENAME\":\"NR}' {} | fzf)+abort"

Note that due to abort the exit status of the command is non-zero.

Was this page helpful?
0 / 5 - 0 ratings