Find and Replace Across Files

Vim can find and replace strings across files, just like other text editors. It’s really (sort of) easy. First load all the files you want to change into the buffer with a splatted directory. :args path/to/files/*/* Then, execute the substitution. :argdo %s/old_string/new_string/ge | update The e flag is important; it tells Vim not to issue an error if the search pattern fails. This will prevent No Match errors from breaking the chain.

May 21, 2015 · 1 min · Jake Worth

Call a Vimscript Method in Vim

To call a Vimscript method in Vim, first source the file. :source ~/path/to/vimscript.vim Next, put the cursor over the method call, and run that line. :exec getline(".")

May 17, 2015 · 1 min · Jake Worth

Current Value of a Setting

Check the value of any Vim setting by adding a ? to the end of its name. # Validate spelling is turned off :set spell? => nospell # Validate incremental search is turned on :set incsearch? => :incsearch

May 14, 2015 · 1 min · Jake Worth

Jump to the First Non-Blank Character

With Vim you can jump down any n amount of lines with n + j, and back up with n + k. An alternate command is n + +, which jumps down to the first non-blank character, and n + -, which jumps back up to the first non-blank character.

May 12, 2015 · 1 min · Jake Worth

Close a File

In Vim there are many ways to close a file. One of the best is :x, also know as :xit. :x writes and quits a file when changes have been made, like :wq, but with one less keystroke. An important distinction: :x and :wq are similar, but not identical commands. The difference is that :wq writes the current file and quits, as long as the file is not read-only and has a name, while :x only writes if changes have been made. So, if you were to generate a file with Vim, make no changes, and try to write and quit with :x, the file would not be written. Doing the same thing with :wq would write the file.

May 8, 2015 · 1 min · Jake Worth

Sort Alphabetically

One way to make a list nicer to read is to sort it. Vim comes with a command built in for just this purpose. Here is a snippet from a Gemfile: gem 'coffee-rails', '~> 4.1.0' gem 'uglifier', '>= 1.3.0' gem 'sass-rails', '~> 5.0' gem 'puma' gem 'gravatar_image_tag' gem 'authem' gem 'jquery-rails' gem 'pg' gem 'redcarpet' gem 'rails_12factor', group: :production gem 'sdoc', '~> 0.4.0', group: :doc These gems might have been added during the development process. To sort them, enter visual mode, highlight the desired range, and enter :sort. Here’s the result: ...

May 6, 2015 · 1 min · Jake Worth

Delete Comments

:g/^\s*#/d will remove comment lines from a file with Vim.

April 18, 2015 · 1 min · Jake Worth