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.
Related
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.
I invoke ghci in gvim by using :!ghci % to load my haskell file, However, the newline is displayed as ^J as the image below shows:
If I invoke ghci in vim instead of gvim, then everything is OK, so How could I get newline in gvim ?
For the graphical version of Vim, GVIM, a crude built-in terminal emulation is used for shell commands (cp. :help gui-shell). As the help page mentions, this has some limitations, and it is not meant to be used for interactive commands.
Switching to Vim in a terminal will run the shell command inside the terminal (with full capabilities); I guess that would be the best alternative if you cannot live with the discrepancies, and don't want to launch a separate terminal from gvim (i.e. :! xterm -e ghci %)
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'
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.
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!