Opening a db/migrate/* file more effectively? Wildcard to complete the beginning of the name of the file? - ruby-on-rails

Working from a bash shell and utilizing vim, I generally have a pretty effective workflow. However, when I attempt to access files in the db/migrate directory of a rails project, it becomes very tedious to access the files as the each contain a long integer at the being of their file names. I've tried vim db/migrate/*name_of_migration.rb but to no avail.
Is there a way to access files via wildcard of in this manor?

If you're using vim-7.3, then you can do this from inside vim:
:set path=/path/to/your/project/root/**
:find migrate/*cr<tab>
and vim will show you the possible candidates for completion.

If you're typing the name of the migration correctly, I assure you that the * will match the leading digits.
For example, from the root of your project,
$ vim db/migrate/*create_users.rb
will open 20111123142812_create_users.rb.
Otherwise, my preferred method is to use
$ vim db/migrate
to "open" the directory in vim, and use the in-vim navigator to select the migration you're interested in from the list of files.

Related

How do I save a ruby file? (error)

I am receiving the error No such file or directory in my command line and I think it's because I am not saving the ruby files somewhere specific. All I did was create a random folder where I would save my ruby files.
Do I need to save my scripts in the original ruby folder? Thanks!
** This is Windows 7.
More info -
All I did was make a simple file named "Matz.rb" because I'm currently reading the O'reilly Ruby book. In my code all I wrote was puts "Hello Matz". I saved this on my desktop. When I go to the command line it and I write ruby matz.rb it says "ruby: No such file or directory -- matz.rb " Please help :(
If this has something to do with PATH or shells, I honestly have no idea what those really are because I just started coding last night.
You are most likely not in the right folder. You somehow need to tell the ruby interpreter where it is looking for the file.
Either make sure you're in the right folder - the cd command allows you to change location:
cd C:\Users\Username\Desktop
ruby Matz.rb
or specify the path explicitly:
ruby C:\Users\Username\Desktop\Matz.rb
By default, the ruby interpreter will look in your current directory (the location shown in your prompt) for whatever filename you give it.
Edit: I'll attempt to explain what I mean step-by-step.
When you initially open the command prompt, it will indicate what folder you are in (your "current working directory") in the prompt:
C:\Users\YourUsername >
In the above example, you are working in the C:\Users\YourUsername folder.
You can move directories using the cd command. For example, typing cd Desktop moves you into the folder called Desktop, assuming such a folder exists in your current location
You can move to another folder outside your current folder by specifying explicitly where you want to be: cd C:\Another\Place
When you run a ruby command such as ruby Matz.rb, the system knows how to find the ruby program because the installer placed its location into the PATH environment variable. Don't worry about this too much, this just explains the "magic" by which it knows what ruby means, no matter where you are.
Ruby finds the file you specify (in the above example, Matz.rb) by looking in the current directory. To re-iterate, it is looking in whatever folder is written right there in your prompt.
You can tell ruby to look outside the current folder by specifying the full path (as shown in the answer above).
To go from a new command window that you've just opened, to typing ruby Matz.rb and having it work, you need to do the following:
Move to the correct directory
Run the command
If we assume your username is alex and you have a folder on your desktop called "rubycode", which contains Matz.rb, you could do this:
Open a command prompt, which will most likely start in C:\Users\Alex
Move to the rubycode folder on your desktop: cd Desktop\rubycode. All subsequent commands will be working from within this folder.
Run the ruby command, specifying the file: ruby Matz.rb
Continue to run ruby commands as you learn ruby.
I hope that makes sense.

Ruby on Rails Tutorial - 5.26 - Sublime Text "Unable to Save" new file "spec/support/utilities.rb"

I am using Sublime Text 2 while following Michael Hartl's Ruby on Rails Tutorial.
The specific portion of the tutorial to which I am referring can be found at http://ruby.railstutorial.org/book/ruby-on-rails-tutorial (ctrl+F "Listing 5.26").
I am able to create the spec/support file. However, when trying to create the spec/support/utilities.rb file, I receive the message "Unable to save ~/rails_projects/sample_app/spec/support/utilities.rb".
Does anyone know why this might be?
Someone on the Sublime Text forum seems to have had the exact same problem: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=8570&p=36922#p36922
This issue sounds like it's a result of incorrect permissions or ownership of the folder. Change directories (cd) so you're outside of the folder where you're creating the .rb file and type:
ls -l
This terminal command lists the permissions attached to all files/folders in that directory. If "root" is listed as the folder owner, change its owner by typing:
sudo chown YOUR_COMP_USER_NAME FOLDER_NAME/
You should now be able to save files from inside that folder.
To diagnose this, first find out if it's an issue in Sublime or your file system:
Does that file already exist? Try looking for it on your file system (not using Sublime).
Verify that you have permission to write to that file. Use "ls -la" on the command line to show the file permissions.
Are you able to create and/or save that file using any different editor, for example TextMate, or Notepad?
The following sublime plugin fixed the Unable to save... bug
https://gist.github.com/3779601
The folder spec/support doesn't exist, and sublime won't create the missing folder, so it errors.
You just need to make the spec/support folder , then sublime will save the file.
I also highly recommend installing the AdvancedNewFile plugin ( Video of it action thanks to Jeffrey Way and NetTuts+ ) , which you can grab straight from Package Control.
It creates files, parent folders if needed, and if you try to create a file that already exists, it opens it instead.
This can happen if you are trying to create the file within a directory that does not currently exist. For ex. I was unable to save
~/rails_projects/sample_app/app/views/shared/_error_messages.html.erb
via the "subl" command from the Terminal because I was missing /shared/ folder. Hope that helps.
so #knice almost had it, I ran into the same problem with permission when starting my first rails project on mavericks.
as mentioned if you run ls -l you'll see your folder / files listed with their permissions
I solved this by changing ownership recursively with the following command from outside my project directory
sudo chown -Rv <your_username> <your_path_and_foldername>
for example if you're in your folder in terminal you should cd .. and then
sudo chown -Rv username ruby_proj/
the -R is for recursive meaning it will apply to all files and folder contained within the folder you specified, and the v after just produces verbose output, showing you which folder and file permissions have been changed.
Hope that helps someone else.

How to 'set path' automatically when opening Vim from a directory?

I'm trying to get :A (e.g. switch between controller and spec) working in vim-rails. If I navigate to my rails project, run vim ., open a controller file and run :A, I get the error:
E345: Can't find file "app/controllers/widgets_controller.rb" in path
If I then set the path explicitly:
:set path=/Users/me/Documents/Code/my-project
then :A works as expected. How can I set the path initially when I open a directory with Vim?
Not exactly when opening a directory: since you seem to be working with projects, give the project plugin a try.
Using that, you could execute arbitrary commands when entering or leaving a project.
From the plugin description:
You can use this plugin's basic functionality to set up a list of
frequently-accessed files for easy navigation. The list of files
will be displayed in a window on the left side of the Vim
window, and you can press or double-click on
filenames in the list to open the files. This is similar to how
some IDEs I've used work. I find this easier to use than
having to navigate a directory hierarchy with the file-explorer.
It also obviates the need for a buffer explorer because you
have your list of files on the left of the Vim Window.
This is what I do to have a local .vimrc file per project:
In my ~/.vimrc file I define the following:
let s:project_root = finddir('.git/..', expand('%:p:h').';')
let s:local_vimrc = join([s:project_root, '.vimrc'], '/')
if filereadable(s:local_vimrc)
exec "source " . s:local_vimrc
endif
In the project root (which usually has a .git dir) I do the following:
touch /path/to/project/.vimrc
In the .vimrc file, I prepend the path variable (notice the ^path). Using :set path^= instead of :set path+= prepends the new directory to the beginning of the path instead of appending it to the end. This makes it faster for the find command to search for your files.
let s:project_root = finddir('.git/..', expand('%:p:h').';')
exec 'setlocal path^='.s:project_root
setlocal wildmode=longest,list,full
setlocal wildmenu
setlocal tags=/path/to/project/root/tags
Now the :find command should only display files and directories relative to the project root.

How to see vimrc files used during startup

I've moved my vimrc file out of the home directory to pathogenize the setup, but it looks like my vim is still picking up a redundant vimrc file that I made somewhere. Is there some variable in vim that I can echo that'll show what files were used during startup?
:scriptnames lists all sourced script names, in the order they were first sourced.
Look for the $MYVIMRC variable:
:echo $MYVIMRC
After starting vim, you can type
:set runtimepath
See the docs here: http://vimdoc.sourceforge.net/htmldoc/options.html#'runtimepath'
I don't think #skinp's answer is necessarily sufficient. the runtimepath variable is the very definition of all places vim looks
Additionaly, you can launch Vim with
$ vim --startuptime filename
which should write a list of loaded scripts (including vimrc) and their respective processing time to file filename.

vimrc - current working directory

I would like to be able to access the current working directory in my vimrc.
For example, I can access the current file by using %.
Specifically,
I have the following line in my vimrc:
map ,l :!latex %
When it runs everything works fine, except the resulting dvi and other files are stored in my home directory instead of my current working directory.
Any suggestions?
See :help autochdir. Vim can automatically change the current working directory to the directory where the file you are editing lives.
Otherwise, if you want to do this manually, see :help cd and :help lcd.
see :he filename-modifiers
:!latex % -output-directory %:h
Most likely, you're running vim from your home directory, so it is the current for him. The latex command, being invoked from vim, also therefore has the home directory as current.
You probably know this, and want just to extract path from the filename and supply it as an argument to -o option of the latex command. Just use the shell capabilities:
:!latex % -output-directory `dirname "%"`
I am not sure that it's -output-directory option, but you get what you asked for--a directory name of the file you're editing.

Resources