Monday, August 24, 2015

Handy shortcuts for Vim

Here are a couple Bash shortcuts that have ended up being fairly useful for me.

Typically if I'm on a Linux command line, I'll need to open files by either the content of the file, or partitial file names.  For example,  I might want to quickly open  all the files that contain the string "Foo."   That, or I don't want to type a/very/long/path/to/some_file.txt.   I'll end up piping the output of `find` or `grep` into an editor repeatedly.  So I just made a couple short aliases that save on typing.    I'm not sure why I hadn't added these years ago :-)

The shortcuts look at everything under the current directory.

Of course, you can customize these to work with any editors or filetypes you are interested in... these are just examples.  

Drop these in your .bash_profile file:

# vim + grep = vig
# Open all files containing a particular string

# For example, to oen all files containing content "Foo", use
#     vig Foo
function vig(){ vim `grep -Ril --exclude='*.class' "$1"`; }

# vim + find = vif
# Open all files where the name contains a string.  
# For example, to open all files named "Foo...", use
#     vif  Foo
function vif(){ vim `find -iname "$1*"`; }

# vim + java = vij
# Open all files matching a class name
# For example, find all classes named, use
#     vij Foo
function vij(){ vim `find -name "$"`; }

No comments: