Skip to content

Instantly share code, notes, and snippets.

@hardselius
Created November 18, 2021 10:10
Show Gist options
  • Save hardselius/5b33bdc58a40651fc987199b1d12a783 to your computer and use it in GitHub Desktop.
Save hardselius/5b33bdc58a40651fc987199b1d12a783 to your computer and use it in GitHub Desktop.

cURL in Vim

curl allows you to specify a text file from wich it will read its arguments from via the -K, --config <file> flag. This is a useful feature if you want to maintain a set of commands but you care about seeing the output and not about keeping/editing/analyzing it. Something like a rudimentary test suite as an alternative to Postman.

From the curl manual:

Options and their parameters must be specified on the same line in the file, separated by whitespace, colon, or the equals sign. Long option names can optionally be given in the config file without the initial double dashes and if so, the colon or equals characters can be used as separators. If the option is specified with one or two dashes, there can be no colon or equals character between the option and its parameter.

If the parameter contains whitespace (or starts with : or =), the parameter must be enclosed within quotes. Within double quotes, the following escape sequences are available: \, ", \t, \n, \r and \v. A backslash preceding any other letter is ignored. If the first column of a config line is a '#' character, the rest of the line will be treated as a comment. Only write one option per physical line in the config file.

Specify the filename to -K, --config as '-' to make curl read the file from stdin.

Note that to be able to specify a URL in the config file, you need to specify it using the --url option, and not by simply writing the URL on its own line. So, it could look similar to this:

url = "https://curl.se/docs/"

This is great news if we want to run curl from within Vim. Suppose we have a file like:

# this is a comment
url = "example.com"
output = "curlhere.html"
user-agent = "superagent/1.0"
 
# and fetch another URL too
url = "example.com/docs/manpage.html" -O
referer = "http://nowhereatall.example.com/"

To execute a curl command from within Vim we can simply, e.g, select all lines in visual mode and execute

:'<,'>w !curl --config -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment