timhwang21.gitbook.io
  • Intro
  • Blog
    • 2023
    • 2022
    • 2021
    • 2020
    • 2019
  • CLI
    • Git
      • Bulk resolve merge conflicts
      • git rebase-onto
      • Patch from diff
    • Shell
      • Recipes
    • Vim
      • Batch editing
      • Buffers
      • <Ctrl-r>
      • External commands
      • :global
      • Help
      • Registers
      • Splits
    • Tools
  • Programming
    • React
      • Testing Styled Components
    • Typescript
      • curryRecord
      • Exhaustive conditionals with ADTs
      • newtype
      • OmitTypes
      • Safe JSON clone
      • Type inferrers
      • XOR type
    • Ruby
      • Pry
      • RSpec
    • Rails
      • Attributes
      • Cheap many to many
      • (Don't use) counter cache
    • Databases
      • Metrics
      • Testing indexes
  • Personal
    • Uses
    • Github
    • Medium
    • LinkedIn
    • Photography
Powered by GitBook
On this page
  • % and !
  • Mapping over files with :w
  • Buffer processing with :%:
  1. CLI
  2. Vim

External commands

% and !

% resolves to the current filename. ! calls an external function. Together, %! executes a command over the file, and replaces the current buffer with the output.

Mapping over files with :w

You can generate derived JSON from an existing file with jq, which I sometimes use to generate and manipulate test vectors:

:w !jq '.key-to-filter-by' > filtered.json

You can easily pass Markdown (or any other text format) files straight through pandoc. This is useful for exporting documentation for sharing with non-engineers.

:w !pandoc -o FILENAME.pdf

Buffer processing with :%:

Processing can be achieved by calling %! !external_fn to replace buffer contents in-place.

In this example, we pipe a JSON file through jq to prettify:

:%: !jq '.'

In this example, we convert the current file from Markdown to HTML using pandoc:

:%: !pandoc -f markdown -t html
Previous<Ctrl-r>Next:global

Last updated 3 years ago