Vim Shortcuts: Explore Files

Vim Shortcuts: Explore Files

Vim is one of the most powerful and customizable text editors, beloved by developers and sysadmins alike. Beyond simple editing, Vim provides a suite of shortcuts and commands to browse directories, open multiple files, navigate buffers, and integrate with powerful fuzzy-finders. This article explores everything you need to know about exploring files in Vim, from built-in netrw to modern plugins like fzf.

1. Built-in File Browsing with netrw

Vim’s netrw plugin is shipped by default. It allows users to browse file systems, open files, and perform file operations without leaving Vim.

1.1 Opening netrw

  • :Explore or :E — Opens the directory of the current file.
  • :Explore ltpathgt — Opens the specified path.
  • :Sexplore or :Sex — Opens horizontally split netrw.
  • :Vexplore or :Vex — Opens vertically split netrw.

1.2 netrw Key Bindings

Key Action
o Open file or directory
i Open in split
t Open in new tab
Go to parent directory
% Create a new file
d Create a new directory

1.3 Custom netrw Settings

Add the following to your .vimrc to make netrw more streamlined:

let g:netrw_liststyle=3    Tree listing
let g:netrw_banner=0       Disable banner
let g:netrw_browse_split=4  Open in prior window
  

2. Buffers and Windows: Navigating Open Files

Vim distinguishes buffers (open file handles) and windows (viewports). Efficient workflows rely on moving between them quickly.

2.1 Buffer Commands

  • :ls or :buffers — List all buffers.
  • :bnext (:bn) — Go to next buffer.
  • :bprevious (:bp) — Go to previous buffer.
  • :buffer ltNgt — Jump to buffer number N.

2.2 Window Navigation

  • Ctrl-w h/j/k/l — Move to left/down/up/right window.
  • Ctrl-w w — Toggle between windows.
  • :split ltfilegt — Horizontal split.
  • :vsplit ltfilegt — Vertical split.

3. Fuzzy File Finders

For large projects, fuzzy-finders like fzf, ctrlp.vim, and LeaderF transform file exploration.

3.1 fzf

fzf is a general-purpose command-line fuzzy finder. With junegunn/fzf.vim, you get :Files to search and open files:

  • :Files — Fuzzy search project files.
  • :Buffers — Fuzzy switch buffers.
  • :GFiles — Search tracked Git files.

Installation (using vim-plug):

Plug junegunn/fzf, { do: ./install --all }
Plug junegunn/fzf.vim
  

3.2 ctrlp.vim

ctrlp.vim indexes your project directory and provides p by default:

  • p — Open the CtrlP window.
  • pf — Files only.
  • pp — Buffers only.
  • pb — MRU list.

4. Searching Within Files

Beyond opening files, you often need to search content. Vim’s built-in :grep integrates well with external tools like rg (Ripgrep).

4.1 grep and makeprg

Configure Vim to use rg:

set grepprg=rg --vimgrep --smart-case
set grepformat=%f:%l:%c:%m
  

Then:

  • :grep ltpatterngt ltfilesgt — Populate quickfix list.
  • :copen — Open quickfix window.
  • :cnext/:cprev — Navigate results.

5. Tag Navigation with ctags

ctags generates a symbol index for jumping around codebases.

  • ctags -R . — Generate tags file for current project.
  • :tag ltsymbolgt — Jump to symbol definition.
  • Ctrl-] — Jump to tag under cursor.
  • Ctrl-t — Jump back in tag stack.

6. Workflow Tips

  1. Start with a fuzzy finder: Quickly locate files with fzf or ctrlp.
  2. Leverage buffers: Keep files open and cycle with :bn/:bp.
  3. Use splits sparingly: Open related files side by side (header/source).
  4. Navigate projects: Mount remote directories via SSHFS and browse with netrw.
  5. Automate: Write custom mappings in your .vimrc for repetitive tasks.

7. Sample .vimrc Snippet

 netrw
let g:netrw_liststyle=3
let g:netrw_banner=0
let g:netrw_browse_split=4

 FZF
Plug junegunn/fzf, { do: ./install --all }
Plug junegunn/fzf.vim
nnoremap f :Files

 Buffer navigation
nnoremap bn :bnext
nnoremap bp :bprevious

 Ripgrep integration
set grepprg=rg --vimgrep --smart-case
set grepformat=%f:%l:%c:%m
nnoremap g :grep

 Tags
set tags=./tags,tags
nnoremap ] :tag
  

Conclusion

Mastering Vim’s file exploration features, from netrw to modern fuzzy finders, dramatically increases your productivity. Familiarize yourself with these shortcuts and plugins, tailor your .vimrc, and build a workflow that keeps you in the editor, focused on coding rather than context-switching.

  • Hola 👋 , soy la IA de Linuxmind.dev, te puedo ayudar a aprender.
Gathering thoughts ...

Leave a Reply

Your email address will not be published. Required fields are marked *