12 ms·
Five lines I put in a blank .vimrc
- lilyball 6y agoKeeping the swapfile on is really helpful when I accidentally open the same file in two different vim editors. But I do recommend configuring vim to place swapfiles in a different location, so you’re not scattering them all over your disk.
- climb_stealth 6y agoI actually went full on in the other direction and often have multiple vim instances working on the same file. It feels so much better than having to deal with swap file error messages. With the below it reloads the file when the window gets into focus. Super helpful when working in lots of terminal tabs. You just have to get into a habit of saving after every change as it conflicts otherwise. " check for and load file changes when a window is focused. Together with " `noswapfile` this allows to edit the same file in multiple nvim instances and " have the changes reloaded whenever the focus is changed. autocmd WinEnter,BufWinEnter,FocusGained * checktime
- throwaway744678 6y agoCan't you also auto-save when losing focus?
- climb_stealth 6y agoHah, I would think so. I'm not really familiar with the scripting side and end up searching for solutions online. See [0] for something to try. [0] https://vim.fandom.com/wiki/Auto_save_files_when_focus_is_lost https://vim.fandom.com/wiki/Auto_save_files_when_focus_is_lo...
- MinusGix 6y agoMaybe could have it work on a cloned temp file, and then autosaving on losing focus. Opening the same real-file would lead to the same temp-file. Might be able to make manual :w save the temp to the real, but may be better to have it as a separate command.
- em-bee 6y agowhy would you need to open the same file in multiple tabs instead of switching tabs to get to the file. in the few cases where i really need to open the file multiple times i use tmux to allow a shared view.
- climb_stealth 6y agoMostly because I'm using kitty [0] and the tab and split-view handling in it is super quick and effortless. For example instead of opening a :vsplit in vim I just split my terminal and open a second instance on vim on the same file. You lose pretty much all the advanced buffer handling that vim offers, but I never used them much in the first place. It's a bit crude but I really like how it results in a uniform way of working in the terminal. As opposed to handling different files and processes depending on what applications is currently running. [0] https://sw.kovidgoyal.net/kitty/ https://sw.kovidgoyal.net/kitty/
- em-bee 6y agooh, i see. in large files you may want to see multiple sections to compare them. i usually use less for that, view (which is readonly vi) also works well. and of course i realize that opening multiple tmux views with the same window doesn't even help with that.
- CalChris 6y agoI'm not going to set noswf. However, I run into the problem the writer mentions all the time where I'm opening an already opened file. Is it possible to just switch to the already opened file?
- kolme 6y agoNo, because it's open in a different instance. You can use one vim instance for all your editing needs and you'll never see that error again. And you probably should, because vim is rightfully warning you, that you might lose changes made in the other instance.
- allis10 6y agoYes, it is (at least on Mac in 2013): https://youtu.be/aHm36-na4-4?t=749 https://youtu.be/aHm36-na4-4?t=749
- CalChris 6y agoThat pretty much looks like what I want. I use VimR on the Mac with its multiple windows as opposed to multiple terminal window and vim. But this is close and I'll see if it work with VimR.
- toxik 6y agoI'd prefer one line it and use the "long" names and add nocompatible set nocompatible hlsearch incsearch ignorecase number then sane tabs set et sts=4 sw=4
- pletnes 6y agoWhy nocompatible?
- kolme 6y agoNot necessary, AFAIK, unless you invoke vim using the alias "vi". If you call "vim" it'll start in nocompatibe mode by default. The nocompatible mode means adding some features to vim rendering it "incompatible" with the original vi. For example in the original vi you can only undo the last action, with compatible mode on vim will mimic this behavior.
- toxik 6y agoTakes a whole stroke off your shell game!
- JoshTriplett 6y agoI switch between enough different projects with different coding styles that I have shortcuts to change between them: set expandtab set shiftwidth=4 set softtabstop=4 function Spaces(...) if a:0 == 1 let l:width = a:1 else let l:width = 4 endif setlocal expandtab let &l:shiftwidth = l:width let &l:softtabstop = l:width endfunction command! T setlocal noexpandtab shiftwidth=8 softtabstop=0 command! -nargs=? S call Spaces(<args>) autocmd BufNewFile,BufRead ~/src/linux/* T autocmd BufNewFile,BufRead ~/src/git/* T autocmd FileType html S 2 autocmd FileType tex S 2 This sets the default to 4-space indents, but lets me do :T to switch to tabs (and does so automatically for Linux and Git which use that style), or :S N to switch to N-space indents (and automatically switches HTML and TeX to 2-space indents).
- 6y ago
- strogonoff 6y agoWhy not one line? set hls ic is nu noswf On a serious note, personally disabling line numbers is the first thing I do in a new editor. Reduces visual noise—status line usually shows current line number anyway, jumping to a line might take a second and I found I don’t need to know my line numbers more frequently than once per month or so. What I do like to enable in an editor is visible whitespace characters (helps notice levels of indent, for example). In Vim that can be accomplished with: set list listchars+=space:• (Might take some tweaking to make whitespace appear subtly enough, depending on your color scheme.)
- falcor84 6y agoInteresting. The first thing I always do in a new is enable the line numbers. I pair-program a lot and find them to be invaluable when talking about the code.
- strogonoff 6y agoI can see how line numbers would be immensely useful if I ever pair program remotely.
- Ancapistani 6y agoLine number are super helpful for navigating in vim, though. :54 when you’re on line 20 is much faster than any alternative.
- repsilat 6y agoSome folk like `34d` so they don't need to press shift. And sometimes set relative line numbers so they don't need to do the subtraction. Others just search for known text, which IMO is a habit I need to get into. I "got" hjkl after I disabled the cursor keys for a day, but I never followed up and disabled hjkl (or maybe even w and b!?) to get super efficient.
- strogonoff 6y ago
- BalinKing 6y agoIn Firefox on macOS, the font fallback to "Apple Color Emoji" is doing really weird things on my machine (like cutting off the top of the number "5"). With that said, thanks for the tips—`set noswapfile` definitely fits my workflow more than not.
- jiannengli 6y agoOh wow I had no idea that it looks so bad on Firefox. Thanks for pointing it out, I will look for a fix. Edit: fixed by removing "Apple Color Emoji" from the list. Thanks!
- pletnes 6y agoI have «relativenumber» but I’ve never touched swapfile. Otherwise I have the same 5. Funny - I often felt vim’s defaults are almost intentionally archaic.
- bonquesha99 6y agonmap ; :
- Isognoviastoma 6y agoYes, that's so relaxing for little finger. In fact my minimal vi config is :nnoremap : ; :nnoremap ; : :nnoremap Y y$ :vnoremap : ; :vnoremap ; : :vnoremap Y y$
- airstrike 6y agoI go with nnoremap but yeah, this is the absolute first edit to my .vimrc
- inshadows 6y agoHow can you live without f/t followed by ;?
- gpvos 6y agoTastes differ; I would never set 'ignorecase' as a default, and instead of 'number', which is only occasionally necessary and most of the time is visually disturbing, I'd set 'ruler', or a slightly more fancy 'statusline'. The most important one, however, for me is 'set guicursor+=a:blinkon0'.
- repsilat 6y agoUsually smartcase does what I want. The only time it doesn't is when I really really want just lower-case characters, but it's a very good heuristic.
- city41 6y agoLike scooble mentioned, you can add \C to the front of the search to force it to be case sensitive.
- scooble 6y agoI went back and forth with ignorecase until I discovered that placing \c in the search pattern will make that search case insensitive. This makes it much easier to do the odd case insensitive search but keep the default case sensitivity. (\C does the opposite if your default preference is the other way).
- leephillips 6y agoIt’s not enough. You should go all the way: set nobackup set nowritebackup set noundofile set noswapfile I explain why at https://lee-phillips.org/badvim/ https://lee-phillips.org/badvim/ Also, it’s best to just have one instance of Vim running, in server mode, so you can send files to it from the command line.
- bananicorn 6y agoWhy would you not just set the location of the backup and swapfiles to a fixed place in your system? Try this: set backupdir=~/.vim/backups,. set directory=~/.vim/swapfiles,. It will only write in the current directory, if ~/.vim/backups does not exist, and the dot is just there for portability, so nothing breaks when using my vimrc on a new system. Edit: Upon actually reading your blog-post, I understand at least one reason - my options may not change the behaviour of a file not being written to the correct inode, so you have a point for some build systems. I actually learned rather much reading this, thank you for sharing!
- leephillips 6y agoIn fact, I think your idea is better, and I’ll probably switch to doing that some day, just in case one of these files turns out to save me. Edit: Hmmm. I’ll have to think some more about it. Anyway, glad you found my witeup useful.
- nemetroid 6y agoYou should end your swapfile directory setting with two slashes, i.e.: set directory=~/.vim/swapfiles//,. From the help text for "directory": > For Unix and Win32, if a directory ends in two path separators "//", the swap file name will be built from the complete path to the file with all path separators replaced by percent '%' signs (including the colon following the drive letter on Win32). This will ensure file name uniqueness in the preserve directory.
- rendall 6y agoI laughed at the poetry of the line "I don’t really want Vim to litter my filesystem with all of these piles of nervous energy." Truth
- lucb1e 6y agoThe swap file saved my work a number of times. If you never use recover and use Vim regularly, then go ahead and disable it; if you haven't used it because you don't know how to use it or you only rarely use Vim, then I'd encourage you to either try it first or leave it enabled (as it is by default) until it becomes a nuisance. And like u/strogonoff, I also disable line numbers, but that's just personal preference of course. For vim newbies, relative lines might also be worth checking out: "set relativenumber". One thing that really annoys me is :Wq or :Q not being commands. Or typing only Q because you didn't hit the : and accidentally entering ex mode. Or hitting F1 instead of escape. I can recommend: command Wq wq command WQ wq command W w command Q q nnoremap Q <nop> map <F1> <Esc> imap <F1> <Esc> Or for the non-newbie that has the 'too many terminals' problem, "set title" will give your terminals more useful titles. There are a bunch more things, of course, but these are the lines in my vimrc that I think are useful to most people.
- OJFord 6y ago> Or typing only Q because you didn't hit the : and accidentally entering ex mode. I'm not certain this is different (because I don't use either deliberately) but I'm forever hitting q (not even trying to quit) and entering some kind of macro recording mode. I think the only reason I haven't yet disabled it like you suggest (after doing it for years now) is that... Well 'some kind of macro recording' does sound like it might be pretty useful if I bothered to learn to use it!
- scns 6y agoMacros are really nice, you can automate transforming several lines of text with them. The Vim plugin in Intellij supports them. Worth learning IMHO.
- lucb1e 6y agoIndeed, I would not recommend disabling q unless you know what it does and tried it a few times. For a quick demo, try it on this file: hello world! here's wolves what's up with the hackers? Go to the first line and do: 1. qq starts a macro named 'q' 2. $vbgUj select last word on line, uppercase it, go down one line 3. q to end macro recording 4. @q run the macro 4b. @@ repeats the last macro 4c. 100@q runs the macro 100 times When you want to do multi-line edits and the columns are not aligned (so block mode, Ctrl+V, doesn't work), this works wonders. You just record whatever you would do on every line plus whatever motions you use to go to the next occurrence, using things like "f|" (find the next pipe symbol) or "/example<Esc>n" (find the next instance of "example"), and then run the macro however many times. If you specify a number of times greater than the file is large, it will automatically stop at end of file, and you can also cancel if it takes too long (Ctrl+C) and undo what it did so far (u). Alternatively, sed -i s/a/b/ my.txt is much much faster than macros, but macros can be more complex and are often easier to record (since you're just showing the computer what to do in a human way) than trying to regex the line. I tried recording it for the lazy but asciinema is having issues (server error when uploading).
- phodge 6y ago`set confirm` is another great option to turn on all the time. It will save you having to retype a command with the `!` added.
- thih9 6y agoThese seem a bit too opinionated to me, mostly because of 'set noswapfile'. For me vim-sensible [1] has a more useful set of defaults (although more than five lines). Also, the readme of that project lists benefits of using a universal set of defaults. [1] https://github.com/tpope/vim-sensible https://github.com/tpope/vim-sensible
- ogre_codes 6y agoI find it odd that you would use the short version of a command in a config file. Brevity is important when you are doing repetitive tasks. In a config file (or code), clarity is more important that brevity. I know you could argue that you know VI so well that you know these commands off the top of your head.. even so, it's particularly pointless to list them this way in a blog where you have to turn around and explain them anyhow.
- wdobbels 6y agoAs discussed in the article, the point is to quickly set up vim on fresh linux installations (e.g. virtual machines). For the vim on your own pc, you would likely use the full version, in addition to whatever extra config you prefer.
- jiannengli 6y agoYes, I definitely am much descriptive for the config file on my personal computer. The configs I'm recommending here is more for ephemeral machines that you are using just to look at files (such as sshing into a VM that was created to run integration tests). For those cases, being able to quickly type something into the .vimrc and improve your experience is nice.
- ogre_codes 6y agoI guess I don't see a lot of situations where I use systems so casually that I don't have config options set. Even when I was managing a bunch of systems I would push out the same config on them all.
- Lio 6y agoOne scenario I could see this fitting is when having to connect to some docker container and test a change on the fly. That's a bit naughty but sometimes needed to prove something useful. In that case you've got an effemeral file system, and probably had to install Vim in the first place, knowing that that when you close the container all will be forgotten. Ofcourse in that situation you probably don't need to disable the Vim swap files, so that's one less thing to changet the defaults on. I often use Busybox for that kind of thing as you're usually missing other useful OS stuff and its little Vi clone is usually enough.
- ulzeraj 6y agoKinda derailing the thread but might ask anyway, does anyone got recommendations on dot files synchronization through personal devices? I'm currently using a git+ss repo on a personal server. I wonder if there are more convenient ways to do this.
- dbolgheroni 6y agoNot the simplest way of doing this, but ansible might be an option. IMO the advantage is being able to customize the configuration file depending on the host OS, IP address, attached filesystem, etc. This is useful because sometimes you want to use the same configuration file on different systems and they usually need some customizations to adapt to each one, which can be done automatically by ansible based on jinja2 templates.
- fpoling 6y agoI have a shell script that either symlinks dot files from my git repo or writes them. Then I use git normally.
- stonewareslord 6y agoI have tried a custom linking script, Ansible, and stow. The best I have found is to just use a bare git repo based off of: https://www.atlassian.com/git/tutorials/dotfiles https://www.atlassian.com/git/tutorials/dotfiles The idea is to make an alias that runs git so that it treats your home directory as a git repository and you can directly commit them. I would recommend bypassing that article's `config config --local status.showUntrackedFiles no` setting and setting a `~/.gitignore`. This way, git will never tinker with your home directory if you accidentally run the wrong command. So the complete list of setup commands is: # Only needs to be done the first time cat >~/.gitignore<<'EOF' * !/.bashrc !/.zshrc EOF # On subsequent runs git clone --bare ssh://git/cfg.git "${HOME}/.cfg" function config() { git --git-dir="${HOME}/.cfg/" --work-tree="${HOME}" "${@}" } config checkout -- ~/.gitignore config reset ~ config checkout -- ~ config submodule update --init --recursive --remote One caveat is adding files in subdirectories (`~/. config/...`), but just requires changes in your gitignore
- ogre_codes 6y agoI guess it depends if you are doing systems admin or coding, but when I'm coding, I've gotten very dependent on Fuzzy Search like what Sublime Text introduced. So for me, installing CtrP or FZF is pretty much mandatory (I've settled on FZF). I know this is about config options, and this is an extension, but for me, fuzzy search is pretty much essential for coding once you get to a moderate sized code-base.
- adrianmonk 6y agoAnother approach to the swap file thing: view foo # opens read-only, no swap file vi foo # opens read-write Another benefit of this habit is, if you know you're not going to want to modify a file, you can protect yourself against errors like accidentally changing something and then absentmindedly saving the file.
- jiannengli 6y agoNice, that'll come handy
- bluenose69 6y agoLine numbers can be quite handy when coding with somebody over a video connection. Instead of saying "I think the problem is 8 or 9 lines up from the cursor", it's just easier to say "look at line 314" or whatever. I seldom use j and k to go more than a line or two, preferring absolute or relative line jumps, and I find absolute jumps easier to do accurately, especially if I'm tired.
- 29athrowaway 6y agoWhat is the kakoune equivalent of these options?
- mawww 6y ago`hook global RegisterModified '/' %{ add-highlighter -override global/search regex "%reg{/}" 0:+u }` will underline the current search pattern There is no ignorecase option, `(?i)` is the way to enable that in a regex, you can `map global normal / /(?i)` to auto insert-it incsearch is the same `add-highlighter global/ number-lines` display line numbers for all buffers There is no swap file in Kakoune.
- arendtio 6y ago> Unfortunately, Vim’s default configurations lack several important usability features compared to popular alternative text editors, such as VSCode. I find it odd, to compare Vim with VSCode and pretend that those 5 lines close the gap (especially when 3 of those lines are primarily about search). My own .vimrc has 166 lines (custom snippets, key bindings, plugin configurations (e.g. powerline, tmux navigator) and comments) and I try not to use more plugins than necessary. I say that as someone who loves Vim. But not just for its features, but also for those that are not present (by default) and make it as fast as it is.
- aftbit 6y agoI've started writing set mouse="" into all of my Debian server vimrc files so I can still use my middle-click paste buffer over ssh.
- _wldu 6y agoDisabling swap files is smart especially if you edit files directly on a web server using vim. I've found index.php~ and .index.php.swp on a number of web servers over the years.
- chrismorgan 6y agoYou can set a few variables to change where these files go: • 'directory' controls where swap files go, toggled by 'swapfile' (default on), filenames like […].swp; ~/.cache/vim/swap// is a good choice. • 'undodir' controls where undo files go, toggled by 'undofile' (default off), filenames […].un~; ~/.cache/vim/undo// is a good choice. • 'backupdir' controls where backup files go, toggled by 'backup' (default off), filename […]~; ~/.cache/vim/backup// is an interesting choice. Arch Linux actually ships a runtime file that sets these paths, creating the directories if they don’t exist: https://github.com/archlinux/svntogit-packages/blob/packages/vim/trunk/archlinux.vim https://github.com/archlinux/svntogit-packages/blob/packages.... (Note that the double trailing slashes are significant and necessary if you’re using a central location for these files. Refer to the help for explanation.)
- coolgirlpewpew 6y agoCan vouch that this guy is 500% legit!! Amazing computer genius AND super nice!! And great dancer!!
- superasn 6y agoVery useful thanks. I've already added it in my .vimrc. One more setting that I find really useful is this: map <F5> <Esc>:w<CR> :!"%:p"<CR> Especially since I'm used to running my code with F5 since Delphi
- russellbeattie 6y agoI have a .vimrc I set up a decade or so ago. I can't remember what's in it, just that I have to comment out a few lines every time I copy it from a Mac to Linux or vice versa. In retrospect, I probably should have just gotten used to whatever the default is, because using Vim without my .vimrc doesn't actually feel like Vim to me.
- njharman 6y agoHoly crap, first 4 lines are cancer! HATE hilight, incremental, very much want case sensitivity, and don’t need wasted space and attention with numbering every line. Current line (and column) in status bar is enough. No swap file is good though.
- aliceryhl 6y agoMine are: inoremap jk <esc> nnoremap <space> :
- jlgaddis 6y ago> Also, as mentioned below, this is for temporary environments where you read more than you edit. It might be more convenient to just do a one-time setup of something like "bring your .bashrc, .vimrc, etc. with you when you ssh" [0] (previously discussed on HN [1]). Personally, I've got a shell script that I just scp over and execute to set up my dotfiles (in a git repo) if they aren't already present on a host, but that may not be an option for some (although I guess I could use sshrc to do that too). -- [0]: https://github.com/danrabinowitz/sshrc https://github.com/danrabinowitz/sshrc (note: clone of the original repo, which now 404s). [1]: https://news.ycombinator.com/item?id=8343469 https://news.ycombinator.com/item?id=8343469
- prussian 6y agoif you're a fan of incsearch, you may also be a fan of neovim's inccommand=nosplit as well.