Skip to content

Instantly share code, notes, and snippets.

Avatar

George Brown george-b

View GitHub Profile
View Vim_fuzzy_open.md

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. We are having to think if we want to use the buffer or edit. With the former simply being able to match on a substring it behaves much like a fuzzy finder would but the later is more nuanced.

View Vim_get_argv.md

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()))
View Vim_auto_hlsearch.md

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
View Vim_search_next.md

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
@george-b
george-b / Vim_cabbrev.md
Last active Oct 6, 2020
Lazy case correction for user commands
View Vim_cabbrev.md

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.

View Vim_create_path.md

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
View Vim_mapping_wildcharm.md

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.

View Vim_helpers.md

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

View Vim_make.md

Introduction

Some people like to run tools such as test runners outside their editor. Perhaps manually or perhaps via some file watching utility. They then review the output, switch back to their editor, and navigate to the appropriate location.

I prefer to explicitly invoke an external program from my editor, review the output, and allow my editor to jump to the first error. As well as storing a list of the locations of any other errors, a list that I may

View Vim_one_window.md

Preamble

In an effort to consolidate my focus in Vim I wondered what it would be like if I limited myself to a single window. This notion came about because I knew I wasn't utilizing things like tags, jumps, and the quickfix list as effectively could be done.

It's important to remember that no configuration is a substitute for discipline when learning to change. This is simply a journey I went on and sharing what I learnt about Vim along the way.

You can’t perform that action at this time.