Skip to content

Instantly share code, notes, and snippets.

View jamescherti's full-sized avatar

James Cherti jamescherti

View GitHub Profile
@jamescherti
jamescherti / bash_find_file_parent_dirs.sh
Last active December 25, 2022 22:05
Bash: Find a file in the current directory or in its parent directories (e.g. ".gitignore").
#!/usr/bin/env bash
# Description: Find a file in the current directory or in its parent directories (e.g. ".gitignore").
# Usage: find_file_parent_dirs ".gitignore"
# Author: James Cherti
# URL: https://gist.github.com/jamescherti/9c129c5621a9899ff7c9f3192855cce3
# License: MIT
find_file_parent_dirs() {
local filename="$1"
local error=0
@jamescherti
jamescherti / vim_chdir_buffer_dir.vim
Last active December 25, 2022 22:05
Vim: change the current working directory to the directory of the current buffer.
" Language: Vim script
" Description: Change the current working directory to the directory of the current buffer.
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/238f5876c71c9be39801535f5a12a292
" License: MIT
function! ChdirBufferDir() abort
if &buftype !=# ''
echo printf('The buffer type ''%s'' is not supported.', &buftype)
return
@jamescherti
jamescherti / inputrc_hjkl_motion.conf
Last active March 5, 2025 10:13
inputrc: add hjkl bindings to ~/.inputrc (used by the shells bash, sh...).
" Config file: Add lines to ~/.inputrc
" Description: Add hjkl bindings to ~/.inputrc
" (used by the readline and command line
" shells bash, sh...).
" Alt-h: left
" Alt-j: down
" Alt-k: up
" Alt-j: right
" Author: James Cherti
" GitHub Gist: https://gist.github.com/jamescherti/3b6582c6079ba80e55c9927021d1edc5
@jamescherti
jamescherti / vim_unfold_move_start_fold.vim
Last active December 25, 2022 22:04
Vim: Unfold a line and move the cursor to the start of the fold.
" Language: Vim script
" Description: Unfold a line and move the cursor to the start of the fold.
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/c8c4d54f9d823cc574f337ad463c78ad
" License: MIT
function! s:unfold_move_to_start_fold(unfold_mapping) abort
let l:first_line_fold = foldclosed('.')
let l:last_line_fold = foldclosedend('.')
if l:first_line_fold !=# -1
@jamescherti
jamescherti / vim_cmd_alias.vim
Last active December 25, 2022 22:04
Vim: Create aliases for Vim built-in commands. The function uses abbreviations (cnoreabbrev) to get around this limitation.
" Language: Vim script
" Description: Create aliases for Vim built-in commands.
" The function uses abbreviations (cnoreabbrev)
" to get around this limitation.
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/4693568eae7f9b87a56903f942487e75
" License: MIT
function! CmdAlias(cmd, ...) abort
for l:alias in a:000
@jamescherti
jamescherti / vim_viminfo_path.vim
Last active December 25, 2022 22:04
Vim: Return the path to viminfo.
" Language: Vim script
" Description: Return the path to viminfo.
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/3651dfc40aac4181f01fffad4095094e
" License: MIT
function! VimInfoPath() abort
if &viminfofile
return fnamemodify(&viminfofile, ':p')
endif
@jamescherti
jamescherti / python_escape_string.py
Last active December 25, 2022 22:03
Escape the characters that occur in a string.
#!/usr/bin/env python
# License: MIT
# Author: James Cherti
# URL: https://gist.github.com/jamescherti/30b5c80a1dd64d6327bb36ac3c956312
"""Escape the characters that occur in a string."""
def escape(string: str, chars: str):
"""Escape the characters in 'chars' that occur in 'string'."""
result = ""
for char in string:
@jamescherti
jamescherti / python_escape_all.py
Last active December 25, 2022 22:03
Escape all characters.
#!/usr/bin/env python
# License: MIT
# Author: James Cherti
# URL: https://gist.github.com/jamescherti/981306ca16f92115a57796e1ae3d241d
"""Escape all characters."""
import re
def escape_all(string: str):
"""Escape all characters."""
@jamescherti
jamescherti / vim_get_mime_type.vim
Last active December 25, 2022 22:03
Vim: a function that returns the MIME type of a file
" Language: Vim script
" Description: a function that returns the MIME type of a file
" (UNIX / Linux only).
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/c7d92a3679f87438c72ffaa3a79b9f21
" License: MIT
function! MimeType(filename) abort
if !executable('file')
throw 'No ''file'' in ' . $PATH
@jamescherti
jamescherti / vim_save_all_focus_lost.vim
Last active December 25, 2022 22:03
Vim: Save all modified files when the focus is lost.
" Language: Vim script
" Description: Save all modified files when the focus is lost.
" Author: James Cherti
" URL: https://gist.github.com/jamescherti/14a87b56e6e72e8eccd90e7d28cb73f1
" License: MIT
function! SaveAll() abort
let s:confirm = &confirm
try
set noconfirm