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.

@g0xA52A2A
g0xA52A2A / Vim_autoreply.md
Last active June 4, 2023 23:30
Vim autoreply

A modified version of Romain's gist.

See prior revisions for functionality closer to the original. This is now a simplification that aims only to provide prompts that would typically follow basic commands.

function! ExpandCommand(pattern) abort
  let aliases =
 \ { 'cn' : 'cnext'

Vim will move the cursor to the beginning of an object after invoking operator upon it. From an interactive editing perspective this may be considered annoying however it is the consistent choice as operators can be destructive. As such restoring the cursor to its prior position after invoking an operator on an object may not make sense.

There are many ways possible to alter this behaviour to your preference with mappings and/or scripting. But with custom operator mappings this can be particularly ugly.

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.

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()))

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.

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

Helpers

Some functions that I think should exist or re-implementations of existing functions with modified behaviour.

Contains

Check if a list contains an item or not, returns a boolean.

Typically the index() function is used to do this. However as it

Performs reverse incremental history search provided the current command-line is empty.

Started out by tweaking ctrlr.vim but ended up being a total re-write. Functionally it's largely the same, differs as follows.

  • History type is detected so works for all command-line types.
  • Mapping uses ternary operator to allow native <C-R> behaviour rather than having to re-implement it.
  • Matches characters using Vim's key notation rather than relying on a dictionary.
  • Gets matches using filter() rather than an explicit loop.
  • Is generally both more readable and concise.

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