I am following the vim tutorial on vimtutor and am trying to write a file.
When I type :w I get E212: Can't open file for writing.
When I type :w !sudo tee % I get
\Vim\vim81\tutor\tutor) was unexpected at this time.
shell returned 1
How can I fix this error?
If you are running vimtutor, it usually creates the working text file in /tmp directory. My guess is that while working with vimtutor, you have accidentally set the file to an invalid path. Since it has somehow mangled the path, you should save it as a new file.
For example, you can do this by saving it as a new working text file in vimtutor:
:w /tmp/myvimtutor.txt
If you want to continue using the same text file in the future, you can then just run vim to its newly created file from the shell.
$ vim /tmp/myvimtutor.txt
Also, to verify your file path, you can type :f to show the current filename. Perhaps it will reveal why you were having problem in the first place.
Related
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.
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.
When I want to open a existing file (I use vim filename.java), it seems that I create a new file. Because it says new file. And there is no code.
I don't know why. Can the command vim filename.java open any file in the computer? Should I put my file into a particular place?
Try to search for your file using a number of ways like:
$ locate filename.java
if it doesn't work, try also find
$ find filename.java
(note: you don't have to write the the "$", it's there to mark the start of the command line)
The commands above will give you an output similar to:
/home/jane/FOLDER/FOLDER/FOLDER/filename.java
To edit, write:
$ vim /home/jane/FOLDER/FOLDER/FOLDER/filename.java
Remember to save after edit!
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
I want to create a file to tarball, without explicitly opening/extracting it but directly by using Vim. Is that possible?
$ vim file.tgz
:e someNewfile
:w! # how can I create here a file?
Vim handles tar and derived files (including .tgz) using a vimscript called tar.vim. You can see tar.vim's documentation by typing :help tar<CR> inside vim. According to that documentation:
When one edits a *.tar file, this plugin will handle displaying a
contents page. Select a file to edit by moving the cursor atop the
desired file, then hit the <return> key. After editing, one may also
write to the file.
Currently, one may not make a new file in tar archives via the plugin.
So you can edit a file which is already in the tar, but you cannot currently add new files using a vanilla setup of vim.