Skip to content

Instantly share code, notes, and snippets.

View g0xA52A2A's full-sized avatar

George Brown g0xA52A2A

View GitHub Profile

Setup $PATH to prepend local installs and ensure all the usual system paths are present preserving preference order.

Born out of a hate of systems removing sbin directories from my $PATH (fuck you there is no a good reason to do this).

Also removes empty items in $PATH because I think blindly having CWD $PATH is insane.

Let's say you want to make use of ignorecase as that's what you'd like when searching with / and ?. However you then realise how oddly pervasive this setting is in that is applies to things like :s. Rather than inserting "\C" into every pattern you think you'll use use an autocmd and so some up with something like the following config.

set ignorecase

autocmd CmdLineEnter : set noignorecase
autocmd CmdLineLeave : set   ignorecase

Only to find it doesn't work, ignorecase still seems to be in effect when using :s. This is because CmdLineLeave fires not only before you truly leave the command-line but also before the command has executed.

getcompletion() hell

As if string comparisons being influenced by 'ignorecase' wasn't bad enough determining what will come out of getcompletion() is a like a bag of cats.

Type Follows 'ignorecase' Force case (in)sensitivity
arglist Always insensitive With \\C
augroup Always insensitive With \\C

Switching buffers and opening files

Out of the box Vim allows us to pretty easily open a buffer on a partial match or make wildcard searches for files to edit. As illustrated along with some other things in [this excellent post][1].

So we certainly don't need something like a flashy fuzzy matcher as we have the functionality built in. However what we do lack is the uniform interface. One of the more common patterns I find myself in is thinking if I want to use the :buffer or :edit command.

As of 8.1.2233 Vim has v:argv available to represent the arguments Vim was invoked with as a list.

We can get an equivalent list on *NIX systems with the following, useful if the version of Vim does not support v:args.

split(system('ps -o command= -p' . getpid()))

Enable hlsearch whenever the cursor is on any part of a match and disable hlsearch otherwise.

Whenever the cursor is moved the current line is checked if it contains a match for the current search. If it does a range is created that can be compared to the cursor's current column. This matching is repeated for the current line until no more matches are found.

function! AutoHL() abort

Searching in Vim isn't limited to / and ? however those other searches don't have the same easy one key command to move through matches and I'm lazy. So let's make N and n move to the next match for whatever the last type of search was performed. I'm mainly interested in the quickfix list but I've written this generically so it can be easily adapted to other sources.

function! Cycle(type, forward) abort
  try
@g0xA52A2A
g0xA52A2A / Vim_cabbrev.md
Last active November 30, 2020 18:15
Lazy case correction for user commands

Some user commands are intended as replacements for built-in commands, for example :Grep may replace :grep. Given the existence of such a "replacement" user command when typing :grep it would be convenient to have it replaced with :Grep for the sake of muscle memory.

For other user commands a similar replacement scheme may be desirable simply to avoid having to chord with the shift key.

We can achieve this with command-line abbreviations.

Vim will not create parent directories if they do not exist on write.

A common method to ensure the path exists is to use an autocmd to check if the parent path exists and if not create it.

function! MkDir(path) abort
  if !isdirectory(a:path)
    call mkdir(a:path, 'p')
 endif

Many people (myself included) have set wildcharm=<C-Z> as suggested by :help 'wildcharm' in their config. However in mappings I often see this is repeated explicitly, e.g.

nnoremap <key> :buffer<Space><C-Z><S-Tab>

I prefer to refer to the setting itself, avoiding repetition and also making things more portable. A conversion of the above would be as follows.