I use Gvim to write code. And use vifm to make file management ( copy, move files, extract tar's ). Sometimes, when navigating in vifm i need to open some file to edit him. Using :e it opened vim in same window. Is there any way to open file to edit in already opened gvim program?
You can use Vim's client-server feature to send the :edit to the existing GVIM instance. In the terminal Vim / vifm, execute:
:!gvim --remote path/to/file
See :help remote.txt for details. You can also open the file in a new tab page with --remote-tab etc.
Partial solution/workaround (I'm using a mac fwiw):
In vfimrc, define
" yank current file path into the clipboard
nnoremap Cf :!echo -n %c:p | pbcopy %i<cr>
To copy filename and dir into system clipboard
Then in vifm, cursor over file and type
Cf
:!gvim "
<cmd-v>
to paste clipboard,
and finish expression with...
"
<enter>
and now that file should open in gvim. It worked for me
Related
When using the delightful netrw support in vim to open a file, opening directly works well, as in
:e scp://hostname//path/to/file
when I have a buffer open, I often need to load another file in the same directory that file is in. However, you can't change the working directory via
:cd scp://hostname//path/
And even with autochdir on, simply
:e another_file
doesn't work.
Is there a way of getting this or equivalent behavior? Key part is I want to be able to use a relative path to edit files.
I also failed to find a way (e.g. environment variable) to suggest VIM to look up relative scp:// path with.
Instead of editing a file with:
:e scp://hostname//path/to/file
you can list the parent directory with:
:e scp://hostname//path/to/
and use netrw to do the file navigation.
The :Rex command, allows you to open the last netrw listing so your workflow could be:
:e scp://hostname//path/to/ " list remote directory
/foo<CR> " search for a file
<CR> " edit that file
:Rex " re-list remote directory
/bar<CR> " search for another file
<CR> " edit that file
:Rex " re-list remote directory
" rince
" repeat
Is there a way of disabling NERDTree when opening a file? When I open a file in mvim (using --remote-silent) and a NERDtree window is currently active, the file will open in that (narrow) window. How can I configure Vim to select another window if available and to open one if not?
There's no way to intercept the --remote-silent; you have to send explict commands to open the file with --remote-send instead. There, you can encode logic to deal with the NERDTree window, e.g. to move to the previous one if it is active:
$ gvim --remote-send "<C-\><C-n>:if &ft == 'nerdtree'|wincmd p|endif|edit filename<CR>"
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.
I find myself in the position where I want to create a new file in the same directory as the one that the open file is in. How do I create a new file in the directory of the open file in vim? Also, is there a a place where I can learn these things on my own? Googling didn't help.
From within Vim, new files are created like existing files are edited, via commands like :edit filename or :split filename. To persist them to disk, you need to (optionally type in contents and) persist them via :write.
Like a command prompt, Vim has a notion of current directory (:pwd lists it). All file paths are relative to it. You don't need to duplicate the path to your current file, there are some nice shortcuts for them: % refers to the current file, :h is a modifier for its directory, minus the file name (cp. :help filename-modifiers). So,
:e %:h/filename
:w
will create a new file named filename in the same directory as the currently open file, and write it.
Alternatively, some people like Vim to always change to the current file's directory. This can be configured by placing
:set autochdir
into your ~/.vimrc file (which is read on Vim startup). Then, above becomes simply
:e filename
:w
Finally, Vim has a great built-in :help. Learn to navigate and search it!
you should have a try with "nerdtree" plugin.
In the nerdtree window, you typed key m, and file operation choices will display to you
If you want to create a new file and also show it in the window next to your current file, you can try this:
:vsp newfile
The vsp stands for vertical split, and it splits the screen in half, one showing your current file, the other showing your new file (also works with just sp, which is a horizontal split).
Per #MartinLyne's comment above, this will create the file in the directory of the file in which you opened vim. To adjust for this, you can change the current working directory as follows:
:cd %:p:h
This command changes the current working directory to the directory of the active file, meaning that running the vsp command (or any of the commands above) will create the file in that directory.
I usually use:
:tabnew my-file
Then add some content and:
:w
It will create new tab with new file.
(I use Vim 8)
When you have opened vim in non existent location like
$ vim /etc/<some_folder/<next_folder>/file.cfg
then to create a new directory while being inside vim, just run in normal mode
:! mkdir -p /etc/<some_folder/<next_folder>
next save your doc as usual :w :x ZZ (whatever you like)
that's it
I'm quite late to the party, but another option is to open NERDtree with :E or :Explore (or its splitting alternatives :Vexplore/:Sexplore == :Vex/:Sex).
In NerdTree you can create a new file with %, and type the name. It will automatically open the file, and create it after you :w/save it.
This is for Gvim!
Enter this to see the current directory.
:cd
then change it with
:cd desktop/somefolder
then save or make new file there
:enew asd.cpp
now again see the file
:cd
With NERDtree
ma <FILENAME>
ma <DIRECTORY NAME> + /
Assuming I work under linux, how do I open a directory of files (contains .c ,.h ,.cpp) using emacs, vim, vi or gedit?
I mean like open all the directory from the shell and have the list of the files in the right/left side?
if you want to open it in vim-
vim *.c *.h *.cpp
and then use :next and :prev to navigate between all open files
This will open Emacs in dired mode in the current directory.
emacs .
This will open one buffer for each file in the current directory. Presumably you can do the same with e*vi*l.
emacs *
You probably mean something like NERDTree for vi (there are also other plugins for emacs etc.).
Check out this page for screenshots: NERDTree for VI