Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

So I don't want to tell you to change your workflow. Like most things that evolve over time, they are the way they are for good reason. No one way is better than another. That said, here is how I do what you described above in Vim.

There are a few components: 1) Some fancy keybindings, 2) A plugin, and 3) an optional wrapper script for launching commands inside a git project.

First, keybindings. You know about '#' and '*' in vim that search for the next occurrence of token under your carat? I have an additional binding that does the same, but doesnt move the carat. I bind it to <CR> (Enter), but you could bind it to whatever. If you bind it to <CR>, it will interfere with the minibuffer, which requires some shenanigans to work around (see the vimrc)

  nnoremap <silent><CR> :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>
This is nice for a few use cases. One, it is nice to see where a token is used in the current file, and two, it puts the highlighted word into a register, that you can later paste.

I use mhinz/vim-grepper to execute searches, with my vimrc I can move the carat to the word I am interested in, press "enter", then type :Gr<TAB><ENTER> which will put me into grepper. Then I can press ^R/ (that is ctrl-r then /) which pastes the content of the '/' register. Grepper will then launch the selected search tool (ag, grep, git grep) and create a mini buffer of matches that you can click on, or highlight and press enter. Here is my vimrc if you want to give it a go. BE WARNED the first line of the vimrc calls git clone, if this is bad on your setup for some reason, dont use my vimrc.

https://gist.github.com/SeamusConnor/d7ebb747c91845055e069f0...

The last piece is executing the search from the correct directory, to do that, I use two methods. First, vim has a built in way to change its working directory (:chdir), so you could do a :chdir ../../. Second, if you are using git it is nice to run commands at the top level of the git directory sometimes. For that I have a script that translates commands from the CWD to the git top level. Some things you can do with it:

Rebuild cmake style project:

  g make -C build && g ./build/my_program # you can execute this from anywhere inside the repo
Open a file inside vim:

  cd src/my/nested/folder/
  g vim my_source_code.cpp  # open vim at git top level directory, and translates the file to src/my/nested/folder/my_source_code.cpp
https://gist.github.com/SeamusConnor/645b66faff12ad5dbe3747f...

Anyway, hope that helps you or someone else.



Thanks for the efforts for describing this workflow, but please note my point above about being "effortless" ;)

To be a bit more specific: I need to see the structure of what I'm searching in, so at minimum, that means neotree/nerdtree. I don't have photographic memory, or alphabetic memory, I always forget the names of folders, files, everything. I just couldn't care less. "../.." means nothing to me. What is that directory? Show me, and I will point to what I want.

Also, I don't want to search in a project. I want to search in a project OR in multiple projects OR in a subdirectory within a project, ad hoc. OR just select some files and search in them.

And in my opinion, for this specific use-case there is nothing wrong with using the mouse. When I'm searching for something in the way I described above I am usually browsing the code, reading it, trying to understand what is going on. This is different to when I'm actually coding something, then vi/emacs suddenly gets much more attractive, depending on the language used :)


This is why I prefaced my response :) every one places different importance on different things. Glad you have something that works for you. Vim does show the age of its design from time to time. The worst has got to be Java, where you end up having to run eclipse in the background to make it work. Talk about a hack...




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: