4

I saw some kind of workflow in vim:
Vim had a file open. Then some combination of keys made vim disappear and the user was in the command line working in cli mode, then opened another file and then suddenly returned to the previously opened file exactly at the place/line he was.
It reminded me the switch among windows we do in Windows.
Does anyone know how this worflow is done in vim?

15

There are several options to do so:

  • You can use a terminal multiplexer like screen or tmux.

    In screen, for example, the shortcut Ctrl+a - a, has the same functiononality as Alt+Tab in graphical environments: switch to the last screen.

  • Or you use vim's internal function.

    Type :!command in vim's command mode. For example: :!ls -l. After the command finishes press Enter to switch back to vim.

  • There is one more option: Job conrol.

    Press Ctrl+z to stop the current process (vim). You will find yourself in a terminal. To bring the stopped process back to the foreground type fg.

For me, I prefer screen. I have an unwritten rule for myself: "Always open a screen."

  • Indeed, multiplexing would explain the "sudden" return. – muru Sep 16 '14 at 20:08
  • +1.Option :!command is excluded since I know about this and that was not used for sure. 1)How would you do (a) with tmux? 2) When you do Ctrl+z and start working in the cli can you still open vim again for other files? So the fg only would bring back the originally opened? – Jim Sep 16 '14 at 20:38
  • @Jim yes you can edit other files. You can also stop them too. In the link I provided, you see the available job control commands. With jobs you can display the currently stopped jobs and with fg 1 you can bring the first of the two back to the foreground. – chaos Sep 16 '14 at 20:47
  • What is the equivalent command in tmux to the one you mention in screen? – Jim Sep 17 '14 at 7:20
  • @Jim in tmux the equivalent is: ctrl-b l see this page for comparsions: dayid.org/os/notes/tm.html – chaos Sep 17 '14 at 9:45
4

You can press Ctrl-z to stop vim and go to CLI, do whatever you need to (edit another vim file perhaps), then press fg on command line to return back into vim at the same place you left off at. If you didn't see the command fg being typed, then it's very likely that screen was being used.

  • After pressing Ctrl-z you can still open other files with vim? – Jim Sep 16 '14 at 20:38
  • @Jim yes, indeed. Though, if you open the same file, you'll get a warning about it already being edited. – muru Sep 16 '14 at 20:40
  • Interesting question - got me wondering if there is a way to edit another file in the same instance that you just stopped (without fg), and it turns out you can send files for editing to that vim instance using vim server. – Harvinder Sep 16 '14 at 20:50
3

I don't know about the "suddenly returned ..." part, but the first bit is fairly trivial. The :shell command opens your shell. For me, it opens at wherever I was when I opened vim, so it is inheriting settings from vim, as G-Man notes. That gives you the CLI mode. You can also open another vim from it. Quitting this shell returns you to wherever you where in vim originally. You can always bind a shortcut to :shell.

  • (1) :sh is already a shortcut to :shell. (2) The shell that is spawned is inheriting its working directory and other environment settings from vim itself, not from the parent shell. To demonstrate this, in vim, type :cd /some/other/directory and then :sh. – G-Man Sep 16 '14 at 20:12
  • @G-Man shortcut as in a keybinding such as Ctrl-K or F11. But thanks for the second tip. – muru Sep 16 '14 at 20:13
  • So if you inherit the environment from vim instead of the parent shell, is that problematic to use? – Jim Sep 16 '14 at 20:40
  • @Jim no, unless you set some environment variables in vim which you don't need in your shell. – muru Sep 16 '14 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.