Vim – Minimal Setup Explained
guckes.net
guckes.net
Not that I'm saying you can't — or shouldn't — re-set options, just that the linked documentation is a little dated. FWIW, I personally re-set a couple of default options mostly so that I have a point of reference for how an option works when I'm digging about in my config.
I'm of the opinion that the combination of recent vim and Tim Pope's sensible.vim² is The Minimal Setup™.
¹ The site says the last update was 2014, and back then $VIMRUNTIME/defaults.vim wasn't a thing for example(7.4.2111).
The least desirable property of defaults.vim is also present in sensible or in system-wide vimrcs written by package maintainers: you get a black box where things are added/changed/removed without your knowledge or consent. "Your" so-called minimal setup is going to evolve over time, but not according to "your" needs.
We got so many support request related to that silly mechanism that's not even funny.
I've given up on vim. I'm now teaching myself Emacs on my own machine, and replace vim with neovim everywhere I can.
Also, amen. Having used vi since before vim, I actually have a few settings to restore vi features and quirks that I like ( cpoptions+=vxZ!$- ).
You can either copy the settings you are interested in or source it entirely in your vimrc file:
source $VIMRUNTIME/defaults.vim
---Here's some more related resource links:
* stackoverflow: useful .vimrc tips - (https://stackoverflow.com/q/164847/4082052)
* vi.stackexchange: How do I debug my vimrc file? - (https://vi.stackexchange.com/q/2003/1616)
* vim-sensible - (https://github.com/tpope/vim-sensible)
* minimal vimrc for new users - (https://gist.github.com/benmccormick/4e4bc44d8135cfc43fc3)
* Vim Configuration From Scratch - (https://marcgg.com/blog/2016/03/01/vimrc-example)
* vimconfig: generate `.vimrc` by selecting options - (https://vimconfig.com/)
But yes, adopting the rule that you should never put anything in your ~/.vimrc that you don't understand from building it up over time and usage is a good one.
As for Ben McCormick's vimrc…
- line 18 is useless and its comment is misleading
- line 30 is a matter of taste so it has nothing to do in that context
- the comment for line 33 is wrong and shows how much the author cares about "new vim users" and how much he knows about the subject
Building one's vimrc from scratch is an integral part of the learning so shortcuts are to be avoided.
au InsertLeave * :normal `^
That goes along with this: set virtualedit=block,onemore imap jj <ESC>(I use my Caps Lock as Backspace. Backspace near the top right is vastly less comfortable to reach than Escape in the top left.)
People really like the home row but (1) I think it is fine to leave it once in a while and (2) my joints don’t like stay in a very static position for too long in any case.
Problem is, without modes, we lose capability that comes from the fact that modes exist, such as simple characters being compositeable commands;
How should a non-modal editor know whether that [d] I just pressed is me wanting the letter `d` or the delete command? It can't;
Either I have to somehow tell it "the next letter I press is to be interpreted as a command", or "interpret what I press as commands until I say otherwise". The former gets tiresome rather quickly (what if I have to do several commands?), the latter would again be a modal editor.
If you want stay in insert mode most of the time you could use Ctrl-o to switch to command mode for a single command and then be immediately returned to insert mode.
Eg
Ctrl-o de
To delete to the end of the current word.Again, I definitely wouldn't do it that way but vim makes pretty much everything possible.
I also have my caps mapped to both ctrl and esc. Ctrl when held, and esc when tapped. Wish I had done that ages ago.
On other keyboards I find fishing for the Esc key with the left ring or sometimes even middle finger good enough since it's so prominently singled out on the top left corner on all the keyboards I had yet to use, but it breaks the flow a bit more.
It probably helps that I'm not typing in proper form for the 10-finger system but only some hodgepodge approximation of it...
The remapping was originally for Vim, but now at least a third of my Esc usage is outside of it.
However, for a long time I used Ctrl-c instead of escape, which is 99% the same except in some odd corner cases (not recommending that, just mentioning my history).
These days I use the common vi tweak of 'jk' being mapped to escape.
So using vim becomes a series of small edits.
I map caps to Ctrl and use Ctrl-[ for escape.
That means I can still use Ctrl for insert mode shortcuts such as
Ctrl-w (delete last word)
Ctrl-r to insert a register (eg Ctrl-r% for filename)
Ctrl-h instead of reaching for backspace.
There’s loads of them.It just takes a bit of muscle memory but if you’re a touch typist it’s well worth it IMHO.
Doing it this way, rather than say ‘jk’ in insert mode, also has the advantage of working in other vim-like programs where you’d have to repeat the mapping. If that’s even possible.
(Also the control key is where the caps lock is. Also another fine position.) :)
But in 2022, this is no longer true. Even the most limited platform you can think of can comfortably run vim. A $5 Raspberry Pi Zero is overkill for vim. And now, labor is more far more expensive than hardware.
So, economically, having commands like "set ek" are objectively a waste of resources. Make commands self-explanatory, drop the abbreviations, and just add autocomplete and suggestions to compensate for the verbosity.
It's time to evolve. The reason this page exists, is because vim is unintuitive. But it doesn't have to.
Vim has autocomplete. That said, while I support long commands, I like having short commands because it's less typing, and I use vim on awkward keyboards where it matters. Also, any changes need to leave the old way or you break people's muscle memory (so again, just add the friendlier versions in addition, not as replacements, and I'm probably on board).
$ change-directory foo/bar
$ amazon-web-services elastic-compute-cloud describe-instances
$ javascript-object-notation-query ...
Not to mention short parameters: `ls -AF`, `grep -v`, `curl -H`, ...One of my most valuable resources is time. I would rather invest it in learning each tool's vocabulary than spend so much time typing (a prefix longer than the existing name, tab, then pick the answer) in an "everything is spelt out in full, acronyms and abbreviations are banned" world.
When writing a script or documenting you use the long form, when using a shell you use short form.
Personally I try to avoid mappings as much as makes sense.
So I’d use
:noh
to clear highlighting instead of making a nohighlight mapping for it.Simply because it’s still quick to type from muscle memory and more portable. I also find them easier to remember than custom mappings.
Well I see that finally I did my programmer life learning only switching insert mode / edit mode, maybe 5 commands like A x etc...,and :q or :wq!
Eg from the first “pick” you could press * to highlight occurrences and move to the next one use cw to change it to “s” and bounce on n to move to the next one you to squash and use . to repeat the change.
That’s visual and lets you decide which commits to squash.
If I wanted to squash all the other commits I’d probably use visual block and } instead, but that’s just me.
I’d save a macro for more complex edits.
git rebase -i ...
followed by
↓dw ↓←dw ↓←dw ↓←dw ↓←dw ↓←dw i s ↓s ↓s ↓s ↓s ↓s
because I keep forgetting :s/pick/s/g is a thing
i, a, :q, :wq, dd, xdd, and D are all the ones I remember, but cutting/yanking pasting is still something I don't have the muscle memory for.
Shift+G takes me to the bottom of a file, but I don't know how to get back to the top :D .
I really need to make time for vimtutor and commit to it.
mostly emacs user here, but when I'm on vim I use :0 and :%
:%s/^pick/s/
to replace "pick" at the start of line with "s" on all lines? If you execute it once in vim you can recall it later with just typing
:%↑
and confirm with Enter.
As for the top of the file:
gg
I use the mnemonic "go go line" (as in Inspector Gadget's "go go gadget") and since you can prefix it with a line number (e.g. 4gg to go to line number 4), leaving it out gets me as close to line number nothing^Wzero as possible, i.e. line 1.
HTH :)
PS: the % says "on all lines" your trailing "/g" would make it match&replace "pick" multiple times on the (current, since % is missing) line
PPS: As an aside, I use "vim somefile +N" all the time to open somefile at line number N;
especially when there's an error message in the form "error at somefile:N" by typing vim, then double-click-middle-click copy&pasting "somefile:N", Ctrl-E to jump to the end and replacing ":" with " +". I really should write a shell function for that...
EDIT: formatting
I know the complement to Shift+G, it's gg. But I can never remember which one goes to the top and which to the bottom, so half the time I press both. :)
The whole thing would be something like <ctrl-v>5jecs<esc>. Ctrl-v starts rectangular block mode, go down 5 lines and to the end of the word, change selection to "s", return to normal mode.
It is immediately apparent to me that my interaction with vim has been heavily informed and limited by my preconceptions and previous text editing experiences.
Time to start engaging with vim as its own entity, with its own conventions and idiosyncrasies.
EDIT: Formatting
How do vim people quickly navigate their code without going "what was the name of that file with that function"?
It lets you do pretty much what you say allowing me to type <leader>-f and partially type things to remember where I am with a preview pane. Seems moderately fast most of the time for the monolith I work on.
These days people generally use some kind of fuzzy finder plugin thats basically the same as the vscode experience. Exactly which one you use depends on various factors, but Telescope and Fzf are two big names and both allow searching through a bunch of other stuff, setting names, edit history etc. (You can also save the fuzzy search results to the quicklist so you've basically got a reusable index).
And neovim is heavily embracing the LSP paradigm, where you'd use something like gd to jump to definition when on top of a function and a language specific LSP server will tell Neovim where to jump to.
This x100. I switched to neovim+pylsp a few months ago, and it's been an absolute delight. I use lspconfig [1] and pyslp [2] since most of my time is spent developing Python code. Not only can I navigate more quickly and easily, I also find myself catching more silly errors before run-time due to better linting from the lsp.
It used to be that I wouldn't use vim beyond quick edits on single files. Although I still prefer to use vim style bindings in IDEs, I can go much further before I feel the need to fire up an IDE in the first place.
I tend to use FZF[1] which has functions for leveraging ripgrep and silver surfer for searches. It can also use find. FZF also has dialogues for paring down results in real time (i.e., as you type)
There are also plugins that are much more nuanced and more involved for searching symbols and providing autocomplete. Duoplete, vimcomplete, youcompleteme spring to mind.
[0]: https://ctags.io/
The other alternative is to set up a Language Server (LSP) for navigation and you can use the same ones as VSCode uses.
I also keep vinegar around for when I want to poke around at the contents of a directory.
You can combine this with grep and the location list to find symbol usage across multiple files.
nnoremap <leader>g :grep <cword><CR>
You can then use :cnext and :cprev (or focusing the window and selecting an entry) to navigate between them.As others have stated, you can also use ctags (plugins like https://github.com/ludovicchabant/vim-gutentags are useful for refreshing tags in a project), but for some languages you may need to add a tag definition (e.g. for something like rust or zig). For older languages like C you should be fine.
[1] https://marketplace.visualstudio.com/items?itemName=jonsmith...
(But I feel like I'm faster with regular VSCode - multi cursors easily solve many "text editing" problems that Vim solves with macros/repeat/regexp)
I just really really wish my minimal comfortable setup would not need to include
set mouse=
on all "newer" machines/vim versions :-\
You can use it as plugin