How do I create a macro to convert a value to a JSON key-value pair?
Answer
qaI"<Esc>A": "",<Esc>jq
Explanation
This macro transforms a plain word into a JSON key-value pair format, useful for converting lists of field names into JSON structure.
How it works
qa— start recordingI"— insert opening quote at line start<Esc>— back to normal modeA": "",— append key-value syntax at end<Esc>j— back to normal, move downq— stop recording
Example
name
age
email
After running on each line:
"name": "",
"age": "",
"email": "",
Tips
- Modify for different formats (YAML, TOML, etc.)
- Use
{count}@ato process multiple lines - Customize the value placeholder as needed
- Combine with
:g/./normal @afor non-blank lines only