Cht.sh search tool
Published: July 10, 2021
Updated: August 11, 2024
What if an API client helped you find programming questions? It exists already: cht. Sh!!!!
Often, we have a rush deadline to deliver a feature; often, we need an answer so quickly that we open the browser and google it. Then, a bunch of wonderful people working on an [open-source project created a toll named cht. Sh](https://github.com/chubin/cheat.sh), which is an API client that retrieves answers to questions based on a list of sheets (Linux, Postgres, JS, and so many more). I've been using it for over three years and love it. There are numerous ways to use it. The simplest is to use a curl request, browser, Postman, or Insomnia. Okay, I know you get it. Let's see one example of that:
curl cht. sh/python/write+a+file
Let's look at the pieces: Cht. sh is just the hostname. Python stands for the sheet list. Write+a+file is the search string (try to be as strict as possible because not all strings can always match any results). Now, moving on with the answer:
# python - What is the correct way to write a line to a file?
#
# You should use the print() function which is available since Python
# 2.6+
from __future__ import print_function # Only needed for Python 2
print("hi there", file=f)
# For Python 3 you don't need the import since the print() function is
# the default.
#
# The alternative would be to use:
f = open('myfile', 'w')
f.write('hi there\n') # python will convert \n to os.linesep
f.close() # you can omit in most cases as the destructor will call it
# Quoting from Python documentation
# (https://docs.python.org/2/tutorial/inputoutput.htmlreading-and-
# writing-files) regarding newlines:
# >On output, if newline is None, any '\n' characters written are
# translated to the system default line separator, os.linesep. If
# newline is '', no translation takes place. If newline is any of the
# other legal values, any '\n' characters written are translated to the
# given string.
#
# [sorin] [so/q/6159900] [cc by-sa 3.0]
Let's analyse the parts. First: cht.sh
is just the hostname. Second: python
stands for the sheet
list. Finally, write+a+file
is the search string (try to be as strict as possible because not
all strings can match any results). Now, moving on to the answer:
call plug#begin('~/.vim/plugged')
Plug 'dbeniamine/cheat.sh-vim'
" ...
call plug#end()
" Vim command used to open a new buffer
let g:CheatSheetReaderCmd='new"'
" Cheat sheet file type
let g:CheatSheetFt='markdown'
" Program used to retrieve cheat sheet with its arguments
let g:CheatSheetUrlGetter='curl --silent'
" Flag to add cookie file to the query.
let g:CheatSheetUrlGetterIdFlag='-b'
" cheat sheet base URL
let g:CheatSheetBaseUrl='https://cht.sh'
" cheat sheet settings do not include style settings neiter comments,
" see other options below
let g:CheatSheetUrlSettings='q'
" cheat sheet pager
let g:CheatPager='less -R'
" pygmentize theme used for pager output, see :CheatPager :styles-demo
" let g:CheatSheetPagerStyle=rrt
" Show comments in answers by default
" (setting this to 0 means giving ?Q to the server)
let g:CheatSheetShowCommentsByDefault=1
" Stay in origin buffer (set to 0 to keep the focus on the cheat sheet buffer)
let g:CheatSheetStayInOrigBuf=1
" cheat sheet buffer name
let g:CheatSheetBufferName="_cheat"
" Default selection in normal mode (line for whole line, word for word under cursor)
let g:CheatSheetDefaultSelection="line"
" Default query mode
" 0 => buffer
" 1 => replace (do not use or you might lose some lines of code)
" 2 => pager
" 3 => paste after query
" 4 => paste before query
let g:CheatSheetDefaultMode=0
" let g:CheatSheetIdPath=expand('~/.cht.sh/id')
" Make plugin silent by setting bellow variable to 1
let g:CheatSheetSilent=0
After that, you will be able to run in the world's most beloved code editor:
:HowIn python writes a file
And a new pane with the result (if it exists) is returned! It couldn't be more striking.