12 ms·
Vim: ZZ and zz: Do you know the difference?
- tasn 4y agoI've been using vim for 20 years and never knew about this. It's a life changer! (I should have looked it up ages ago...)
- bspammer 4y agoYou may also want to know about zt and zb, which move the current line to the top and bottom of the screen respectively.
- tasn 4y agoYeah, saw them in the post!
- nurettin 4y agoThere are a lot of us dinos out there who haven't even used hjkl to navigate. All I do is yy, dd, p, ctrl-v to rectangle select, shift-v to line select, g or G to navigate c and the occasional s/^/#
- WastingMyTime89 4y agoAnd it's a perfectly fine way to use vi or vim as a basic editor present on every POSIX system. Yet, as a long time user, I regret that beginners are often presented with a list of commandes to memorize and are never introduced to what makes vim actually nice: composing scopes and actions. I have the same issue with the s command. Most people treat the parameters as something slightly magical to be googled when needed. Meanwhile, reading the ed manual was eyes-opening for me. It really helped me understand both where vim comes from and what sed does.
- michaelteter 4y ago> hjkl These movement keys are at least 40% the reason I love and use vim or vim plugins. Not having to move my hand, and being able to “drive” with only one hand are immensely beneficial. With cursor keys, it feels like half of my editing time is spent finding the arrow keys and then finding the home typing position. Emacs ctrl n/p/f/b are better than cursor keys often, but they require two hands.
- R0flcopt3r 4y agoC-f, C-e, C-b, C-a only require one hand. But I see your point. The best part of evil-mode in Emacs is that I get to use Emacs bindings in insert mode, and then vim keys in normal mode. For instance when writing function signatures, I can hit C-f to easily jump over the closing parenthesis, or C-e to easily jump to the end of line, usually also to skip the closing parenthesis. I'm not sure how you would do that in vim without exiting back into normal mode and using for instance S-a to append at the end of the line.
- julesnp 4y agoYou can use this plugin to get the same mappings in insert mode for Vim: https://github.com/tpope/vim-rsi https://github.com/tpope/vim-rsi
- nudome_lata 4y agoI'm a long-term emacs user who recently learned vim for fun. Deciding when to exit insert mode has been part of the learning curve. From insert mode, vim lets you run one command without explicitly switching back to normal mode. :he i_ctrl-o for details. It's very handy for positioning the cursor for more input, for example ctrl-o f) Also works in evil.
- aidenn0 4y agoAnd other dinos who use "hjkl" because they are significantly faster than arrow keys over a 120cps terminal...
- glaucon 4y agoGetting a database connection error when I try to reach that page but this works ... https://web.archive.org/web/20230109091524/https://www.programmerhat.com/vim-zz/ https://web.archive.org/web/20230109091524/https://www.progr...
- _blz2 4y agoWhy are vim/neovim tricks/plugins/basic commands being spammed on hn recently??
- nbzso 4y agoI like those "spams". This is the real value of HN. Don't you think? Or we have to cope with "the usefulness" of the new reddit like trend?
- distcs 4y agoI can empathize with the parent comment and understand why basic commands about Vim can be bothering them. Many of us (and perhaps the parent commenter is one of them too) come to HN to look for interesting news and articles. Something interesting that has happened in the tech world. Or some interesting knowledge about the technology tools we work with or the real life world we live in. It so happens that rehashing of Vim's documentation on basic commands does not make it interesting enough. When articles on these basic commands (that are easily and better learnt from Vim :help) come on HN again and again, it can lead to a boring reading experience. It stops being interesting. For some people, this can reduce the real value of HN. I'll confess that my comment may appear as elitist. But hey, at least I am being honest, from my high-horse, elitism and all. If you can get past the thorny issue of elitism, perhaps you can see my point. My point is that I appreciate why these articles appearing too often can be bothering to some. I hope you can appreciate it too. That's not to say that there is anything wrong with these posts. If the community likes it and upvotes it enough, so be it. There is a "hide" link to hide the story if it is really bothering someone. A few basic Vim articles every week is not really a big problem even if it ends up bothering some people. I can live with it and I hope the parent commenter can live with it too.
- johncoltrane 4y agoThere is nothing elitist in pointing people to freely available and easy to follow documentation. Quite the contrary.
- simplicio 4y agoSince I learned this the other day on HN, Ill add a minor nitpick: "ZZ" doesn't save and quit the article suggests, but saves if modified and quits.
- kzrdude 4y agoI only use zz and have no use for ZZ. I guess it's funny from the outside that these are so similar but different. zz: Center view vertically on the cursor line. ZZ: save and close.
- frederikvs 4y agoI really wish the article would give easy to remember explanations. E.g. it mentions :wq means "exit after saving". Explaining :wq as "write and quit" makes it a lot easier for new vim users to remember.
- in9 4y agoBut that only goes so far. Some of the key combination do have a nice mneumonic attached to it. But it definetly can't scale. ZZ is one of those, which is the case with most key combinations :/
- WorldMaker 4y agoMy experience someone somewhere generally has a mnemonic for even the weirdest vim key combinations. Even ZZ probably has a mnemonic at least one person uses. Mnemonics sometimes have a lot of brains to scale on.
- iamevn 4y agoHuh, I wouldn't have been able to say what zz does from memory despite using it all the time. I think this is because I am on my phone, away from a keyboard and it's the sort of motion where I don't ever even think about what key I'm hitting, I just do it. I'd need to be at a keyboard to access my muscle memory.
- adql 4y agoI was thinking about writing down some visual keybindings map precisely for that reason, I have gathered a bunch of muscle memory about shortcuts I set up but it would take a bit of effort to remember what one was if I didn't use it for a while
- rgoulter 4y agoI'd recommend against trying to memorize Vim keybindings by position. 1. If you want to learn a non-QWERTY layout (e.g. Dvorak or Colemak), you're going to have trouble. (Although some users to try and go to the effort of remapping to suit the layout). 2. Similarly, you might also have trouble if you get a fancy keyboard that doesn't use a row-stagger, like a kinesis, moonlander, crkbd etc.
- WorldMaker 4y ago3. Many of vim's keybindings accreted mnemonically in the first place. There's often a mnemonic explanation or back-explanation for every single keybind. Many people communicate with them ("just yank those four lines"), and they form a sort of vocabulary to the "language" of vim normal and visual modes.
- bajsejohannes 4y agoI had the exact same experience, but if I pictured pressing them in my mind's eye, I realized what they did. It reminds me of some bilingual kids I know that can understand their dad's language perfectly, but they have a really hard time speaking it (since they always reply in their mom's language). You only learn what you practice.
- sillysaurusx 4y agoGah. I never used ZZ, despite using zz all the time, so I went to my editor and started hammering the Z key and ended up closing most of my files. Well played. This is a nice way to check whether you read the article first or last. What's up with thirty different ways to exit vim? ZZ, :q, :wq, :x, good lord! Vim must be the most effective editor as long as you want to not use it. :)
- worksonmine 4y ago:wq is still :q, just saving before exiting. The flavors are just different ways to deal with changes, with ZZ (:x) and ZQ (:q!) being aliases to the most useful ones. This is not unique to vim, other editors handle it with prompts and buttons.
- Izkata 4y agoThen there's also :wqa and :xa, which are like :wq and :x except they apply to all open buffers instead of just the current one.
- I_complete_me 4y agoI think you'd enjoy this then: https://github.com/hakluke/how-to-exit-vim https://github.com/hakluke/how-to-exit-vim.
- sillysaurusx 4y agoHahaha. The emacs way is awesome.
- glaucon 4y agoThis is far funnier, and informative, than I expected. Love the time out approach when aliased to 'vim'.
- michaelteter 4y agoJust use as little or much as you like. You don’t have to use every variant of a type of command. Shift-zz is great as it uses only one hand and can be done in a rolling sort of movement.
- pawelduda 4y agoQuick pro tip for zz, I pair zz with search/jumps, so the next match/jump location ends up centered. Edit: more elegant solution https://news.ycombinator.com/item?id=34308688 https://news.ycombinator.com/item?id=34308688 " Center screen on next/previous selection. nmap n /<CR>zz nmap N ?<CR>zz " Last and next jump should center too. nnoremap <C-o> <C-o>zz nnoremap <C-i> <C-i>zz
- vault 4y agoThe source you copied this from offers a simpler version: " Center screen on next/previous selection. nnoremap n nzz nnoremap N Nzz " Last and next jump should center too. nnoremap <C-o> <C-o>zz nnoremap <C-i> <C-i>zz
- pawelduda 4y agoI copied it from my vimrc, I don't recall after many years where it came from :)
- 2mur 4y agoI pair it with half-page ups/downs. The cursor is always on the center line for easier vertical movements after scrolling nnoremap <C-d> <C-d>zz nnoremap <C-u> <C-u>zz
- fleekonpoint 4y agoVery cool!
- multani 4y agoI use ZZ all the time, but it's so wired into my memory muscle that i had to read the article to realize I was actually using it. Also, zz is _so_ practical, it's probably the shortcut I miss the most when I'm not using a vi-compatible editor...
- michaelteter 4y agoAnd somehow after 30 years of vi use, I never knew of or used zz. I use ma, mb, etc and ‘a ‘b and ‘’ to mark and jump around, beyond the other options.
- apetrovic 4y agoMost editors have zz functionality. Ctrl+L for VSCode, JetBrains tools, and SublimeText, for example.
- quanticle 4y agoAll of whom copied it, in turn, from emacs :)
- nzach 4y agozz is a neat command, however I find it a little cumbersome to use. I prefer to use the scroll offset[0] to keep the cursor line always at the center of the screen. I've been using 'so=20' for a long time now and it feels great. [0] - https://vim.fandom.com/wiki/Keep_your_cursor_centered_vertically_on_the_screen https://vim.fandom.com/wiki/Keep_your_cursor_centered_vertic...
- endorphine 4y agoSame here. I didn't know about zz and while reading the comments here I didn't understand what all the fuss was about. Then I realized that I use some alternative setting that makes zz kinda redundant. I use `set scrolloff=3`.
- taway23234234 4y agoThe only problem with this is when you are actually at the bottom of the file. But great tip in general!
- proto_lambda 4y agoI have a feeling to author doesn't know what "parsing" means.
- deleted 4y ago[deleted]
- jacknews 4y agoYes, what is meant by 'parsing the commands with a search'? Just performing zz after the search, ie 'pairing'? I couldn't figure it out otherwise.
- hamburglar 4y agoThat last example is also completely unhelpful for figuring out what he’s talking about because it doesn’t actually show you the keystrokes it uses.
- imron 4y agoOf all the z’s my favorite by far is Ctrl-z, which send the current application to sleep and drops you back to the terminal, where you can do whatever you need before using ‘fg’ to return the slept process to the foreground. It works with everything but I use it with vi most of all. You can even sleep as many processes as you like and choose which one to return to the foreground as needed.
- caymanjim 4y agoI almost never suspend vim, because I run it exclusively inside tmux, and it's a lot easier to just open a new window/pane, run something, and close it when done.
- Lapsa 4y agoyap... same here
- kagevf 4y agoIt's useful running vim in git bash on Windows. I'll switch, though, if I ever figure out how to install tmux in that environment then I'll switch.
- caymanjim 4y agoThere's no good reason to run git bash on Windows anymore either, since you can use WSL!
- kagevf 4y agoActually, when I tried to use git on Win10 it worked at first but then at some point stopped functioning. Now that I'm on Win11, I definitely need to revisit that - thanks for the reminder! The only downside, though, is the clipboard won't be as easily accessible. I'm thinking of things like piping text to clip.
- 4y ago
- raverbashing 4y agoHere's a vim pain point: a lot of commands should be case insensitive Because it is deeply frustrating when Vim complains about :Wq "not existing" well, duh. A lot of people remap those to lower case so that Wq works (also Q!, etc)
- urxvtcd 4y agoYou might know it, but all of vim's built-in commands start with lowercase letters, but it's impossible for users to define new commands starting with lowercase letters. Kind of a namespacing I guess.
- unsafecast 4y agoHah, I thought I was the only one who lifts shift too late and gets :Wq.
- atahanacar 4y agoI set up my vim so that :W is "save as root"
- nathias 4y agoI mostly use zz paired with g for broad movement, I'm thinking of incorporating marks for working with large files.
- Aachen 4y agozzg doesn't do anything differently than just zz (the g needs an argument). Care to share an example of what you meant?
- nathias 4y agoI just meant the G, gg, 22G etc. commands. There are other z commands like zt and zb, but I don't really have a use for them.
- petepete 4y agoI find marks tricky to work with. I've made several attempts to get used to them over the years and it never really stuck or felt intuitive.
- stitched2gethr 4y agoI'm often surprised and intrigued by what makes it to the front page.
- distcs 4y agoIt takes the minority of users like me who visit https://news.ycombinator.com/newest https://news.ycombinator.com/newest often and upvote new stories that look interesting. If it gets 4-5 votes early (like within 30-45 minutes), it usually makes it to the front page. BTW I did not upvote this story.
- iamflimflam1 4y agoWould be very interesting to see how many hits the newest page gets relative to the main pages.
- dm319 4y agoTips for developers' most loved editor gets a lot of positive attention.
- anthomtb 4y agoSame here. You would think editor-related content would make the front page more frequently.
- kjetil 4y agoThere is actually one more way to quit vim. If you only have one window open in vim, Ctrl-W Q will exit.
- gigatexal 4y agobeen using neovim for years now and I didn't know this. I've only recently started using ctrl+c to move from input mode to normal mode
- 0daym 4y agoI set scrolloff=8
- tromp 4y agoAlso useful are ctrl-e and ctrl-y to shift the whole screen up or down while keeping the cursor on the same line.
- devnullbrain 4y agoYes, I have read :help.
- Lapsa 4y agocool! didn't know z. z-
- dm319 4y agoI use zz regularly, especially when taking notes, which tend to move the cursor towards the bottom of whatever you're using to write in. This is bad for your posture, so a regular 'zz' helps to keep my neck from straining. Another nice thing about vim is that it will scroll beyond where there is text. Once you have more than a screen of text in almost any text editor or word processor, it is often hard to get the bottom of the file to the top of the screen.
- Anarch157a 4y agoI'm a native Portuguese speaker. ZZ makes a nice pun in our language: "Zalva e Zai", from the words "salva" (save) and "sai" (exit). It might work for Spanish too.
- nequo 4y agoAnd zz could be “zzentro?”
- mtlmtlmtlmtl 4y agoI almost never use zz. I prefer setting scrolloff to a fairly high number like 50. Scrolloff short for scroll offset is the number of lines vim ensures between the cursor and the modeline, as well as between the cursor and the top of the screen. Super useful for always keeping a good amount of context visible. You can even set it so high the cursor is always centered.
- tpoacher 4y ago> Do you waste a lot of time trying to bring the current line of code to the center, going all the way down and then all the way up to reposition the editor’s viewport? Well, I used to do that too… But then I switched to nano.
- deleted 4y ago[deleted]
- twobitshifter 4y agowhat’s the mneumonic here? ZZ = sleeping zb = zoom bottom zt = zoom top z. = zoom end of line (like a period) but it’s not really “zooming” it’s scrolling or panning.
- GavinAnderegg 4y ago`ZZ` is great when you want to save. If you're looking at a read-only file, or just want to quit a file without unsaved changes, I personally prefer `Ctrl-w q`. I do a lot of windowing in Vim (controlled with `Ctrl-w`), and if you follow that with `q` (and you're on the last window of the session), it will quit Vim. I have Ctrl mapped to Caps Lock, so I can do this quite quickly.
- _blz2 4y agoThe previous post about ':x' was posted by the author of the article in the OP. I suspect the author of the article using multiple HN accounts to shill this.
- fleekonpoint 4y agoI use zz a lot, I also like C-d and C-u which scroll the document up and down without moving the cursor. I've mapped these to C-j and C-k to make them easier to use: nnoremap <C-j> 4<C-d> nnoremap <C-k> 4<C-u> vnoremap <C-j> 4<C-d> vnoremap <C-k> 4<C-u> I set it to scroll 4 lines at a time but you can tweak the number to make it scroll faster or slower. Unrelated but I also have a command to remove highlighting from search results: nnoremap <silent> <C-l> :nohl<CR><C-l>
- stevenhuang 4y agoJust be careful and don't accidentally mash :X<junk><CR> otherwise you'd have encrypted the file and thrown away the key :)