Delete a Line From Another Line

Today I was cleaning up a test with an extra empty line at the top of the file, away from my Vim cursor. I wanted to delete it… without moving the cursor. It seems like Vim almost lets you do this, with :[range]d. But it leaves the cursor on the deleted line, which isn’t very magical. This is the hack we found: :[range]d Then: '' '' returns the cursor to the last jump point. ...

November 9, 2015

Explore Buffers with BufExplorer

I’m a huge fan of Vim buffers. I try to have my buffer list roughly mirror the files I am currently holding in my brain. The BufExplorer Vim plugin helps, and is included in the Hashrocket Dotmatrix. Today I learned to use the command \bs, which opens a colorful buffer list that you can navigate with Vim directions. https://github.com/jlanzarotta/bufexplorer

October 12, 2015

Edit the Current File Always

The Vim command :e edits a file. Add a bang, :e!, to edit the current file ‘always’, discarding any changes to the current buffer. This is useful if you rename a file with a new file extension (i.e. .txt to .rb). :e! reloads the file in Vim, picking up any customizations you have in your configuration files such as syntax highlighting.

May 29, 2015

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

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

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

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

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

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

Delete Comments

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

April 18, 2015

Don’t miss my next essay

Hear from me immediately when I post: no ads, unsubscribe anytime.