3. Important Keymapping and Settings


Now I’m about to jot down numerous neovim configuration code, the primary configuration can be settings that assist me working with the configuration itself.



Open and re-source the configuration

As I already talked about that the primary purpose for why I construction my configuration the way in which I do is as a result of I would like to have the ability to adjustments, after I’m in my every day work, and I discover that one thing is missing in my configuration. I must shortly make the change, and get again to the place I used to be, which is why closing and reopening vim for these sorts of adjustments is one thing I actually wish to keep away from.

So the very first thing, I do is setup keyboard shortcuts to edit and rerun, or “re-source” the configuration. This tip, I realized from Be taught Vimscript The Exhausting Approach

I additionally wish to map my chief key to one thing simply accessible, and my thumb is at all times resting on the area bar, in order that can be my chief key.

native operate reload()
  dofile(vim.env.MYVIMRC)
  print("Configuration reloaded")
finish

vim.g.mapleader = " "
vim.g.maplocalleader = " "
vim.keymap.set("n", "<chief>ve", ":tabnew $MYVIMRC<cr>", { desc = "Open neovim configuration file" })
vim.keymap.set("n", "<chief>vs", reload, { desc = "Re-source neovim configuration file" })
Enter fullscreen mode

Exit fullscreen mode

Observe: whereas resourcing might have been achieved utilizing :supply $MYVIMRC, I’ll later want so as to add extra behaviour to the reloading, which is why I have already got made a operate for the behaviour. For more information, see :assist :supply

For extra data on chief keys and mapping sequences, see Chief keys and mapping keyboard sequences



Escape hatch

The default key for exiting insert-mode, <Esc> is a little bit distant. I’ve remapped that to jk, a sequence of keys that fast exits to regular mode, and it is a sequence that’s by no means utilized in actual phrases or code sequences (I’ve used this mapping for about 8 years now, and the one time it conflicts with an actual use case is after I write about my vim configuration) – this was additionally a tip I realized from “Be taught Vimscript …”.

vim.keymap.set("i", "jk", "<esc>", { desc = "Exit insert mode" })
Enter fullscreen mode

Exit fullscreen mode

In case you are used to vim, and wish to undertake to this sample, it may be useful to remap <Esc>, to the no-op (see :assist <nop>)

vim.keymap.set("i", "<esc>", "<nop>")
Enter fullscreen mode

Exit fullscreen mode

I had this mapping setup after I first adopted this technique. Now, it is not essential for me.



Faster save

After having used Home windows for a few years, I am used to <Ctrl>+s for saving a file. And I’ve remapped the CAPS LOCK key to work as <Ctrl>, in order that mixture is one which solely requires my left pinky to maneuver barely to the left to succeed in. I additionally wish to have this mapping obtainable in each regular and insert-mode, as each edit usually ends with a save anyway, i.e. after saving from insert-mode, I will be again in regular mode for enhancing.

vim.keymap.set("n", "<C-s>", ":w<cr>", { desc = "Save present file" })
vim.keymap.set("i", "<C-s>", "<esc>:w<cr>", { desc = "Save present file" })
Enter fullscreen mode

Exit fullscreen mode



Wise indentation

Whereas writing the reload operate, vim by default inserted a tab. I need spaced, and I need two of them.

vim.choose.expandtab = true
vim.choose.tabstop = 2
vim.choose.shiftwidth = 2
vim.choose.softtabstop = 2
Enter fullscreen mode

Exit fullscreen mode

Observe, that these are actually the default settings. Every file can overwrite the settings. E.g. in Go, I do wish to have tabs1.



Disable swap file

Vim retains a “swapfile” that serves as a backup whereas working, and may help recuperate unsaved work within the case of a crash. I do not discover this very useful. I save usually, and I’ve model historical past in git.

The swapfile shouldn’t be a lot a nuisance in neovim because it was in vim, as neovim shops the swapfile away out of your working listing. The vim variations I’ve used saved the swap file in the identical folder because the edited file, making it numerous noise within the file system.

vim.choose.swapfile = false
Enter fullscreen mode

Exit fullscreen mode

When connecting to a distant server, a swapfile can be useful to recuperate from a damaged connection. However tmux may also remedy the identical drawback.



Line numbers

Displaying line numbers may be useful, because it helps you navigate shortly to the specified line, e.g. 30gg or 30G each bounce straight to line 30. Relative line numbers then again will present what number of traces above or beneath every line is, usually making for a shorter enter, e.g. 8j, moderately then 128gg.

Mix relativenumber with quantity to indicate absolutely the line quantity for the present line, and relative numbers for all different traces

vim.choose.quantity = true
vim.choose.relativenumber = true
Enter fullscreen mode

Exit fullscreen mode

Screenshot showing the actual line number for the current line, and for all other lines, it shows the number of lines above or below

I’ve beforehand been a bit irritated by the fixed shifting round of the numbers within the gutter, however proper now I am attempting out relative numbers. I’ll disable it once more sooner or later if I am sad with it.

Observe: Absolute line numbers could also be extra useful for pair programming, because the navigator can shortly reference the concrete line, “I am unsure the conditional logic on line 30 is right” (the quantity stays right even when the motive force strikes round on the identical time).



Search choices

By default, when looking out with / or ?, the search is case delicate. I usually do not wish to be bothered with typing the proper case. Simply that the proper letters are within the search must be sufficient. I set ignorecase to have case insensitive search

When looking out, the search outcomes are highlighted, and the command :nohighlight or :noh disables it once more. I’ve setup a shortcut to extra shortly dismiss this

vim.choose.ignorecase = true
vim.choose.smartcase = true
vim.keymap.set("n", "<chief>h", vim.cmd.nohlsearch)
Enter fullscreen mode

Exit fullscreen mode



Take away ineffective capabilities

If the cursor is positioned on a quantity, the shortcuts <Ctrl>+a and <Ctrl>+x increments/decrements that quantity. Not solely do I not have any use for this behaviour, <Ctrl>+a can be used to manage tmux, which I usually use along with neovim.

When I’m unexpectedly not in a tmux session, and I attempt to management tmux, I’ve on a couple of event incremented a quantity, not realising it till I observe a bug in my code. So I eliminate these two ineffective mappings

vim.keymap.set("n", "<C-a>", "<nop>")
vim.keymap.set("n", "<C-x>", "<nop>")
Enter fullscreen mode

Exit fullscreen mode



Developing

Now, I’ve the naked necessities up and working, and now is an efficient time to decide to the git repository. That is a job I wish to carry out from inside neovim itself, so within the subsequent half within the sequence, I’ll add git help to neovim, which once more requires some form of package deal administration, so I will additionally add a package deal supervisor moderately than managing that on my own (though that’s completely doable).


  1. Go is so opinionated about formatting that there’s just one method to format indentation, and that’s utilizing tabs. You’ll be able to configure your editor to the way you need it to look. It nonetheless makes use of areas for alignment, making certain it’s not depending on editor settings. 

Leave a Reply

Your email address will not be published. Required fields are marked *