4 ms·
The 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
by lucb1e 6y ago
The 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).
- wwweston 6y agoI hit :w so reflexively and regularly in working files that swap files only occasionally saves my butt: when I've done substantial work in a new buffer that I haven't figured out how to name yet and there's a crash. This happens just often enough that I have to regard it as useful, but it's also less common than crashes that leave me with swaps to contend with across a dozen files that really were pretty much fine. This is a frequent annoyance. So: occasional big help vs frequent annoyance. I don't think the problem is actually swap files themselves, though, but the user experience with them. What vim does is prompt you to choose what to do before you've even had a chance to compare the swap with what's saved, and provides no help with the comparison. What'd be nice is if it gave you some kind of visualization of a diff between the two and then prompted you for a choice.
- lucb1e 6y ago100% agree on that last paragraph. For what it's worth, Vim never crashes on me. For me swap files are mostly helpful with broken connections, killed VMs that apparently still had a vim session open in a faraway terminal, or other such situations.
- lloeki 6y ago> I don't think the problem is actually swap files themselves, though, but the user experience with them. Agreed, the UX is 99% the reason of me hating swap files. More often than not I choose recover, only to discover the diff is literally null. Come on, you could not check for that before interrupting? And then in any case you have double the annoyance with that recovered buffer as the old swap remains and you have to delete it or you get the warning again on the next open. It’s kind of like changing branches with git and then back again, vim notices the time stamp of an opened file changed vs the last buffer save, lets you change the file but complains on :w that the file “changed” outside, yet is unable to a) show you a diff of current buffer vs current file, nor b) realised that the filesystem state vs buffer state at last save (which vim fully knows about) produces a null diff, so the buffer changes are safe to write.
- lucb1e 6y ago> only to discover the diff is literally null. Note that you can save the trouble of running diff by watching for the message "Buffer contents equals file contents." Only when they're different, you'll want to write the file to another place and diff it. What bugs me a lot is that, even if the contents are equal, it doesn't cleanup the swap file... is there maybe a command for that so I don't have to go back to my terminal, manually remove .<filename>.swp, and open up vim again?
- weaksauce 6y agoinoremap jk <Esc> inoremap kj <Esc> are essential to me... press both at the same time and you exit insert mode regardless of which one gets pressed slightly faster and you never leave the home row and if you press it when you are not in insert mode it's basically a nop unless you are at the start or end of the file. even if you are you might move down or up a line accidentally but i've never had that happen personally.
- nishparadox 6y agoThis is the first thing in my config, whenever I try to setup in a new system. Now "kj" and ":w" has become more like a muscle memory, even when I am just staring at the abyss...
- kzrdude 6y agoThen you can't type words that have jk or kj in them, right? :) Depending on languages used it could be a blocker
- weaksauce 6y agoyou certainly can. it just requires you to wait whatever the timeout is set to. it doesn't come up all that often and i've used it for a long time.
- hopw_roewur_ne 6y agoI tried that, but found it annoying to occasionally get kicked out of insert mode while writing. I ended up rebinding Caps Lock to Escape instead, with Shift+CL to toggle caps; I quite like this rebinding even outside Vim. Two lines in autohotkey on Windows, or setxkbmap in X11.
- JNRowe 6y ago> relative lines might also be worth checking out: "set relativenumber". I really like the behaviour of `set number relativenumber`, where it shows me my actual line number of the current line and relative numbers on the lines I'm going to operate on.
- xyzzy_plugh 6y agoYes, I resent that this is for newbies some how. It's a small visual aid and unless you're using a tiny terminal it's hard to be able to do a 78d by sight.
- Lio 6y agoPersonally I get round some of these problems by using :x instead of :wq for write/quit. It's one letter shorter and less likely to be confused.