Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Okay, here's one that I've been stuck with. What does Ctrl-Z do in vim, and how do I "undo" it? I always have to kill -9 the vim process, and it sucks cleaning up where I was.


tl;dr: type `fg`.

One nice thing about this setup is you can "stack" processes in the shell.

Say you're working on file_x, but file_x depends on header_file_y and file_z. file_x makes sense on its own, so you don't want to split a new pane; you just want to leave your workspace as it is and come back to it later.

Hit ctrl-z, and fire up vim with the new stuff you want. When you're done, quit the current vim session and come back to your old context with `fg`.

The key observation is this stacks inductively, so you cant tumble down the dependency rabbit hole and always come back to the context you were at originally. This feature alone has made it impossible to ever leave console vi.

It really came in handy in my Operating Systems class where I could fly around the codebase an order or magnitude faster than everyone else.

If you really need to go back to a particular context %[num] lets you go back to the nth suspended process, but this will corrupt your stack of processes, so `fg` won't be in a clean order.


That's an anti-pattern too. You should use one of the many ways of loading up multiple files in one vim session. I don't know what the best is, because I'm an emacs user, but I know there are many options.

Losing access all your editing state when you move to a new file is silly, regardless of editor.


It goes beyond just editing multiple files. I'm a huge fan of tabs in vim so I do a lot of :tabnew filename (which has autocompletion by the way) and mapped \tp and \tn to :tabprevious and:tabnext. I sometimes just open in a new file buffer like emacs does, no tabs and no window splits but when I'm done with the file I go back to the other one.

But I also use ctrl+z frequently, even more when I'm editing over ssh. Usually because I want to do something outside of the editor window not directly related to editing. Or it might be related to editing, it depends, I just want a shell. I may even have forgotten which file contains what I want to edit, so I might back out to a shell and use a find|xargs grep or ack command. Basically any time gedit users open the terminal for something in the middle of editing, I just ctrl+z (or open a new shell tab, but as mentioned ctrl+z is joy on ssh where a new tab means a new connection).

So I use ctrl+z. Also :!bash is stupid, but :r!cmd should be on the page. (It pastes the output of cmd into the file.)


Ctrl-Z to actually suspend vi is fine. (Quick shell commands can be done in vi of course, but sometimes when you need a shell, you really need a shell.) It was the recursive opening of vi I was saying is a bad idea.


Why not gt and gT? All the commiting/greping/chmoding/building stuff can be done from within Vim. You don't really need to get out of it unless you want to launch a curses program or an interactive SSH session. Eck, even in such cases I think it's way better to have multiple terminal windows or panes.


It's mostly habit for me not using gt and gT. I only learned :tabnext and :tabprev when I learned of tabs at some point, and the mappings followed shortly after. I have a lot of other \whatever bindings too. (Such as \gt that's mapped to :tab split<CR>:exec("tag ".expand("<cword>"))<CR>)

And yeah, you can do every shell action in vim with things like :make and so on and :! when a nice wrapper isn't pre-made, but at that point it starts to feels a little too emacs-y for me in a lot of places and again, frequently I ctrl+z for things orthogonal to what I was working on. Sometimes I do use those wrappers, of course, but it's unnatural in many cases and I think it's of questionable utility compared to mastering the command line normally or compared to other vim features. Linux is my IDE, vim is my editor. I like to have them work together instead of one dominating the other.


Vim's tabs are horrible if you insist on using it like other apps' tabs but they are really useful exactly for that kind of situation: just open a new tab with your "stuff from a different context". From there, switching from tab (context) to tab (context) is pretty easy with gt and gT. No inductive stack to corrupt, no complex mental projections…


and if you've gone so far down the hole you're completely lost, 'jobs' is a useful way (in most shells, I think?) to figure out where you are.


Others have already answered the gist of your problem, so I'm just going to add the following:

That's not a feature of vi(m), that's a part of some shells (bash, zsh). You can suspend any programming running in a shell with ^Z. In addition to getting it back to run in the foreground with 'fg', you could also put it in the background with 'bg' (which is equivalent to starting a program with '&' at the end), or detach it from the shell process with 'disown' to make it a distinct process instead of a child of the shell process (which means it will keep running when you close the shell, which is often useful).


It backgrounds the running process. Type 'fg' to bring it back to the foreground. This isn't actually a vim thing, it's a shell thing


Normally Ctrl-Z will pause a process and it will be kept as a background job. Type 'jobs' in your prompt to see running jobs.

If there is only one a '%' will bring to foreground the last job, otherwise you can do 'fg x' where 'x' is the job number.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: