Navigating a Codebase

File Switching

by Guy Routledge | @guyroutledge

Slides:
https://talks.guyroutledge.co.uk/switching-files/slides/

The Problem:

File Switching

 

Working with a codebase, this is something you do a LOT.

Anything you do a lot, you want to do FAST.

The Solution:

Search and Switch

Remembering every file name or path is a waste of time

Searching is the quickest way to find what you're looking for

 

But remembering what to search for is also a waste of time...

Fuzzy Search

In computer science, approximate string matching (often colloquially referred to as fuzzy string searching) is the technique of finding strings that match a pattern approximately (rather than exactly)

 

Wikipedia: Approximate String Matching

How does it work?

A series of operations determines the closeness of a match

Insertion:         co*t → coat
Deletion:          coat → co*t
Substitution:      coat → cost
Transposition:     cost → cots

What does this mean?

You can find files at the speed of thought

You don't even need to remember the full path or filename

Sometimes just a file extension could be enough...

How can I use this in Vim?

Let's look at three plugins that use fuzzy search

Give a quick demo of how they work

Look at their pros and cons

Peepopen

https://peepcode.com/products/peepopen

What is it?

A Mac app that hooks into your text editor

Supports MacVim, Xcode, Textmate, Coda, Emacs

GUI interface for searching and opening files

What does it do?

Fuzzy search

Opens files in new tab/current window

Displays SCM metadata

peeopoen screenshot

Commands

Launch peepopen:     <leader>p
Next item:           <Up>
Previous item:       <Down>
Open file:           <CR>
Close peepopen:      <ESC>

Live Demo

Peepopen

thumbs up

Pros

Easy install

Nice GUI interface

Smart sorting

Shows SCM metadata

thumbs down

Cons

Limited options

Navigation with arrow keys

Costs cash money

CommandT

https://github.com/wincent/Command-T

What is it?

A Vim plugin written in Ruby and C

Inspired by the "Goto File" feature of Textmate

What does it do?

Fuzzy search of folders

Fuzzy search of open buffers

Opens files in:

  • Current window
  • New tab
  • In a split window
  • In a vertical split
commandT screenshot

Commands

Launch CommandT:          <leader>t
Launch CommandT:          :CommandT

Launch buffer search:     <leader>b
Launch buffer search:     :CommandTBuffer

Next item:                <C-j>
Previous item:            <C-k>
Open file:                <CR>
Open in split:            <C-s>
Open in Vsplit:           <C-v>
Open in Tab:              <C-t>
Close CommandT:           <ESC>

 

There are multiple mappings for movement and closing

Configuration

If you use EasyMotion, you'll want to remap CommandT

There are lots of configurable options too

I have these lines in my .vimrc

 

nnoremap <silent> <C-e> :CommandT<CR>
nnoremap <silent> <C-b> :CommandTBuffer<CR>
let g:CommandTMaxHeight=25
let g:CommandTMatchWindowReverse=1

Live Demo

CommandT

thumbs up

Pros

Fast

Customisable

Accurate results

thumbs down

Cons

Installation was fiddly

CtrlP

https://github.com/kien/ctrlp.vim

What is it?

A pure vimscript fuzzy finder

What does it do?

Fuzzy search of files

Fuzzy search of open buffers

Fuzzy search of MRU files

Fuzzy search of tags

Opens multiple files

Regex mode

Run commands after opening

Opens files in:

  • Current window
  • New tab
  • In a split window
  • In a vertical split
ctrlP screenshot

Commands

Launch CtrlP:             :CtrlP
Launch buffer search:     :CtrlPBuffer
Launch MRU search:        :CtrlPMRU

Cycle modes:              <C-b> and <C-f>
Next item:                <C-j>
Previous item:            <C-k>
Open file:                <CR>
Open in split:            <C-x>
Open in Vsplit:           <C-v>
Open in Tab:              <C-t>
Mark multiple files:      <C-z> and open with <C-o>
Create new file:          <C-y> also creates parent directories
Close CtrlP:              <ESC>

 

You can end the input with a colon to execute commands - such as open a file at a certain line number

Configuration

let g:ctrlp_map = '<C-p>'
let g:ctrlp_cmd = 'CtrlP'

let g:ctrlp_working_path_mode = 'ra'

For more detailed information about setting the smart project root finder, check out the readme on github

Extend

A small handful of plugins have been written for CtrlP

screenshot of ctrl-p funky

Search for function definitions without needing ctags

Live Demo

CtrlP

thumbs up

Pros

Easy install

Pure vimscript

Multiple search modes

Regex support

Multiple file opening

thumbs down

Cons

Slower than CommandT

Hit and miss accuracy

Too many features?

Bonus Feature:

Lusty Explorer

https://github.com/sjbach/lusty

What is it?

A different take on fuzzy searching and file switching

More of a browser, less emphasis on speed

Commands

<Leader>lf       Opens file explorer
<Leader>lr       Opens file explorer at the dir of current file
<Leader>lb       Opens buffer explorer
<Leader>lg       Opens buffer grep

Live Demo

Lusty Explorer

thumbs up

Pros

Easily browse your files, buffers and folders

Buffer grep is very cool

thumbs down

Cons

Slower file switching

Opens files in current window

Conclusions?

Got any questions?

http://www.guyroutledge.co.uk | @guyroutledge

 

Slides:
https://talks.guyroutledge.co.uk/switching-files/slides/