How to make a new directory and a file in Vim - 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

Related

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.

Opening file present in another folder which has same relative path as current opened file in vim using vertical diffsplit?

Say I have following 2 similar files in 2 different workspace on same linux machine.
/user1/ws1/ip/src/ip_main.c
/user1/ws2/ip/src/ip_main.c
Now I did,
cd user1/ws1/
vim ip/src/ip_main.c
then I press ESC then executed
:cd ../ws2
which shows my pwd as "user1/ws2"
Now I want to open ip/src/ip_main.c in pwd i.e "user1/ws2"
using ESC
:vertical diffsplit "some logic to get to ws2/ip/src/ip_main.c"
note after :cd ../ws2
:pwd command show "user1/ws2" but :echo $PWD command still show "user1/ws1"
How can i do it,Can anyone help ?
Diffing both files is easily done via shell globbing, e.g. in Bash:
$ vimdiff -O /user1/ws{1,2}/ip/src/ip_main.c
Withing Vim, you have to use relative paths (i.e. prepend ../.., then go down into the other hierarchy). <C-R>% on the command-line (cp. :help c_CTRL-R) inserts the current path; this may avoid retyping much of the similar path, especially when used with the command-line window (:help c_CTRL-F).
With my EditSimilar plugin, you can use this:
:DiffSplitSubstitute 1=2

How can I change to a different directory on an FTP server in 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.

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.

Change vim's current directory upon starting from command line

Vim (actually, gvim is in question, but I believe it works both ways) upon starting has its current directory in its runtime directory.
I would like to upon starting, to have its cd in let's say, c:\pro
I know I can just do :cd c:\pro, but is there a way to change it upon starting it, with
something alike gvim c:\pro (only that doesn't work :-| ?
You can use
gvim -c "cd C:/pro"
-c allows you to run commands just after Vim has finished loading.
Vim's working directory is initially set to the working directory of the process that invokes Vim.
If you're double-clicking on the .exe in Explorer, that's why it's the runtime directory.
If you're using the Start Menu shortcut, then it has the Start in path set to the runtime directory. You could change the Start in path of the shortcut.
If you use the run dialog, then vim's initial working directory is likely your Documents and Settings directcory.
Can't you put cd path in your .vimrc ?

Resources