Skip to content

Instantly share code, notes, and snippets.

@NickKelly1
Last active April 24, 2022 23:20
Show Gist options
  • Select an option

  • Save NickKelly1/899adc6d80ca64e7a24edca087167c99 to your computer and use it in GitHub Desktop.

Select an option

Save NickKelly1/899adc6d80ca64e7a24edca087167c99 to your computer and use it in GitHub Desktop.
vim-init.sh
#! /usr/bin/env bash
#
# vim config script
#
# https://gist.github.com/NickKelly1/899adc6d80ca64e7a24edca087167c99
#
# usage:
# $ curl -s https://gist.github.com/NickKelly1/899adc6d80ca64e7a24edca087167c99/raw | bash
#
# author: Nick Kelly
# created: 2022-04-24
# updated: 2022-04-24
#
set -e
set -u
set -o pipefail
VIMRC_FILEPATH=~/.vim/vimrc
echo
echo "======================================"
echo "=== Initialising vim configuration ==="
echo "=== author: Nick Kelly ==="
echo "=== created: 2022-04-24 ==="
echo "=== updated: 2022-04-24 ==="
echo "======================================"
echo
echo "Vim conf filepath: ${VIMRC_FILEPATH}"
if [[ -f "${VIMRC_FILEPATH}" ]]; then
echo "${VIMRC_FILEPATH} already exists"
read -r -p "Append to the existing vimrc file? [Y/n] " cont
case ${cont} in
[yY][eE][sS]|[yY])
# allow continue
;;
[nN][oO]|[nN])
echo "Exiting"
exit 0
;;
*)
echo "Invalid input..."
exit 1
;;
esac
fi
# get the desired leader
LEADER_DEFAULT=","
LEADER=
read -e -p "Select a leader key: " -i "${LEADER_DEFAULT}" LEADER
# lowercase
LEADER=${LEADER,,}
# lowercase
LEADER=${LEADER,,}
# first char
LEADER=${LEADER:0:1}
LEADER=${LEADER:-${LEADER_DEFAULT}}
LEADER_SECTION="\" leader: ${LEADER}"
if [[ "${LEADER}" != "${LEADER_DEFAULT}" ]]; then
LEADER_SECTION=$(cat <<-EOF
# change leader from ${LEADER_DEFAULT} to ${LEADER}
let mapleader = "${LEADER}"
EOF
)
fi
# ensure .vimrc directory exists
mkdir -p $(dirname ${VIMRC_FILEPATH})
# bash heredoc
# append these lines into VIMRC_FILEPATH
cat <<-EOF >> ${VIMRC_FILEPATH}
${LEADER_SECTION}
" sql syntax in pgsql cli
au BufRead /tmp/psql.edit* set syntax=sql
" tabs & spacing
set expandtab
set smarttab
set smartindent
set tabstop=2
set softtabstop=2
set shiftwidth=2
" error bells
set noerrorbells
set visualbell
set t_vb=
" no annoying swapfiles
set noswapfile
set nobackup
" undo directory to remember change history between opens
set undodir=~/.vim/undodir
set undofile
" incremental search & search highlighting
set incsearch
" to un-highlight the current search, use :hos (i.e. nohlsearch)
set hls
EOF
echo "Finished. See new configuration in ${VIMRC_FILEPATH}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment