when I use "open file under cursor" in VIM eg. #include('partials.card') there are no problems, you jump to file and everything works as expected, but the same does not work i VS Code VIM mode unless full filename is specified eg. #include('partials/card.blade.php'). Is there a workaround for this beside putting the whole filename?
Related
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).
I have a few text files, each for its own purposes. (Like: download.txt, questions.txt, word-meaning.txt etc.)
questions.txt:
I put all my question, doubt approaching in my mind to this file to ask/clear when I go online (I've no access to internet everytime of the day). I delete that line from the file when I ask that question.
download.txt:
I keep names of all packages or zipballs or tarballs in this file and download when I am connected.
word-meaning.txt:
I am not a native English speaker, so whenever I see any word which's meaning I don't in my native language, I write that down in this file and use Google Translate to translate it to my native language when I am connected.
In all above cases I have to go to last line of the file everytime I have to add anything to those lists.
My Question:
Can I make vim go to line line, last character of the file and then go in in insert mode? I will alias that to something like vimll to use it with these type of files.
Similar Question:
How do I start vim in insert mode?
You can define an autocmd to go into insert mode at the end of the file whenever one of your files is loaded into a Vim window:
autocmd BufWinEnter questions.txt,download.txt,word-meaning.txt $|startinsert!
You can do this in terminal:
vim filename.ext +$ +starti!
To go to the last line, last character of the file filename.ext and then in insert mode.
You can also alias that for your convenience of use, so add the following in your .bash_aliases file:
alias vimll='vim +$ +starti!'
In Sublime Text 2, when I have a bunch of files opening, I can go to any opened file by 3 following steps:
Hit <Ctrl>+P.
Enter the pattern to search for. The editor immediately display the file which match the pattern while I am typing (without hitting <Enter>).
After the right file is shown, I hit <Enter> to select the file.
How can I have similar functionality in Vim to search through all buffers/tabs? I know there are plenty of plugins which allow me to search all buffers, but they are just not as simple as the above 3 steps to go to a file. I think this feature is particularly useful in navigation.
You can either use the vim CtrlP Plugin to search through all files in your current working directory (:pwd, changed with :cd dir) or you can use the Lusty Explorer plugin to search through all open buffers in vim
With CtrlP, it you would type ctrl+p, then the file pattern, then enter (or ctrl-t to open in a new tab)
With Lusty, you would type Leader lb (leader is \ by default), type the name of the buffer you have open, and hit enter or ctrl-t
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.
If I have a file that contains a complete path for a file, is there a way to highlight the filename (using visual mode) and open the file (preferably in a split screen)?
Here is the behavior I would like: if the file name contains a / character, assume it is a full path (i.e. the current directory is root). Otherwise, use the current folder (i.e. default behavior). Is this possible?
Put the cursor on the filename and type gf (in command mode). Or use CTRL+W | CTRL+F to open in another window. See also :help gf (no, it's not your girlfriend).
gf command opens file under cursor.