Neovim: Manipulate Markdown Tables by Column Name
In this article, I share an improvement to the process that lets you manipulate markdown tables in Neovim. Previously, you specified a column to operate upon using it’s index number (e.g., 1, 2, …, n). The improved process is to specify the column name (e.g., id, name, age). Using the column name is much more convenient. This is demonstrated below with animated GIFs, and the source code is included.
Utility Script for Getting Column Indices
This is script is used by the others to get the index associated with the desired column name.
#!/bin/bash
DEFAULT_DELIMITER='|'
DELIMITER="${DEFAULT_DELIMITER}"
ARG_OFFSET=1
while getopts ":d:" opt; do
case "${opt}" in
d)
DELIMITER="${OPTARG}"
ARG_OFFSET=2
;;
\?)
echo "Invalid option -${OPTARG}" >&2
exit 1
;;
esac
done
REGEX=$(echo "${@:${ARG_OFFSET}}" | tr ' ' '|')
while IFS=$'\n' read -r line; do
echo "${line}" |
sed "s/${DELIMITER}/\n/g" |
nl -s ',' -w 1 |
grep -E "${REGEX}" |
cut -d ',' -f1 |
tr '\n' ' ' &&
echo
done
Table Features
Swap Columns
Script: TableSwapColumns
#!/usr/bin/env bash
# Purpose: Swap columns in a markdown table
# Read in all input (to be processed more than once)…