13 ms·
A Good Vimrc
- iKlsR 11y agoThe first line is what I wrote about on my blog http://ricardolovelace.com/blog/2015/01/11/vim-is-not-a-black-box/ http://ricardolovelace.com/blog/2015/01/11/vim-is-not-a-blac.... Oddly enough there aren't more articles like these, just repos with a couple hundred line long blobs with every plugin, "hack" and keybinding known to man.
- Svenstaro 11y agoGreat writeup and thank you making it clear that you want people to understand what's happening. I love the clean style of your site. Let me also recommend some of my favorites: set matchpairs+=<:> " match < and > as well set ignorecase " case-insensitive search set smartcase " upper-case sensitive search If you already use ctrlp, you might also cpsm [0], a better matcher for it and ctrlp-funky [1] which allows you to go to methods and other symbols in a ctrlp style. [0] https://github.com/nixprime/cpsm [1] https://github.com/tacahiroy/ctrlp-funky
- exDM69 11y agoFor those just getting started or using vim only occasionally, Tim Pope's vim-sensible is a good, conservative starting point. It just sets some reasonable base configs instead of Vim's crazy default settings. No custom keybindings or fancy plugins included. Vim is by default intended to be compatible with some historical implementation of vi. In my opinion, it doesn't make any sense any more, but I guess there is some reasoning behind the decision. https://github.com/tpope/vim-sensible https://github.com/tpope/vim-sensible
- Svenstaro 11y agoneovim has some saner defaults, by the way.
- fmoralesc 11y agoMostly based on vim-sensible's (with some input from the community): https://github.com/neovim/neovim/issues/2676 https://github.com/neovim/neovim/issues/2676
- jondubois 11y agoAgreed, the current defaults are a major turn-off. Especially for first time users. Vim is incredibly frustrating without auto-indent. The problem with vim is that you can't use it effectively unless you know almost everything that there is to know about it. When it comes to vim, you're either a novice or an expert, there is no middle-ground.
- nbouscal 11y agoThat's just really not true. Vim has a massive middle ground. It has a plethora of features that you don't need to know about, but that make your life marginally easier when you do learn them. Getting up to parity with another editor takes a little bit more time than with a yet-another-WYSIWYG, but you can certainly use vim effectively after a day or two. Everything after that point is just bonus productivity.
- pyvpx 11y agocan anyone suggest similarly helpful resources for those of us stodgily using nvi?
- a_bonobo 11y agoThis one's a bit older but I based most of my .vimrc on it: http://unlogic.co.uk/2013/02/08/vim-as-a-python-ide/ http://unlogic.co.uk/2013/02/08/vim-as-a-python-ide/ and http://www.sontek.net/blog/2011/05/07/turning_vim_into_a_modern_python_ide.html http://www.sontek.net/blog/2011/05/07/turning_vim_into_a_mod...
- MichaelGG 11y agoI, too, wondered about $ and ^ - they are rather inconvenient to type. But instead of B and E, why not map <Space> as leader, then have <Leader>b and <Leader>e? Space seems like the perfect leader key. Mapping , means you lose prev-finding for some searches (f).
- woogley 11y agoI actually map H to ^ and L to $. In my head it's like "h is left, H is far left"
- black_knight 11y agoNice tutorial. While I understand the pleasure of configuring stuff — being a Gentoo-lover I'm throwing stones in a glass house here — I always stuck to a very simple Vimrc file. I find it better to learn how to use the standard settings than forever me tinkering on my own. One could always wish the standard settings were more sane, though…
- tinco 11y agoThis is a very nice write up, but the tabs/spaces part of it makes no sense at all. "I like it, since it means my source code looks the same on every machine." That reason just makes no sense at all. There's only one reason for configuring tabs to be anything but tabs, and that's because someone else set the standard. So if you're editing python or bash, and you don't feel like being a rebel with a cause, you set expand tab. But obviously you shouldn't be doing that manually every time you open a file, so you should configure Vim to detect what's going on. You can do that by adding the `ciaranm/detectindent` plugin and then setting the following line: autocmd BufReadPost * :DetectIndent To detect the indentation any time you open a file. That at least for me makes the headache go away most of the time.
- Spiritus 11y agoThere's also https://github.com/tpope/vim-sleuth https://github.com/tpope/vim-sleuth, by Tim Pope.
- zo7 11y agoWhy would either tabs or spaces be better than the other? Isn't that one of those pedantic style debates that often come down to a matter of personal preference? I'd get rather annoyed if someone on my team was constantly changing the indentation from tabs to spaces or vice versa every time they opened a file. To elaborate on the author's reasoning for using spaces, people sometimes mix spaces with tabs in the indentation unknowingly. If you're not using the author's tab size on your machine, the code doesn't format well at all and if you want to convert to spaces you'd have to manually edit a lot of things. There's also the issue with aligning things as well, which spaces are more consistent for as well. At least with spaces you can see the author's original intention and then reformat it as you wish.
- tinco 11y agoYou're mixing up arguments. No one on your team should be changing indentations. You should decide as a team what your settings are going to be, that's why I suggest using DetectIndent to make sure your settings match whatever the project guidelines are. It's not a matter of personal preference though. Tabs are simply superior for encoding indentation. If you use tabs for indentation you can configure their width in your editor to match your personal preference, spaces doesn't allow for that. That's the only significant difference, and the only good reason for using tabs instead of spaces. None of the arguments you made about spaces make sense. What's the fact that sometimes people mix up tabs and spaces got to do with anything? The whole idea of using tabs is that the code does format well regardless of what tab-width setting you have on your machine. Converting tabs to spaces does not require manual intervention at all. Alignment should be done with spaces, no one is argueing to use tabs for alignment. You can't use tabs for alignment because their width is undefined! The whole idea of using tabs is semantically encoding the authors original intention to allow you to reformat as you wish! At this point.. are you just trolling me?
- chappi42 11y agoNice writing. One point/one question: . section titles can be finished with {{{1 and then no closing brackets are necessary. It will just wrap to the next {{{1 occurence. . regarding nvim: how can I find out what the defaults are there? Probably I have quite a lot of definitions which are no longer needed...
- devpipes 11y agoNeovim Defaults From the current docs: [0] https://neovim.io/doc/user/vim_diff.html#nvim-option-defaults https://neovim.io/doc/user/vim_diff.html#nvim-option-default... From the GitHub issues page: [1] https://github.com/neovim/neovim/issues/2676 https://github.com/neovim/neovim/issues/2676
- charlieegan3 11y agoDisabling the search highlight seems like a rather lowly use for leader+space. Great guide though, didn't know about quite a few options in here, wildmenu is great. I switched to vim from subl about a month ago - it took a bit of getting used to but I'm loving it now. I still use subl for some multi cursor wizardry but that's less and less common now. I found the disabling the arrow keys in normal mode was the point of no return!
- deleted 11y ago[deleted]
- hippich 11y agothere are at least one plugin for multiple cursors stuff - https://github.com/terryma/vim-multiple-cursors https://github.com/terryma/vim-multiple-cursors
- charlieegan3 11y agoYeah - I need to have another look into it. Visual block is working ok for the time being.
- eru 11y ago> I found the disabling the arrow keys in normal mode was the point of no return! Is there any reason to do this when you have a keyboard that has easily reachable arrow keys? (Kinesis Advantage, in my case.)
- code_sterling 11y agoYea, it forces you to use the vim bindings. This is advantageous, since your hands don't leave the home row. It seems minor, but when you actually "get" vim, you'll actually see speed improvements. It sounds silly, the half second saved here and there, but once you've done it long enough, you get into a flow that you just can't get moving your hands to the arrows or mouse.
- 11y ago
- hoorayimhelping 11y agoI don't use vim as my go to editor so I may be asking a silly question; does it just use idiotic defaults or something? I've only met a couple 'vim users' who could just hop onto someone else's vim setup and be productive, and I've never met a single vim user who could use someone else's setup without making comments about everything being different. I guess the question is do people modify vim cause they can or cause they have to?
- adbachman 11y agoFor me, it's because I can. When I find a job I do often, I add it to the config, when I find a tool that makes a job easier I pick it up. Bare, freshly installed vim is quite useable, but is missing the affordances of my personally customized environment. It's kind of like the difference between being able to cook and walking into a strange kitchen and immediately knowing where everything is. In my home I'm comfortable, I know that the vegetable peeler is in a different drawer than the steak knives. In a friend's home, I'm just going to have to ask where something is every few minutes if I want to get work done.
- dchest 11y agoCorrect, the defaults are indeed idiotic, probably because they are from the last century. I even have a freaking plugin installed to properly close buffers (https://github.com/cespare/vim-sbd/ https://github.com/cespare/vim-sbd/). It's indeed harder to use vim without my customizations. I guess it's more like a "build-your-own-editor from parts" kind of thing.
- sean-duffy 11y agoOn my home machine I have a vimrc of about 30 lines, but I could do without most of it, I could probably reduce it to about 5 lines if I had to. So whenever I use vim on a computer at university or on a new machine in the workplace, as long as it has syntax highlighting and auto indent on, which it usually does, that's fine for me.
- JD557 11y agoThe default vim config is pretty barebones. Actually, one of the main goals of Neovim[1] is to provide better defaults[2], such has syntax highlighting. [1] https://neovim.io/ https://neovim.io/ [2] https://github.com/neovim/neovim/issues/276 https://github.com/neovim/neovim/issues/276
- callesgg 11y agowhat is the benefit of representing tabs by 4 spaces?
- DiabloD3 11y agoIt is called personal choice.
- allannienhuis 11y agoone argument for spaces > tabs is that with tabs you cannot see if you have (mistakenly) added spaces to indent a particular line (without :set list which can be annoying to some). If someone else opens the file and their tabstop setting was set to a different number than you, they'll see different indentation on those lines. If everyone uses spaces, everyone will always see the same thing. I think tabs settings as something that should be agreed on by a team or project lead - too easy to screw things up. Same with line ending settings in version control if you're working with a mixed OS team.
- emanuelev 11y agoGreat guide, thanks for sharing. Although that B and E remapping.. man, it hurts!
- JD557 11y agoI've been testing your recommendation of using ag in ctrlP (let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'), but the performance seems to be pretty much the same. Do you notice that much of a difference? Disclaimer: I'm using a SSD.
- octref 11y agoI'm using command-t and it works fine for me even in large code base. AFAIK, ag shines for searching code in large code base, if you are just doing matching with a list of file paths, ag is an overkill. EDIT: And if you set `wildignore` correctly, you wouldn't have a lot of files. Getting node_modules/ out of my Command-T result made it much faster. I actually wrote a plugin[0] to extract patterns from `.gitignore` and apply to `wildignore`, so that Command-T and CtrlP could have a shorter list to search for. [0]: https://github.com/octref/RootIgnore https://github.com/octref/RootIgnore
- eru 11y agoShouldn't even large code bases fit into your RAM's disk cache these days?
- ilaksh 11y agoDid he mention pathogen, vundle, youcompleteme, nerdtree, and powerline?
- DiabloD3 11y agoHeh, I store my entire config in a Github repo: https://github.com/Diablo-D3/config https://github.com/Diablo-D3/config
- chanchan1987 11y agogoods
- Nyubis 11y agoRebinding jk to escape because escape is too far away seemed like a fairly ugly hack to me, especially because you can't type it anymore without waiting. Why not simply rebind caps lock to escape? Caps lock is used awfully little, considering how prominent its position on the keyboard is. On Linux, you can rebind it by adding setxkbmap -option caps:escape to your .profile. If instead of an additional escape you prefer it to be swapped around, use setxkbmap -option caps:swapescape This is perhaps the most impactful change on my vim workflow.
- bonobo 11y ago> [...] especially because you can't type it anymore without waiting This was my thought when I saw this mapping for the first time, and when I started customizing my vimrc I opted for using caps lock as well. It didn´t take long for me to change that and start using kj instead -- in the end it was more common for me to type constants in all-caps than typing "Reykjavik."
- ajross 11y agoAck, no. Bind caps lock to Ctrl, and escape is now the trivial chord C-[. As an added benefit, getting used to this layout will help you out with migrating out of vim to emacs.
- fernandotakai 11y agoyup. i find binding ctrl <-> caps way more useful than binding ctrl to esc (and my keyboard even has a ctrl key made for the caps place and a caps key made for the ctrl position). BUT there's also a way to do something like: if you press and hold caps, it acts like control. but if you press and release quickly it acts like esc. stevelosh describes how to do that on osx here http://stevelosh.com/blog/2012/10/a-modern-space-cadet/#controlescape http://stevelosh.com/blog/2012/10/a-modern-space-cadet/#cont... but i still haven't found a way of doing that on linux.
- zipperhead 11y agoSee link to xcape in this thread.
- Niksko 11y agoGood tutorial. Looking forward to combing over my vimrc tomorrow when I have some time. It's similarly copied from somebody else's many moons ago, and there are things that it does that I don't really like but haven't been bothered to change (eg. sometimes I want 2 space indents instead of 4).
- nightcracker 11y agoI don't understand why you'd want to map leader to anything but space.
- weaksauce 11y agoI fully agree. I think it seems like this key that people think is special like caps lock. The symmetry afforded by the space key and every other key on the keyboard is phenomenal.
- sandGorgon 11y agoI just realized something - my vimrc is 12 years old (carried on from my college). It has all my quirks and shortcuts that doesnt make sense to anyone but me. Every now and then, I pick up sone cool stuff from other vimrc and it becomes part of my own. What I just realized is that my Dockerfile is becoming something like that. Everything from the way the file system is laid out to the supervisord config is something that has become familiar. I do my development in it and its already a year old. All my various production servers use the same base Dockerfile (it includes Ruby, Python, Nginx, Openssl, Golang and Java!).
- welder 11y agoA must have plugin for moving to the middle of a word: https://github.com/bkad/CamelCaseMotion https://github.com/bkad/CamelCaseMotion
- platz 11y agoBadwolf is great
- deltaprotocol 11y agoI'm seriously interested in such an article for Emacs. Anyone?
- curiousdude99 11y agoCheck out spacemacs
- deltaprotocol 11y agoI'm a relatively long time Spacemacs user, and just recently dropped it exactly because it abstracts too much, and I wanted Emacs keybindings. I've failed to comprehend how it answers my question. Thank you anyway.
- gnuvince 11y agoA couple times a year, I'll scour a few .emacs.d repos on github and see if I can find something interesting in there.
- dllthomas 11y agoOne thing I've added fairly recently is: try source ./.vimlocal catch /^Vim\%((\a\+)\)\=:E484/ endtry which lets me tweak my settings (project specific key bindings, &c) by dropping a .vimlocal file in the directory I'm editing from. The try-catch ignores the error raised when the file is absent.
- uniqueusername1 11y agoWhy not check for file existance, instead of try-catch: if filereadable(expand("~/.vimlocal")) source ~/.vimlocal endif
- dllthomas 11y agoI've no strong objection to that - it's racy but not in any way that probably matters. Otherwise, it's mostly just a question of forgiveness vs permission...
- sriram_sun 11y agoCame here to say "Don't put anything in your vimrc that you don't understand". Pleasantly surprised when I clicked the link!
- curiousdude99 11y agoMy advise is to migrate to spacemacs
- halayli 11y agonothing special about this vimrc. This is what I have: https://gist.github.com/halayli/ef3dd268986914ed77d5 https://gist.github.com/halayli/ef3dd268986914ed77d5 At the very least, you need vim-session if you work on multiple projects (or branches) vim-airline is a nice to have Ag is a must have. it allows you to search files and preview the results directly. I've also written a small function SwitchHeaderSource to switch between .c/cpp files and their headers. ,s is the shortcut.
- dmytrish 11y agoThe `ToggleNumber()` function may be shortened to :set invnumber There two kind of prefixes for `:set` variables: `no-` and `inv-`, the first one turns the variable off or empties it, the second just toggles between states.
- edanm 11y agoCool trick. FYI, you can do the same with :set number!. Adding ! at the end of a variable will invert it. Adding ? at the end will echo its value.
- fantastick 11y agoNice writeup, give Sift a chance - https://sift-tool.org/info.html https://sift-tool.org/info.html - a nice replacement for the silver searcher
- ende42 11y agoOfftopic. But still: Thanks. Nice.
- sac2171 11y agoI've played around with vimscript and my .vimrc many many times, and always found it very unwieldy. I've started using a new and very different approach, which involves using the amoffat/snake library to write my new .vimrc functions in python. This is definitely not for everyone, but I've been able to do some pretty cool stuff with it, like creating a function for writing trace like debug statements, or sending the currently selected vim lines to a shell via the window manager/os level, rather than a terminal multiplexer like screen. If anyone else is interested, an example can be found at gist.github.com/sac2171/ce7b086421e8387193fd
- pan69 11y agosyntax enable " enable syntax processing The comment should be enough to describe this one. Sorry, no it's not. The syntax processing of what? The syntax of the current file loaded? Some sort of commands?
- rane 11y agoA good example of a completely useless comment.
- james2vegas 11y agothe best vimrc is the one that doesn't exist, so that vim runs in compat mode
- 0x54MUR41 11y agoI just started using VIM. But, I got stuck when I was working VIM along with `tmux`. How do we configure it well with `tmux`, for example?