How do you change the root of the netrw tree in vim? - vim

The title sums it up, really. I'd like to know how to set the current root of the tree when browsing files using netrw in vim.
Any ideas?

You can also use "gn" mapping to change the current directory to the folder under your cursor.
CHANGING TREE TOP netrw-ntree :Ntree netrw-gn
{{{2
One may specify a new tree top for tree listings using
:Ntree [dirname]
Without a "dirname", the current line is used (and any leading depth
information is elided). With a "dirname", the specified directory name
is used.
The "gn" map will take the word below the cursor and use that for
changing the top of the tree listing.

Per the documentation for netrw, use the ex command
:Ntree [dirname]
Assuming you have netrw installed, one can obtain more information on this using help
:help Ntree
Often times, I will just change the current work directory using the ex command
:cd [dirname]
Next, I start the explorer (ex command again)
:Exp
***Addendum***********
To update your version of netrw, I would just follow the directions that the author, Charles Campbell, posted on www.vim.org.

quite old thread, but maybe someone find this information useful that currently you can use c to change to current dir.
If you want to always change pwd while browsing around with netrw you could use:
let g:netrw_keepdir = 0

What do you mean by "the current root of the tree"?
Do you want a faster or more deterministic way to browse directories in netrw than the usual --/foo<CR><CR>/bar<CR><CR>/baz<CR><CR>?
Or do you want to be able to open netrw in a specific directory?
Or something else?

netrw v151 supports changing the treetop (root of tree):
:Ntree [directory]
and one may use the "-" key to have the treetop become the parent of the current tree. See http://www.drchip.org/astronaut/vim/index.html#NETRW for the latest netrw.

As of the doc of netrw, just type cd to make the browsing directoyr the current directory:
*netrw-c* : This map's name has been changed from "c" to cd (see |netrw-cd|).
This change was done to allow for |netrw-cb| and |netrw-cB| maps.
Note: the doc seems inconsistent, because the quick reference has a line with
c Make browsing directory the current directory |netrw-c|
... but cd works as explained above.

You can also press "C" while the cursor is on the directory you'd like to make the current home.
From NerdTree help
" Filesystem mappings~ |~
" C: change tree root to the |~
" selected dir |~
" u: move tree root up a dir |~
" U: move tree root up a dir |~
" but leave old root open |~
" r: refresh cursor dir |~
" R: refresh current root |~
" m: Show menu |~
" cd:change the CWD to the |~
" selected dir |~
" CD:change tree root to CWD

Related

Vim ctrlp only works with a git repo

The vim plugin ctrl p only works for me into git repo.
Why does it need a .git file?
Be work i mean it is searching my entire machine when no .git file is found.
my settings
let g:ctrlp_working_path_mode = 'ra'
let g:ctrlp_match_window = 'top,order:ttb,min:1,max:30,results:30'
let g:ctrlp_custom_ignore = 'node_modules\|vendor/|DS_Store\|git'
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.idea/*,*/.DS_Store,*/vendor
Also tried:
let g:ctrlp_working_path_mode = 'r'
let g:ctrlp_working_path_mode = 'c'
I'm not entirely sure what you are after, it sounds like you want ctrlp to pick up all files system wide and not just the "project" root directory.
The g:ctrlp_working_path_mode flags you set would either tell ctrlp c - only show me files from current loaded buffers directory including subdirectories, or r the first project root directory as identified with some sort of hidden repository directory like .git. Neither of which suggests what I think you are asking for...
So I'll go out of a limb and suggest that you can probably update the global variable:
let g:ctrlp_cmd = 'CtrlP /'
To look for all files, on a linux system. As detailed in the reader:
Run :CtrlP or :CtrlP [starting-directory] to invoke CtrlP in find file mode.
Or test the above with CtrlP / in command mode first.
Again, I've guessed what you wanted here...

How to configure Vim Ctrl-P to search for directory name and open default file?

My current project file structure looks like this:
When I trigger CtrlP and start typing the file name "index.js" it will find too many results. And when I prefix the letters with the foldername then there are none.
How can I configure Vim CtrlP to search for directory name instead of file name and deliver as a result the index.js inside the directory (that's the default behavior in Atom's fuzzy finder)?
Apparently it's an issue of my vim ctrl-p configuration:
ctrl-p doesn't match foldernames
Ah, as Andy and hobbs noted, ctrl-p should match against foldernames.
In my config it was overwritten with:
" Default to filename searches - so that appctrl will find application
" controller
let g:ctrlp_by_filename = 1
By replacing 1 with 0, the default behavior of ctrl-p could be restored and it matches foldernames.

vim: switch to unlisted buffer dir

In netrw version 134 I could use the following work pattern:
open dir x
select file a
:b# back to dir x
select file b
:b# back to dir x
select file c
... etc ...
But in netw version 149 this doesn't work.
The b# refuses to redisplay the directory buffer, which is unlisted.
How can I convince b# to display the unlisted directory buffer?
Thanks!
Steve
You got used to a what looks a lot like a bug (there are many in netrw) or at least an oversight.
The correct way to go back to the netrw window is:
:Rex
Try netrw v153q: http://www.drchip.org/astronaut/vim/index.html#NETRW
I was unable to duplicate this problem with the latest version of netrw.

Getting relative paths in Vim

Say I am running Vim and pwd returns
/home/rafid/myproject
And say I am currently editing the file
/home/rafid/myproject/website/editpage.php
Is there any command that returns this for me?
website/editpage.php
That is, the path of the file relative to the current folder.
Although expand('%') often works, there are rare occasions where it does not. But you can force Vim to always present the relative path by calling fnamemodify:
:echo fnamemodify(expand("%"), ":~:.")
From the manual:
:. Reduce file name to be relative to current directory, if
possible. File name is unmodified if it is not below the
current directory.
For maximum shortness, use ":~:.".
The :~ is optional. It will reduce the path relative to your home folder if possible (~/...). (Unfortunately that only works on your home; it won't turn /home/fred into ~fred if you aren't logged in as fred.)
As Adam pointed out the comments, this can be shortened to:
:echo expand("%:~:.")
Reference: :h expand<Tab> and :h fnamem<Tab>
If you are limited for space (e.g. using this in your statusline), and can manage with "fuzzy" information about where the file is located, then check out pathshorten() which compresses folder names down to one character:
:echo pathshorten('~/.vim/autoload/myfile.vim')
~/.v/a/myfile.vim
Reference: :h pathsh<Tab>
Another option would be to write a vim function. Here's my humble attempt:
function! Relpath(filename)
let cwd = getcwd()
let s = substitute(a:filename, l:cwd . "/" , "", "")
return s
endfunction
You call Relpath with any full path name, and it will strip the current directory name from its argument.
For example, try :echo Relpath(expand("%:p")) (the :p modifier asks Vim to return the full path). Obviously, this is not necessary in your case, since % by itself returns relative path. However, it might come in handy in other cases.
This works for me :
:echo expand("%")
if you use autocmd to always set the current directory of the buffer that you are working on ( cd %:p:h ) then you can just type :cd
Blockquote
This works for me :
:echo expand("%")
This is only working if you opened that file with a relative file:
for vi ./foo, expand("%") will be ./foo
but
for vi /tmp/foo expand("%") will be /tmp/foo
Yes, you can use
:args
This will give you the filename of the current file, for informational purposes.
A workaround can be :cd . which seems to re-evaluate the path relative-ness. I agree this is very annoying though.

How to create a directory using nerdtree

I have been using NERDTree for a while. Every time I need to create a new directory I need to go to terminal. Is there a quick and easy way to create a directory using NERDTree.
I read the doc but could not find anything.
When in the NERDTree window, press 'm'; you should see a menu at the bottom. Type in 'a' for add childnode. Now input the directory you want to create, making sure to add a '/' at the end, otherwise the script would create a file.
AFAIK NERDTree cannot create parent directories like 'mkdir -p' does.
Pressing m would open a menu below and you can select from a list of actions.
NERDTree Menu. Use j/k/enter and the shortcuts indicated
==========================================================
> (a)dd a childnode
(m)ove the current node
(d)elete the current node
(r)eveal in Finder the current node
(o)pen the current node with system editor
(q)uicklook the current node
(c)opy the current node
Add a childnode
==========================================================
Enter the dir/file name to be created. Dirs end with a '/'
/Library/WebServer/Documents/new-teacher-center/app/Model/
Pressing a would let you add a childnode. A childnode can be a file or folder depending if you add a forward slash ( / ) or not.
If you don’t add a forward slash like below, it would create a file.
Add a childnode
==========================================================
Enter the dir/file name to be created. Dirs end with a '/'
/Library/WebServer/Documents/new-project/app/Model/file
If you add a forward slash like below, it would create a folder.
Add a childnode
==========================================================
Enter the dir/file name to be created. Dirs end with a '/'
/Library/WebServer/Documents/new-project/app/Model/folder/

Resources