How can I change to a different directory on an FTP server in Vim? - vim

Vim has a fancy NetRW plugin that lets me type
vim ftp://ftp.example.com/
And get a nice directory listing, which I can edit files from by simply pressing CR while on top of them.
However, I can't seem to do this with directories. In the help file that appears when I press F1, it says I should be able to:
BROWSING netrw-cr {{{2
Browsing is simple: move the cursor onto a file or directory of interest.
Hitting the <cr> (the return key) will select the file or directory.
Directories will themselves be listed, and files will be opened using the
protocol given in the original read request.
However, when I use j/k to navigate to a directory and press <cr> to try to open it, I just get:
somedirectory: Not a regular file
Vim tries to access ftp://ftp.example.com/somedirectory. Notice that it doesn't add the slash signifying that it's a directory.
I'm at a loss for why this occurs. How can I simply enter that directory within Vim, without having to exit Vim and retype vim ftp://ftp.example.com/somedirectory/?
I'm running Vim 7.4 on Ubuntu 14.04.

Vim didn't seem to know that my directory really was a directory, as it didn't show a / sign after it in the directory listing.
Luckily, I found a way to force NetRW to act like a file is a directory: gd.
FORCING TREATMENT AS A FILE OR DIRECTORY netrw-gd netrw-gf {{{2
Remote symbolic links (ie. those listed via ssh or ftp) are problematic
in that it is difficult to tell whether they link to a file or to a
directory.
To force treatment as a file: use
gf
To force treatment as a directory: use
gd
So, simply press gd instead of <cr> to change to the directory under the cursor.
I'm still not sure why NetRW thought my directory was a file. The help message only said "remote symbolic links," which they're not (I think). It may have something to do with my web host (iPage), which might be using symbolic links everywhere instead of actual directories. Or maybe something just borked on my end.

Related

How do I go to a file in vim/nerdtree?

I'm trying to transition to vim, but I'm having a hard time mapping over some functionalities in pycharm over to vim.
The first being how do I directly go to a filepath. In pycharm, I believe it is cmd-shift P. You'll type the file-path and it'll take you there. I think there's auto-complete too?
Like -- I know that there's a .css file I want to access. So I'd instinctively start typing: cmd shift p .css and this would return the .css files.
How do I do that in vim?
Thanks!
:edit is the most basic command for editing an existing file.
:edit <your file name>
To get a list of all the files ending in ".css" use :edit e *.css and then press Ctrl+d. See :help c_CTRL-D in Vim for more information.
:find <file> is a more powerful version of :edit. It searches for <file> from the directories listed in your path option. For example, if your current directory is project and the value of the path contains
/path/to/project/**, then :find file.css will search all the subdirectories of project for the "file.css".
There is also a plugin called "ctrlp.vim" that should be similar to what you used in pycharm.
For more information about file navigation, I highly recommend reading "Death by a thousand files", an excellent article by Romain Lafourcade.

How to get to long directory quickly when writting code in VIM

I am writing Bash script using VIM. I need to cd to a directory and run the command tool. The command tool is deep inside the directory. How do I quickly cd to that directory instead of manually typing the directory out in VIM ? In terminal prompt, I can get to the directory quickly using tab. It does not work in VIM.
Thanks
ffl3883
You can change to the currently edited file's directory with :cd %:h; see :help filename-modifiers. Likewise, if you trigger the tool from Vim :! % can do this quickly (and repeat with :!!). Or just :set autochdir, so that the current directory within Vim always follows the currently edited file (and you can then just reference the file via ./).
When typing file paths in vim (as I often do for shell scripts), I find filename-complete invaluable. Simply type <C-X><C-F> in insert mode.
N.B. It does not work in all cases (generally vim prefers the path to be a separate WORD), but a quick edit-complete-fixup isn’t terrible.

How to open files from within Vim

I am trying to learn how to use Vim. Apparently I have failed at the first hurdle since Vim (certainly on my computers) cannot open files from within itself. I know this must somehow be a mistake on my part since how can Vim still be around with such a flaw??
Anyway I have searched for the last day or so with no solution.
I have tried:
:e .
And Vim helpfully tells me that: "." is a directory. I was under the impression that this command would open a file browser in current directory, but it doesn't.
Similarly I have attempted other commands:
:Ex
:Explore
:Sexplore
:Sex
:Vexplore
:Vex
:Hexplore
:Hex
I have tested these from How do you open a file from within Vim? but nothing suggested there works.
All of these produce: E492: Not an editor command: <insert any of the above commands here>.
I am left with the conclusion Vim can't open files unless Vim is called from the terminal and the file is passed as an argument or the files happen to be in the current directory (where ever that may be) and you know the file's name.
Can someone help? I would like to be able to open files in other directories and list them but for the life of me nothing is working despite every guide I have read saying it would.
Thanks.
At the request from Zaffy this question has been solved.
At Robby Cornelissen's prompting I checked the MX's Linux's package manager and found that vim-common was installed but weirdly not vim. Once I'd installed vim :e . worked and I can now navigate the filesystem.
I have no idea the difference between vim-common and vim or the reason for the separate packages; Robby Cornelissen suggests that vim-common is probably a minimal or tiny version of vim.

How to make a new directory and a file in Vim

When using Vim as a text editor, is there a way to create new directories and files, while in text editor mode? Instead of going back to the command line and creating a new directory and file.
If you are in the file explorer mode, you can use:
d for creating a directory
% for creating a new file
You can get into the explorer mode with issuing a command :Sexplore or :Vexplore
There is no need to call external commands with !
Assuming you're running a shell, I would shell out for either of these commands. Enter command mode with Esc, and then:
:! touch new-file.txt
:! mkdir new-directory
A great plugin for these actions is vim-eunuch, which gives you syntactic sugar for shell commands. Here's the latter example, using vim-eunuch:
:Mdkir new-directory
Switch to file browsing mode
:Ex or if that is not working use :Explore
then press
d
and add the new directory name.
Assuming you just ran vim on new file in the directory that does not exist:
vim new_dir/new_file.txt
When you try :w you will get 'E212: Can't open file for writing'
To create new directory and file use this:
:!mkdir -p %:h
For the sake of completeness:
Shell out and use normal commands, such as :!mkdir my_dir and :!touch foo.txt (as mentioned in Jake's answer here) will create the directory and file in CURRENT working directory, which is the directory when you started your current vim process in the beginning, but NOT NECESSARILY the same directory of the file that you are currently editing, or the same directory that your :Explore explorer is currently viewing. When in doubt, always use :!pwd to check your current working directory first, and use relative path when necessary.
So if your project contains multiple sub-directories, a more convenient way is to:
type :Explore to enter the explorer mode first,
and then you can easily navigate to whatever sub-directory you like, by typing up-arrow or down-arrow (or j or k) to move cursor, typing Enter to enter a sub-directory, typing - to go up a level of directory. (Note that, all these navigation does NOT change your current working directory either);
Now you can type d to be prompted for a directory name, or type % to be prompted for a file name, and then they will be created in the directory currently shown on screen.
PS: These keys are actually mentioned in the built-in help F1.
Alternatively you can use :e . to get to explorer mode and then hit d .to create the new directory .Thought a shorter answer might be better

gvim: change the default working directory

when I open gvim using Alt+F2 it takes as its default working directory my home folder.
How can I change the working folder after or while running gvim? can i pass that folder as a parameter when open gvim?
You could use a shortcut.
The simplest way, though, would be to
:edit $MYVIMRC
append a line
cd /home/user/my/work/dir
save (optionally execute :w|source % to immediately reload)
Inside vim
use
:pwd
:cd some/other/dir
To view/change current working directory.
Use e.g.
:cd %:h
to change to the directory containing the file loaded in the active window.
If you need/want to do this often, consider just setting 'autochdir'
:se autochdir
From the docs:
When on, Vim will change the current working directory
whenever you open a file, switch buffers, delete a
buffer or open/close a window. It will change to the
directory containing the file which was opened or
selected. This option is provided for backward
compatibility with the Vim released with Sun ONE
Studio 4 Enterprise Edition.
Note: When this option is on some plugins may not work.
You can pass an a folder to gvim (when you have NERDTree then it will be a file tree) You can cd before start to begin in directory you want or use :cd <path> command to change current working directory, which can be passed to -c flag when running Vim:
$ [g]vim -c 'cd <path>'
You can also check current dir using :pwd command.
You can change the working directory with the :cd command. You can also pass this in a command-line option like this:
vim -c "cd wherever"
If you like the working directory to always be the file you're currently editing you can use the set autochdir option. Put that in your ~/.vimrc or see :help autochdir.
I know I'm late, but I started using CDargs which is a bash tool to mark certain directories as bookmarks, then use cdb and press tab to list all the bookmarked directories.
There is a vim plugin that interacts with the settingsfile of this tool: vim-cdargs.
This combo works really nice for me to switch between projects.
Or after opening gvim to go quickly to some bookmarked folder, then use Ctrl-p plugin to quickly find the file I want to edit.
extra hint: I don't even want to type :Cdb so I abbreviated c to expand to :Cdb by adding this to my vimrc:
cnoreabbrev c Cdb
after which typing :c followed by a space, will expand into :Cdb.
EDIT: I now use vim-startify which provides a start page for vim that shows the most recent used files. And with the option let g:startify_change_to_vcs_root = 1 it will change the working directory to the outermost vcs root folder of the file you opened. Which is almost always what I want.
Furthemore, I created my own 'plugin' with some key mappings that will switch to the closest or furthest directory, in the path of the current buffer, containing a .git directory or file. In order to easily switch between searching for files in the current git submodule or in the overal supermodule.
Also I switched to fzf with fzf-vim instead of Ctrl-p, which works significantly faster and is more highly configurable.

Resources