Did you know that it is 2017 and the VIM editor still does not have a decent multi-file search and replacement mechanism?! While you can always roll your own, it's rather cumbersome, and even though some would say this isn't in the spirit of an editor such as VIM, a large community has emerged around extending it in ways to behave more like a traditional IDE.
Having written about doing something similar to this via the cmd line a while back, and having refactored a large amount of code recently that involved lots of renaming, I figured it was time to write a plugin to do just that, rename strings across source files, using grep and sed
Before we begin, it should be noted that this is of most use with a 'rooting' plugin like vim-rooter. By using this, you will ensure vim is always running in the root directory of the project you are working on, regardless of the file being modified. Thus all search & replace commands will be run relative to the top project dir.
To install vsearch, we use Vundle. Setup & installation of that is out of scope for this article, but I highly recommend familiarizing yourself with Vundle as it's the best Vim plugin management system (in my opinion).
Once Vundle is installed, using vsearch is as simple as adding the following to your ~/.vim/vimrc:
Restart Vim and run :PluginInstall to install vsearch from github. Now you're good to go!
vsearch provides two commands :VSearch and :VReplace.
VSearch simply runs grep and displays the results, without interrupting the buffer you are currently editing.
VReplace runs a search in a similar manner to VSearch but also performs and in-memory string replacement using the specified args. This is displayed to the user who is prompted for comfirmation. Upon receiving it, the plugin then executes sed and reports the results.
In the department of things I've spent far too much time working on, I just wrote a vim script that allows you to automatically insert a random number into the file you're editing. It probably could use a little work, but does what I need it to do (easily generate random numbers for test fixtures). To use it yourself, add the following to your ~/.vimrc:
" generate random number at end of current line function! s:Rand(max) y a redir @b ruby << EOF rmax = VIM::evaluate("a:max") rmax = nil if rmax == "" printf rand(rmax).to_s EOF redir END let @a = strpart(@a, 0, strlen(@a) - 1) let @b = strpart(@b, 1, strlen(@b) - 1) let @c = @a . @b .s/.*/\=@c/g endfunction command! -nargs=? Rand :call <SID>Rand(<q-args>) nmap <F6> :Rand <CR> nmap <F7> :Rand 100<CR> nmap <F8> :Rand 100000<CR>
When using vim, simply enter command mode and type :Genrand <maxnumber> to generate a random number and insert it at the end of the line. Alternatively, simply hit <F7> to generate and insert a random number between 1 and 100.
I just found the "Domestic Manners" font that I really like and feel is very good for extended text editing (terminal, coding, blogging, etc). Installing it for Fedora users is really easy:
sudo yum install dustin-domestic-manners-fonts
You can set it in vim / your vimrc with:
set guifont=Domestic\ Manners\ 8
Also you can select it in the gnome-terminal font settings.
Attached are two screenshots of the font in action.
So I've been playing around with vim to try to better my usage of the text editor and have found some very interesting features / plugins and one annoying pitfall which I want to share.
To start of, I'm no longer using the standard vim app, but rather gvim as I've been looking for better mouse support for a while. I've no need of the toolbar (I like keeping the menus around), and removing it is as simple as adding the following to your ~/.vimrc:
Using tabs is nice with the gui (albiet also present w/ the regular text version), you can create a new one with :tabnew
I've installed the showmarks plugin which is useful for visualizing the line / column markers you have set in the file you are editing. I've just also found the project plugin which I now wonder how I've even lived without it (at one point I was considering writing a standalone app just to do what this plugin does) and makes configuring projects and files like you would in an IDE a cinch (I very much recommend trying it if you use vim, it takes less than an hour to learn / completely setup).
Unfortunately one relatively new feature I wanted to try doesn't work just right in the vim version that currently ships with Fedora. Omni-completion is a very powerful feature of vim that uses ctags to provide code-completion and other functionality to users editing documents. While it comes built in for a wide variety of languages, the Fedora vim version isn't built with the necessary flags for ruby support and thus auto-completion doesn't currently work.
If you don't mind building it yourself you can simply follow the following steps:
- yum remove vim-common
- yum install rpm-build ruby ruby-devel "perl(ExtUtils::Embed)" libacl-devel gpm-devel libXpm-devel wget # there might be other dependencies, if subsequent steps complain about things missing try to yum install them
- wget http://download.fedora.redhat.com/pub/fedora/linux/development/source/SRPMS/vim-7.2.245-3.fc12.src.rpm
- Add the following line to your .rpmmacros " %_topdir ~/rpmbuild"
- rpmbuild --rebuild vim-7.2.245-3.fc12.src.rpm
- sudo yum localinstall ~/rpmbuild/RPMS/x86_64/vim-common-7.2.245-3.fc11.x86_64.rpm ~/rpmbuild/RPMS/x86_64/vim-enhanced-7.2.245-3.fc11.x86_64.rpm ~/rpmbuild/RPMS/x86_64/vim-X11-7.2.245-3.fc11.x86_64.rpm --nogpgcheck
Now vim / gvim will be installed on your system with ruby omni-completion support.