how to automatically make new file with mvim - vim

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'

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.

vi dot doesn't work

After I installed YADR, I cannot use vi . command.
If I use vi . command, my macbook's vim just says
"." is a directory
Press ENTER or type command to continue
I want to open netrw browser when I open directory by vim.
I think the problem comes from my vimrc file: https://github.com/skwp/dotfiles/blob/master/vimrc

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.

vimdiff and MacVim

I'd like to run vimdiff on MacVim. Is there an easy way I'd be able to do it?
From the terminal
If you have the mvim script installed (it is in the dmg, just put it somewhere in your PATH), you can just type in a shell:
mvim -d file1 file2
You can alias this to mvimdiff if you like.
From within macvim
In macvim, like in vim , you can also use:
:e file1
:diffsplit file2
or
:vert diffsplit file2
The second option gives you a vertical diff, which is usually more readable
If you symlink gvimdiff to mvim then mvim will start with -d etc. Vim and Mvim both look at what executable name they've been started with to determine how they behave. That how gvim and vim can be both the same executable on linux and it's the same on OSX.
Ok. I have kindof answered my own comment:
To diff two open buffers in vim, open them vertically, and select the first and type :diffthis then the second one and type :diffthis again.
This works for more than two buffers too =)
This worked for me on mac. I am using MacVim snapshot 72:
/Applications/MacVim.app/Contents/MacOS/Vim -d -g path_to_file1 path_to_file2
It works exactly like gvimdiff on windows.
Update: It even works with 3 files. This command will split the screen 3-ways and let you do a 3-way diff in macvim:
/Applications/MacVim.app/Contents/MacOS/Vim -d -g path_to_file1 path_to_file2 path_to_file3

reuse vim instance from linux terminal

By making use of the remote feature in vim, is it possible to reuse an instance of vim to load up multiple files as needed.
It will be nice to have that facility from within the same terminal instance.
I am more keen to have a tab based interface, which available in vim 7+
The scenario should be
Open terminal
vim file1.cpp
Edit - Save - Ctrl+Z to get to prompt
open another file
vim file2.cpp
Now we have file1.cpp and file2.cpp open in the same editor
Is that possible?!
I'm not sure if this can be done in exactly the manner that you're specifying, but something very similar can probably be done using a vim server running on your local machine.
Look into the :help remote.txt in Vim.
If your version of vim was compiled with +clientserver you can use vim to create a vim server, and then execute commands on it, e.g. opening another file.
The --servername switch can create a new server, and the --remote switch can send additional commands or files to it.
e.g.
vim --servername test file1.txt
vim --servername test --remote file2.txt
I've had a look, and the vim I'm using as standard on xubuntu on one of my computers doesn't have it, but there are some instructions here that may help if yours has it compiled. If it isn't, installing gvim and symlinking is apparently an option (as gvim has it included by default), or compiling the binaries from source.
Edit:
I've had more of a play with gvim and this doesn't look possible to do this within the terminal. Control-Z suspends the job at the process level. I thought it might work with screen, but no communication seems to take place unless gvim has launched in a graphical window,
This is easy to do if you compiled VIM with +clientserver, as Andy suggested in a previous answer.
I did the following:
I started up VIM as a server:
vim --servername abc
I suspended it with CTRL+Z and did:
vim --servername abc --remote ~/.cshrc
fg
Now VIM had ~/.cshrc open.
Then I did:
vim --servername abc --remote ~/.aliases
fg
Now VIM had one buffer with ~/.cshrc and another with ~/.aliases.
Then I did:
vim --servername abc --remote-tab ~/foo_bar
fg
And now I VIM had one tab with the two previous buffers open and another tab with ~/foo_bar open.
In call cases VIM was running in the terminal, not as a GUI.
I have a couple suggestions for you, though neither is exactly what you're talking about. The first is NERD Tree, which gives you a nice tree-based file browser for opening other files. Very handy. I also set up a hot key (ctrl+o) to open NERD Tree for me. I keep an alias of whatever project I'm on in ~/curr/trunk, so this always works for me:
map <C-O> :NERDTreeToggle ~/curr/trunk/<CR>
The other thing that I would suggest is to do away with ctrl+z. It's somewhat clunky, and everyone I know who uses that method tends to get lost and end up with 3 or 4 vim's running in the background. Take a look at how to open a shell for yourself. I use a map for ;s to execute a shell script:
map ;s :!~/.vim/shell.sh<CR>
Which executes:
#!/bin/sh
/usr/local/bin/bash -l;
I also have a bit of magic in my .profile for making it obvious I'm in VIM:
if [ "$VIMRUNTIME" != "" ] ; then
export PS1="\u#\h \W \t$ vim> "
fi
</2 cents>
You can split the current screen and open two (or more) files in the following way:
For a horizontal split, do:
:sp 'filename'
and for a vertical split, do:
:vsp 'filename'
To tab between the two, hit ctrl+w, then use an arrow key to navigate to whichever file you want to edit.
Also, if you want to just switch files (and only have one open), you can do this:
:e 'filename'
G'day,
Or if you want to have multiple files but use the whole vim window for one file at a time you can just enter
:e 'filename'
to open the new file. You can do this multiple times. To see what you've currently got open, enter
:ls
To bounce between the files you've got open you can use cntl-^ (shift-cnt-6) and that will alternate between the main and secondary files (shown with a % and # in the file list)
Or you can enter
:n b
where n is the number at the beginning of the file you want in the list shown by the 'ls'command.
HTH
cheers,
You can set the hidden feature on vim, to allow it to have multiple files open:
:set hidden
Then you can open as many files as you want, without bothering to save them every time you want to switch a "buffer":
:e 'filename'
You have several commands to navigate the buffers:
:bnext
:bprev
:buffers
Of course, as mentioned by samoz, you can split your screen to see multiple buffers on the same window.
I'd suggest to read this for a good introduction about vim, it will save you a lot of time.
Good luck!

Resources