I am using linux.
In terminal I can type
vim sample
A vim window for the file 'sample' opens
Here any change can be saved with :w
But I want to open a new vim file having no name and save it with the name sampleName using
:w sampleName
But I am unable to do so.
Typing only vim in terminal gives me a window with about and copyright information
I am not using gvim but vim
You should be able to just run vim with no arguments. It will open vim by itself. To save it to a file, execute the command: :w <new file name>
Issue the Command, :enew and Also Learn about How Vim Manages File Editing
I would recommend reading up on file and buffer usage in Vim in the help file, usr_22.txt.
As architrex has indicated, by default, if one does not have a file(s) listed in the argument list when starting Vim, a new buffer is created. See :help starting.
If you started vim with one file in the file argument list like you described, vim sample, a common way to work with an empty buffer is to issue the :enew command (typically after you would have written changes to the file named, sample, :w).
We can see the new buffer here:
Once one is done modifying the new buffer, you can issue the write command, :w sampleName, with the expected result of writing the file.
Vim's use of buffers is intuitive and you will become more skillful as you use it.
When I started using Vim, I leveraged using NETRW which is a seeded file navigation plugin with Vim for file creation (and placing those new files in buffers).
What follows is one way to leverage NETRW to do this.
If you have already started Vim, I would type :Exp or :e . which are ex commands which will start the file explorer which is a feature of the seeded file navigation plugin, NETRW.
One could start Vim:
Start the file explorer for NETRW:
I would then use the file explorer to navigate to the desired file to create a new file you wish to edit (k is up, j is down, enter key means select).
Next, I would type,%, once I have navigated to the desired location.
You will be notified to "Enter filename". Just enter "sampleName" and press enter.
Go into edit mode (e.g. i) and start typing.
NETRW is a robust file navigation tool. Creating files in the file locations desired is an essential skill to have to utilize Vim well.
You could also read the help files concerning NETRW (e.g. the ex command, :help netrw).
Is there a way to make NERDtree load only directories that I specify? I don't see how to do this.
One idea could be to use bookmarks for that purpose. For doing that, use the following sequence of commands:
Start VIM and NERDtree inside it.
Navigate to the directory you want to visit often, lets name that directory example my_proj.
Enter the command: Bookmark my_proj.
You may now open the list of Bookmarks by pressing B.
Navigate to the bookmark you want to open.
Press RETURN there.
The bookmark seems to be persistent, so you are now able to open a new VIM editor, and revisit the directories you have bookmarked. And you could even start the NERDtree with an additional argument, which could be a bookmark or directory (thank's david for the hint). So by entering NERDtree my_proj, everything is started fine.
Another idea could be to insert in your .vimrc file the following line:
:cd c:/path/To/my_proj
so when you start then NERDtree, your project directory should be in focus.
Sorry to ask such a novice question but I am looking for a way to handle multiple files. I dont want to type huge file paths to open every file using :tabnew and :e commands
Fuzzy Finder is a handy plugin to quickly find and open files.
Basically you have to only type a few letters like test and you'll get a pop-up menu to open in your current path :
footest.c
bartest.h
footest.h
...
It is a bit slow when used on NFS but it is useful if you don't want to type long path and file names.
Alternatively if you don't want to use any plugin, by default gvim/vim includes a file browser called netrw.
To start it, just type :e . you'll get the content of your current directory, you can then navigate through the directory structure quite easily. (There is even commands to delete, rename, etc like a standard file explorer)
:help netrwfor more information.
A couple of tips that you might be interested in:
You can configure Vim so that the
current directory "follows" the
directory of the file you are
currently editing. That way you can
edit another file from the same
directory without having to type the
full path. This can be achieved by
putting either set autochdir or
autocmd BufEnter * lcd %:p:h in
your .vimrc
You can use wildcards with tab
completion. e.g. to edit
a_file_with_a_long_name.txt you could
do :e a*long and then press
Tab followed by
Return.
Usually, vim supports buffers for that. Use :badd to add buffer, :bdelete to remove it and :ls (or :buffers) to list all opened buffers. I believe, GVim supports these features too.
For example, if you wanna edit all .rb files in your app/controllers/pages dir (in the case of Rails project), you type vim app/controllers/pages/*.rb in your terminal and then edit the first file (buffer) in the vim window. When you've done with all changes, save changes as usual with :w (note: do not use q! option - this will close all your buffers you've opened) and then use :bn<tab> (or fully, :bnext) or :bprevious to switch to the next file (buffer). When you run :bnext on the last buffer, you'll be dropped to the first one.
You can open a directory in Vim, search for the file o directory you are looking for with '/' and type [enter] to open it.
I installed Nerd_tree plugin for vim on ubuntu 10.04. It is great plugin. But I met the following problems:
after open nerd_tree to browse the files in /etc/apache2/sites-available/, now I close it, and go on with my work. Later I want open nerd_tree again to edit another file in /etc/apache, but nerd_tree shows me always the user home directory /home/toledot, So I have to up dir->up-dir->up-dir again. Is there a way let nerd_tree to remember the path, which I have used at the last minute?
I opened nerd_tree and select one file to edit. After my update on this file, and want to select another file to make some change. But I can not go back to the nerd_tree. I tried to make double click and tab and so on. It doesn't work. What is the hotkey to switch nerd_tree and the opened files in vim?
I am absolute new with vim and plugins, so please help me and explain the solution in details. I am very thankful for any suggestion or infos.
A couple of points that may be helpful.
If you set autochdir the current working directory will be automatically be set to that of the current file. You can do this either interactively or in your .vimrc file.
The official way to open the NERDTree window seems to be with the command :NERDTree. You can map this to any convenient key sequence.
You should be able to view the helpfile with the command :help NERDTree. If you have trouble getting the help, change to the directory containing file NERD_tree.txt (in vim) and execute :helptags .
You can also add bookmarks for file/directories that you often use. See
:help NERDTreeBookmarkCommands.
What you need is a handy map to :NERDTreeToggle. Put this in your .vimrc
nmap <silent> <F4> :NERDTreeToggle<CR>
And just hit <F4> to open/close NERDTree, just like that, just as you last toggle-closed it.
You can also open a specific directory using :NERDTree /my/dir
I advise you to set autochdir so that whenever you open a file, working directory (you can find out this with :pwd) will be changed accordingly
I want to open a file in Vim like in Eclipse using Ctrl + Shift + R, or via the Ctrl + N option of autofill. Invoke a keyboard shortcut, type the file name/pattern, and choose from all the matching files names.
I know opening it normally like:
:tabe <filepath>
:new <filepath>
:edit <filepath>
The problem here is that I have to specify the whole file path in these cases.
What I normally do is e . (e-space-dot) which gives me a browsable current directory - then I can / - search for name fragments, just like finding a word in a text file. I find that generally good enough, simple and quick.
I recently fell in love with fuzzyfinder.vim
... :-)
:FuzzyFinderFile will let you open files by typing partial names or patterns.
:find is another option.
I open vim from the root of my project and have the path set to there.
Then, I can open files located anywhere in the tree using:
:find **/filena< tab >
Tab will autocomplete through various matches. (** tells it to search recursively through the path).
You can search for a file in the current path by using **:
:tabe **/header.h
Hit tab to see various completions if there is more than one match.
Consider using CtrlP plug-in.
It is included in Janus Distributive.
Allows you to find files in the current directory, open buffers or most recently used files using "fuzzy matching" or regular expression.
unless I'm missing something, :e filename is the fastest way I've found.
You can use tab to autocomplete the filename as well.
I like the :FuzzyFinderTextMate (or Ctrl + F) on my setup.
See http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim
I use a couple of shortcuts in my .vimrc file (exact syntax below).
They are based on the fact that in 90% of the cases, I want to open another file in the same directory as the file that I am currently editing, or in a directory that is very close in the hierarchy to that edited file.
Here's what the commands do do:
,cd : Change the current working directory to the directory that the current file you are editing is in.
,e : Opens a file with the current working directory already filled in so you have to specify only the filename.
Put these into your .vimrc:
map ,e :e <C-R>=expand("%:p:h") . "/" <CR>
map ,cd :cd %:p:h <CR>
Here's a sequence of events:
You are editing a file called test.java in "/home/prog"
,cd -> Current working directory now
becomes "/home/prog"
,e -> Expands to ":e /home/prog" so
that you can just fill in the file
name, say test.h.
,e -> Expands to ":e /home"
tab -> Cycle through subdirectories of /home
enter -> cd to the directory you
want say /home/prog
,e -> Expands to ":e /home/prog"
There's also command-t which I find to be the best of the bunch (and I've tried them all). It's a minor hassle to install it but, once it's installed, it's a dream to use.
https://wincent.com/products/command-t/
Use tabs, they work when inputting file paths in vim escape mode!
If you've got tags (and you should), you can open a file from the command line just by the name of the class or method or c function, with "vim -t DBPlaylist", and within vim with ":tag ShowList".
If you're editing files in a common directory, you can :cd to that directory, then use :e on just the filename.
For example, rather than:
:e /big/long/path/that/takes/a/while/to/type/or/tab/complete/thingy.rb
:sp /big/long/path/that/takes/a/while/to/type/or/tab/complete/other_thingy.c
:vs /big/long/path/that/takes/a/while/to/type/or/tab/complete/one_more_thingy.java
You can do:
:cd /big/long/path/that/takes/a/while/to/type/or/tab/complete/
:e thingy.rb
:sp other_thingy.c
:vs one_more_thingy.java
Or, if you already have a file in the desired directory open, you can use the % shorthand for the current filename, and trim it to the current directory with the :h modifier (:help :_%:) :
:e /big/long/path/that/takes/a/while/to/type/or/tab/complete/thingy.rb
:cd %:h
:sp other_thingy.c
:vs one_more_thingy.java
And, like others have said, you can tab-complete file names on the ex-line (see :help cmdline-completion for more).
This isn't exactly what you're looking for, but it's good in many cases (though not all).
If you VIM open and there's a name of a file in the buffer, you can put the cursor on that filename and type gf. This opens the file whose name is under the cursor in the same buffer. It's the same as
:e CTRL+r CTRL+w
I know three plugins that permit to open files, support auto-completion, and don't require to enter the full path name of the file(s) to open (as long as the files are under one of the directories from &path vim option):
searchInRuntime that I'm maintaining (the completion is not on :e/:find, but on split actions)
fuzzy finder as it has been already pointed out,
lookupfile.
Lately, I've seen another plugin with a similar feature, but I don't remember the name.
Soon, :find is likely support auto-completion -- patches on this topic are circulating on vim_dev mailing-list these days.
you can use (set wildmenu)
you can use tab to autocomplete filenames
you can also use matching, for example :e p*.dat or something like that (like in old' dos)
you could also :browse confirm e (for a graphical window)
but you should also probably specify what vim version you're using, and how that thing in emacs works. Maybe we could find you an exact vim alternative.
FuzzyFinder has been mentioned, however I love the textmate like behaviour of the FuzzyFinderTextmate plugin which extends the behaviour to include all subdirs.
Make sure you are using version 2.16 of fuzzyfinder.vim - The higher versions break the plugin.
With Exuberant ctags, you can create tag files with file information:
ctags --extra=+f -R *
Then, open file from VIM with
:tag filename
You can also use <tab> to autocomplete file name.
In GVIM, The file can be browsed using open / read / write dialog;
:browse {command}
{command} - open / read / write
open - Opens the file
read - Appends the file
write - SaveAs dialog
I installed FuzzyFinder. However, the limitation is that it only finds files in the current dir. One workaround to that is to add FuzzyFinderTextmate. However, based on the docs and commentary, that doesn't work reliably. You need the right version of FuzzyFinder and you need your copy of Vim to be compiled with Ruby support.
A different workaround I'm trying out now is to open all the files I'm likely to need at the beginning of the editing session. E.g., open all the files in key directories...
:args app/**
:args config/**
:args test/**
etc...
(This means I would have possibly scores of files open, however so far it still seems to work OK.)
After that, I can use FuzzyFinder in buffer mode and it will act somewhat like TextMate's command-o shortcut...
:FuzzyFinderBuffer