How to close gvim from command prompt - linux

Usually, we open gvim from the command line like this:
gvim someFile.txt
But how are we going to close it from command line too? Instead of closing it from gvim itself?
I wanted to do some automation, hence, will need to close it from command line.

gVIM has the ability to act in client/server manner. That means you can send commands to a running vim.
Do the following
Start GVIM, open a document, do whatever
Run the following command to close GVIM
vim --servername GVIM --remote-send '<C-\><C-N>:wq<CR>'
That will save the file in GVIM and close GVIM.

First, why are you automating using a gui application?
But you can close it by sending it either <ESC>ZZ or :q!

Related

How can I open files within vim's terminal emulator

Using vim, I use :term to open the terminal emulator. After cd /path/to/project within the terminal emulator, I have a file called foo.txt. If I did vim foo.txt to open it as I would in a normal terminal, it would open vim within vim which causes a variety of issues. I see two potential solutions:
Find some way to open a file in a split from the terminal emulator
Find some way to change the cwd of vim to the cwd of the terminal emulator.
Does anyone have tips on either solution?
Inside Vim's builtin terminal, do
vim --remote foo.txt
This assume that your Vim was compile with the +clientserver option. You can check with :version or :echo has('clientserver').
vim --remote works on vim, but neovim compiles without clientserver. neovim-remote seems to be an alternative for neovim.
You have a couple of options.
One would be to get your shell to print the full path of the file that you would like to edit (for example, by using bash's realpath ./<file_to_edit_goes_here> command), then exit insert mode, place your cursor over the filepath that was printed, and use gf to open the filepath under the cursor (see :help gf).
Alternatively, if you are using Neovim, I've written a plugin called nvim-unception to open files without nesting Neovim sessions by using Neovim 0.7's built-in RPC functionality.

How to run scripts in background in Neovim?

I wish I could mimic original VIM behaviour by running scripts in the background and not in the internal Neovim terminal emulator.
Basically the reason is that I cannot seem to get colors to work properly, plus, I like to Ctrl+Z (put the editor in the background) and check what was the output of the last command.
Anyway I can configure nvim to do the same as vim in this regard?
Here is the comparison:
EDIT: My tests are run by using :! ./vendor/bin/phpunit {file}
VIM
NVIM
EDIT
By "Background" I mean, not async, in the background while Neovim is on the "top". I mean, to place the editor in the background (like when we do ctrl+z, and then run the tests "on top". Then I hit a key and Neovim comes back to the top.
In other words, I want to configure nvim in a way that when I run a test, it is the same as doing CTRL-Z; execute test.
Sorry, this may be super confusing :D
Instead of using :!{cmd}, I'd encourage you to experiment with running the tests via the :te[rminal] {cmd} command:
:te ./vendor/bin/phpunit {file}
That way the output from phpunit will be captured in a terminal buffer. You can switch between the terminal buffer and the test file using <C-^> (or :b#). Or you can open the terminal buffer and the test file side by side in separate windows. When you're finished with the terminal buffer, you can delete it using :bwipeout {num}.
One cool feature of terminal buffers is that if you place your cursor on a filepath and press gf, Vim will open the specified file. Better still, you use gF, then Vim will open the file at the specified line/column number, if those are present.
For more info, look up :help terminal in Neovim.
In newest version(HEAD version in github repo) of vim with terminal feature enabled.
You can run current file in background with following command:
:terminal ++hidden ./%
for neovim
You may need to install some plugin like:
https://github.com/tracyone/neomake-multiprocess
or
https://github.com/skywind3000/asyncrun.vim
If you are using tmux, you can
https://gist.github.com/tracyone/65cffd685fc9b9308e50c1a1783d1fb0
You could use :silent !./vendor/bin/phpunit {file} to run the script background.

How do I open a file in another window in VIM through the terminal?

Say I want to open the file "main.cpp". I have my linux terminal open in the correct directory. Normally, I just type "vi main.cpp", but this opens the file in the shell window. How do I open the file in another window?
I'm not sure I understand your question. I try to answer nevertheless:
:help client-server
Thus, you create a server instance:
vim --servername foo
Afterwards you can open files in that instance from any shell via:
vim --servername foo --remote file1 file2
Or even shorter:
vim --servername vim and vim --remote file1 (the server name 'vim' is assumed here implicitely).
EDIT: Your Vim needs to have support for the client-server architecture:
:echo has('clientserver') should result in '1'.
You can do it using two steps:
:vs (vertical split) or :split (horizontal split)
:open (path to filename)
You may want to try gvim main.cpp which will fire up vim in its own GUI which technically will do what you're asking here.
vi is a terminal text editor. It will open in the terminal window it is called from. If you wanted an X based editor, like gVIM, then you are using the wrong editor.

how to automatically make new file with mvim

When I run mvim . it opens NERDTree but doesnt open a new file/buffer.
How might I accomplish this? Ideally when you type mvim . from terminal it would open MacVim, close NERDtree, and open a new buffer
I'm not sure if this is possible but is there a way that if I run mvim . from the command line multiple times it wouldn't open vim in a new window each time?
As for your second question, vim allows you to send a file to an already running instance with --remote arguments, if vim is compiled with +clientserver. MacVim should be - if :echo has("clientserver") prints 1 in the command-line, then this should work. This will work for any vim compiled with +clientserver, including vim running within a terminal window.
When vim is using clientserver you can run mvim so that it sends the new file(s) to an already-running instance of vim, e.g.:
$ mvim --remote-silent file2.txt
Make an alias for mvim that always passes --remote-silent.
See :help remote for more details.
1.You are asking it to open your directory viewer, right? If not, why do you start vim passing the current directory (.) as argument? Leave it off and it will start with an empty buffer.
$ mvim
2.Take a look in the vim manual (man vim). You probably want the --remote-silent option.
$ mvim --remote-silent file
I personally use this so often that I've created an alias for it in my .profile:
alias v='mvim --remote-silent'

how to launch chrome/chromium from vim?

I'm looking for a shortcut in vim that would launch chrome/chromium and have it always open the same local file ?
ps: on ubuntu
Put the following in your ~/.vimrc:
map <F8> :!chrome /path/to/file<CR>
The ! command tells vim to run the following command with the shell. In this case, replace <F8> by your preferred keystroke and chrome by the command which opens chrome/chromium from the shell on your machine (probably google-chrome). Obviously, you also have to change the /path/to/file to the file you want chrome to open when it starts.

Resources