Is there a way to open all the files in a directory from within Vim? So a :command that would say in effect "Open all the files under /some/path into buffers".
Ideally, it would be great to open all the files under a dir recursively.
The command you are looking for is args:
For example:
:args /path_to_dir/*
will open all files in the directory
Why it doesn't work if I want to open all files ending with a certain extension?
I tried
:n ./**.cs
and opens only the files in the currenty directory.
I found the answer.The correct code is :n **/*.cs
For more information :h find
Did you try
:n /some/path/*
It will open all files in /some/path
I don't think it'll open file recursively though.
EDIT
Maybe using ** will open recursively as daf mentionned
A method that doesn't require messing with args is to put the list of files in a text file, and then use the :so command to run the commands in that file.
For example, if you want to open all the files that end in .php in a given directory, first create files.txt containing the list of files, prepended with whatever command you want to use to open them.
sp alpha.php
sp bravo.php
sp charlie.php
Then, within vim:
:so files.txt
If the list of files is large, it's relatively trivial to generate the files.txt file quickly, by redirecting the output of ls to a file, and then using a vim macro to prepend sp before each filename.
This obviously isn't as elegant as using the args and argdo commands, but those commands are also a lot more complicated.
There also might be a way to do this with a single command on the command line, but even after 16 years I still find vim programming to be strange and arcane.
Another way to open files recursively
find . -type f -exec vi {} \;
If you'd like to add to the argument list;
:arga what_you-d_like_to_add
see
:he arga
from/in vim for more information.
Related
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.
Guys how do i open multiple files in vim with a single command?
These files i want to open has some kind of pattern, example:
myfile1dsa
myfile2dsdas
myfile3xzczxcz
and also do these opened files create their own tab in my vim window?
and out of topic question:
what does "--" means in a linux command? how does it differ from just "-"?
example:
grep --color 'data' fileName
You can open them from within vim using
:args myfile*
or if you want to open all files matching the pattern in subfolders
:args **/myfile*
This all assumes your current directory is the folder from wich you want to open files from. Prepend a directory to myfile if it's not.
:args /yourfolder/myfile*
:args /yourfolder/**/myfile*
Edit (cudo's to romainl)
To open all the files found in tabs, you can use
:argdo tabe
wich essentially goes like this:
argdo: for each file in the argument list
tabe : open a new tabpage and edit the file
I have a project as follows:
/dir
dir1
dir2 -> symbolic-link to /otherdir
file1
tags *
I want vim to use THIS tags file which includes tags for files in dir1 and dir2.
When I edit file1, VIM cannot find the correct tags file.
I have the following setup in .vimrc:
set tags=tags;/
Is there a way to keep this file structure without explicitly telling VIM the absolute path to tags?
You can append to the same ctags other tags, so for example if you want to ctag everything inside dir1 you would execute:
ctags -R *
and if you want to add some other tags from dir two:
ctags -R -a ~/path/to/dir2/*
-a is for appending.
Now what I do to always have my ctags no matter where I open my vim, is to add this line in my .vimrc:
set tags+=./tags;$HOME
this will look for tags in the current directory and will go down recursively to your home folder, if you would like it to search until the root folder or less just change $HOME for / or /path/to/root/project/
With this line in my ~/.vimrc and a similar layout as yours, tags related features (:ts, <C-]>, etc.) use the same tags file situated at the root of dir, alongside dir1 and dir2.
set tags=./tags,tags;$HOME
The tags file is first searched in the current file's directory, then in the cwd, then upwards until it reaches $HOME.
What does :echo tagfiles() say when you are editing file1? Here it says ['/home/romainl/Desktop/dir0/tags'].
EDIT
Throwing a symlink doesn't seem to change anything.
ENDEDIT
I think it's just a question of being in the right directory. When you start working in this project, use :cd /dir to get into the directory with the tags file, and make sure the autochdir option is turned off. Then when you edit a file inside dir2, the working directory will still be dir, and it will still find the same tags file.
If, on the other end, you end up with dir/dir2 as your working directory, that will actually mean you're in /otherdir, so when Vim looks for the tags file from there, it can't find it in that directory or in / . I suspect that's what's happening to you now.
You can see what directory you're in at any time with the :pwd command, just like in the shell.
I’m currently using MacVim and I’d like to print out all the files in my working tree. Is there a way to simply do this, perhaps using the hardcopy command?
A convenient way to execute a command for a group of files is to
(1) collect the list of their names, define it as the new argument list
(see :help arglist), and then (2) iterate the command over that list.
1. To perform the first step, use the :args command with
a wildcard matching the desired files. For example,
:args ./**/*
sets the argument list to the names of all files in the current
directory and its subdirectories; similarly,
:args /tmp/**/*.{c,h}
selects all .c and .h files in /tmp and its subdirectories.
For details about wildcard syntax, see :help wildcard.
If the path to the root of a subtree containing files to print is
unknown beforehand and is built by a script, use the command
:exe 'args' join(map(split(glob(p . '/**/*'), '\n'), 'fnameescape(v:val)'))
where the variable p is supposed to contain the path to that
root directory.
2. For sending files in the argument list to the printer, execute
the :hardcopy command for those files using the :argdo command:
:argdo hardcopy!
The ! specifier suppresses the modal dialog for selecting printing
parameters.
A more complicated command can be used to print each file to
a separate PostScript document located at the same directory
as that file:
:argdo hardcopy! >%:p.ps
Here the name of a printed file is concatenated with the .ps suffix
to get the name of a corresponding PostScript file (see
:help cmdline-special).
For speeding up the :argdo argument command, Vim ignores the
Syntax autocommand event by adding it to the eventignore list.
This implies that if Syntax autocommands had not been run for
a file in the argument list before the :hardcopy command is
:argdone, the corresponding printed document would not be
syntax highlighted (despite syntax:y being set in printoptions).
To execute Syntax autocommands for all files in the argument
list, use the following command first:
:argdo set ei-=Syntax | do Syntax
To do this in the same run as printing, concatenate the two
commands:
:argdo set ei-=Syntax | do Syntax | hardcopy! >%:p.ps
Edit Sorry, I misunderstood before.
To print all, say php and C# files in your working directory:
:args ./*.{cs,php} **/*.{cs,php}
:argdo ha
I have a folder containing hundreds of TTL (TeraTermLanguage) files.
Now I wanted indent all these files.
I have created teraterm.vim for indentation and I open a file using VIM and do "gg=G" and whole file gets indented properly.
But is there any way, where I can indent all the files in folder.
I wanted to do with help of Shell. But in VIM I couldnt pass file indent command as the argument to VIM.
Please suggest which is the best way I can do indentation to all the files in VIM.
Much simpler than scripting vim from the bash command line is to use vimscript from inside of vim (or perhaps a much simpler one-liner for scripting vim from the command line). I personally prefer using the arg list for all multi-file manipulation. For example:
:args ~/src/myproject/**/*.ttl | argdo execute "normal gg=G" | update
args sets the arglist, using wildcards (** will match the current directory as well as subdirectories)
| lets us run multiple commands on one line
argdo runs the following commands on each arg (it will swallow up the second |)
execute prevents normal from swallowing up the next pipe.
normal runs the following normal mode commands (what you were working with in the first place)
update is like :w, but only saves when the buffer is modified.
This :args ... | argdo ... | update pattern is very useful for any sort of project wide file manipulation (e.g. search and replace via %s/foo/bar/ge or setting uniform fileformat or fileencoding).
(other people prefer a similar pattern using the buffer list and :bufdo, but with the arg list I don't need to worry about closing current buffers or opening up new vim session.)
Open up a terminal. Type:
$ vim -w indentme.scr foo.c
Then, type this exactly (in command mode):
gg=G:wq
This will close vim, saving the process of indenting all lines in the file to a Vim script called indentme.scr.
Note: indentme.scr will contain a record of all key commands typed, so when you are done indenting the file, don't spend a lot of time using the arrow keys to look around the file, because this will lead to a much larger script and will severely slow down batch operations.
Now, in order to indent all the lines in a file, just type the following command:
$ vim -s indentme.scr unindented-file.c
Vim will flash open-shut (if you're on a fast computer and not editing a huge file), indenting all lines, then saving the file in-place.
Unfortunately, this will only work on one file at a time, but you can scale the functionality easily using sh's for loop:
for filename in *.ttl ; do
vim -s indentme.scr "$filename"
done
Note: This will save-over any file. Unless set bk is in your ~/.vimrc, don't expect a backup to be saved.
I went off of amphetamachine's solution. However, I needed to recursively search through multiple directories. Here's the solution that I used:
$ find . -type f -name '*.ttl' -exec vim -s indentme.scr "{}" \;
Taking reference from the above answers I would like to make this complete.
I will start from the scratch so that a beginner can understand.
Step-1
Install astyle (tool used for formatting ) by using the following command
Open up a terminal. Type:
sudo apt-get install astyle
Step-2 Make sure you have vim installed on your system.Run the below commands from the directory in which your code files are.The below command will create a script that we intend to run recursively so as to beautify each and every file in our directory.(as mentioned in the above answer)
vim -w indentme.scr foo.c
Step-3 Then, type this exactly (in command mode) and press enter
:%!astyle
Step-4Then type this exactly (in command mode) and press enter
:wq
Step-5 Last run this recursively by the following command:
find . -type f -name '*.cpp' -exec vim -s indentme.scr "{}" \;
All your cpp files will be formatted properly.