Ack search, open files in vim

Tyler Tyler (271)

Posted in these interests:

23 guides
1 guide

This command will open all files that match your pattern in separate buffers in vim. In the following steps I'll break it apart and show you how to open them in tabs - if you're the kind of person who uses tabs.

This syntax is called command substitution. It takes the output of command and replaces it with the command itself. So when used in conjunction with vim it would replace this:

vim $(echo test)

with this:

vim test

And when you use the vim command with multiple parameters, it opens them each in a separate buffer.

If you're unfamiliar, ack is a grep-like text finder. You can use it like this:

ack "some pattern"

and it will recursively search all files trying to match some pattern.

When used with the -l option, it only outputs the names of the files rather than the matches themselves.

So the following code:

ack -l "codes"

Might generate output like this:

$ ack -l "body"

So all together, the output from ack -l "pattern" is passed directly into vim and opens each file in a new buffer.

Simply use vim's -p option to open files in separate tabs.

vim -p $(ack -l "pattern")
Tyler's profile pictureTyler
Joined in 2015
Software Engineer and creator of howchoo.
Share this guide!
Related to this guide:
Vim undo branches
You probably already know how to use vim's basic undo and redo features. For a quick refresher, read this short tutorial on how to use vim redo and undo.
Dayne's profile picture DayneView
In these interests: vim
3 ways to use tabs in vim
Vim tabs can be a great way to visualize which files you currently have open. Many vim users get bogged down by them and either end up working too hard to use them or give up altogether.
Dayne's profile picture DayneView
In these interests: vim
Vim redo and undo
Learn how to undo and redo changes using vim. If you are brand new to vim, learn more about the basics in this intro to vim guide.
Dayne's profile picture DayneView
In these interests: vim
People also read:
Once you learn to think in text objects, you'll never be the same
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines.
One of the strengths of Vim is that it's highly customizable. And while remapping keys is possible, we often want to create shortcuts without changing the default key bindings.
If you find yourself highlighting text in Vim with your mouse to copy and paste it elsewhere, stop. There's a better way, using the yank command, to copy text into your clipboard on macOS or Windows.
Basic vim commands
Because you've accidentally opened Vim and don't know how to exit
Vim is amazing. Well, Python is doing all the work, but I still like Vim. Learn how to format JSON in Vim like a pro with this short guide! tl;dr Paste your json, and run this in vim normal mode. :%!
Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.
Posted in these interests:
23 guides 16 subscribers 
1 guide 1 subscriber 
Discuss this guide: