aliquote.org

Custom Vim shortcuts (2/n)

July 25, 2021

« Previous post in this series
» Next post in this series

Here is a list of the personal shortcuts I use the most in Vim. Nothing special, but feel free to copy/paste in case you find anything useful for your own workflow. Of note, I’ve read so much posts on remapping everything to Vim’s hjkl hot keys that I got tired of it, really. I use the hjkl keys when I think I’ll need to repeat a command, otherwise we just have plain old arrow keys, right? Likewise, in Tmux, the arrow keys allow to switch to any pane easily. Arrow keys are mapped in many applications as well, whether it be Firefox, iTerm, Gnome terminal, Kitty, etc. Even in Vim you can use arrow keys with to navigate along the way. Home row, you said? Come on, the arrows are just below, on your right – not so far away in fact. But let’s go to the point.

Of course, I realize this is all subjective and it really depends on your needs and what you expect from your text editor. I for one consider my text editor as a helping hand: When I need to perform an action, give me the means to do it quickly or efficiently; I have poor abstract memory but if I repeat the same action a sufficient amount of time it will become muscle memory; I want it to correct my misspellings and fix my code formatting automagically, and sometimes – sometimes only – suggest better alternatives to what I wrote (mostly code, not prose). Above all, my text editor must be as fast lightning. I don’t need sugar candy app, or fancy editors that look like Christmas trees with everything highlighted here and there. I don’t really care about the color theme as long as it’s not everywhere unnecessarily. My current color scheme in Vim is deliberately refined to just highlight intentional comments (which must be emphasized), keywords and constants – and that’s all good. I talked about those choices earlier on.

Alabaster highlights comments. Most themes try to dim comments by using low-contrast greys. I think if code was complex enough that it deserved an explanation then it’s that explanation we should see and read first. It would be a crime to hide it. — Nikita Prokopov (tonsky)

Snippets were fun a while ago, but I don’t like them anymore. Most of the snippet-based packages are probably inspired by the great Textmate app, which I used to use a while back. I’m not a robot, I can write, slowly but I like it nevertheless – I mean, to write, not slowly. Errors are so much instructive and rewarding, and snippets do not cover every user cases, unfortunately. Likewise, autocompletion is great, but only when I need it. Most of the time I know what I want to write and I don’t need a popup window to show complete options. This is why I do not use snippets and set a high time value for autocompletion suggestion.

And here we go with my collection of personal shortcuts. These are Vim mappings, mostly for normal mode, so please adapt to your personal settings:

Keys Description
,xclose current buffer
-open Netrw
␣xclose quickfix, loclist or Trouble window
␣.set current working directory
␣eedit files from current buffer directory
␣Eedit in new tab files from current buffer directory
tabswitch to next tab (cycle)
␣tabcreate a new tab
␣$open a terminal in a new tab
C-jgo to next quickfix item (also ]q)
C-kgo to previous quickfix item (also [q)
,ssort current paragraph in lexicographic order (vip:sort u, thanks strager!)
zsfix misspelled word using first suggested entry (1z=)
␣dswitch dictionary (custom autoload function to alternate between FR and EN dicts)

That’s it, not a big deal. I tend to use built-in shortcuts a lot because I learned the hard way that most often than not, default settings are good.

In Vim, my leader key is <space> () and my localleaderkey key is ,. I tend to use the leader key for installed plugins, while the localleader is used for custom settings or to remap commands I use a lot. Of note, my CAPS lock key is remapped system-wide to (so ,<esc> is equivalent to ,<caps> to hide all other buffers in split view). As I said these mappings are mostly for normal mode. I only use <C-e> and <C-a> in normal and command mode, because these shortcuts are wired in my hands because readline’s everywhere. I tend to use the same mapping in lower and upper case for related tasks. I recently replaced all my Fzf setup with Telescope (see my previous post). I prefer to use the quickfix window rather than the loclist or Telescope for specific task, but I can display the quickfix list in Telescope as well. My complete Telescope mappings are shown below:

utils.map('n', '<leader><leader>', '<cmd>Telescope buffers<CR>')
utils.map('n', '<leader>!', '<cmd>Telescope commands<CR>')
utils.map('n', '<leader>/', '<cmd>Telescope current_buffer_fuzzy_find<CR>')
utils.map('n', '<leader>?', '<cmd>Telescope live_grep<CR>')
utils.map('n', '<leader>*', '<cmd>Telescope grep_string<CR>')
utils.map('n', '<leader>:', '<cmd>Telescope command_history<CR>')
utils.map('n', '<leader>f', '<cmd>Telescope find_files<CR>')
utils.map('n', '<leader>G', '<cmd>Telescope git_commits<CR>')
utils.map('n', '<leader>g', '<cmd>Telescope git_status<CR>')
utils.map('n', '<leader>h', '<cmd>Telescope help_tags<CR>')
utils.map('n', '<leader>q', '<cmd>Telescope quickfix<CR>')
utils.map('n', '<leader>r', '<cmd>Telescope oldfiles<CR>')
utils.map('n', '<leader>w', '<cmd>Telescope treesitter<CR>')

Note that Telescope respects autochdir, so it will limit itself to the current working directory if that option is set to true. As we are generally interested in considering the root directory of a project if there is one, you can use the following mappings as a workaround: (this of course assumes that your project lives in a Git repo)

vim.cmd[[nnoremap <leader>f <cmd>lua require('telescope.builtin').find_files{ cwd = vim.fn.systemlist("git rev-parse --show-toplevel")[1] }<cr>]]
vim.cmd[[nnoremap <leader>. <cmd>lua require('telescope.builtin').live_grep{ cwd = vim.fn.systemlist("git rev-parse --show-toplevel")[1] }<cr>]]

However, my own shortcut <space>e was devised long ago to handle the general CWD issue with Vim. So if you are using the above setup, provided autochdir is not explicitly set to true, you should be able to use Telescope to browse your project while <space>e, after eventually setting the root directory using <space>., would open or create file in the same directory as the current buffer or in the root directory previously defined.

See Also

» Let's go modern with Neovim (1/n) » Neovim and Lua » Reading list on Lisp & Co. » Vim and LSP » Random musings with VS Code