zsh completion of paths with a prefix - path

I'm trying to get zsh to autocomplete a custom command.
The idea is that in my command foo:
foo --args #path/to/fi
all arguments with an "#" at the beginning should be autocompleted the same as regular files, keeping the "#".
So in the example above, when pressing <TAB> I would expect autocompletion to suggest file1.txt or file2.txt (or #path/to/file1.txt, #path/to/file2.txt), given these two files exist in the folder path/to, so I could autocomplete it to, for example:
foo --args #path/to/file1.txt
I've looked at the tutorial and the documentation, but can't quite get to grips with the syntax...
I expect I need something in:
_foo() {
# What to add here?
}
#compdef foo

Related

Search for files that contain pattern

I have this search - I would like to print out the paths of files that contain the matching text:
grep -r "jasmine" .
and it yields results that look like this:
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:- [Jasmine Google Group](http://groups.google.com/group/jasmine-js)
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:- [Jasmine-dev Google Group](http://groups.google.com/group/jasmine-js-dev)
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:git clone git#github.com:yourUserName/jasmine.git # Clone your fork
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:cd jasmine # Change directory
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:git remote add upstream https://github.com/jasmine/jasmine.git # Assign original repository to a remote named 'upstream'
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:Note that Jasmine tests itself. The files in `lib` are loaded first, defining the reference `jasmine`. Then the files in `src` are loaded, defining the reference `j$`. So there are two copies of the code loaded under test.
./app-root/runtime/repo/node_modules/jasmine-core/.github/CONTRIBUTING.md:The tests should always use `j$` to refer to the objects and functions that are being tested. But the tests can use functions on `jasmine` as needed. _Be careful how you structure any new test code_. Copy the patterns you see in the existing code - this ensures that the code you're testing is not leaking into the `jasmine` reference and vice-versa.
But I just want the file names, I don't want to print out the matching contents, just the file names, how can I do that?
The problem is that the matching text will wrap around in the terminal and make the results basically unreadable.
Did you use the -l flag from grep?
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.
A simple search on my home directory,
grep -rl 'bash' .
./.bashrc
./.bash_history
./.bash_logout
./.bash_profile
./.profile
./.viminfo
As a matter of fact, -l is a POSIX defined option for grep should be available in almost all distros.

Generating a better ctags file for Rails projects

How can I generate a good ctags file for a rails project ? One that includes all the gems, ruby libs and sorts them in the right order (like when you search for “Rails” it doesn’t go to the airbrake gem or whatever)
I'm using this right now, but I don't like it for the reason mentioned above:
ctags --tag-relative -Rf.git/tags.$$ --exclude=.git --exclude=*.html --exclude=tmp --exclude=public --exclude=app/assets --languages=-javascript,js,sql,html `bundle show --paths` $MY_RUBY_HOME .
So it turns out the better option (better than ctags) is either Robe for Emacs or a combination of vim + tmux + vmux + pry (to get show-source and edit).
irb-config is an interesting example of vim setup.
Neither Vim nor ctags understand or can be taught to understand your code and/or your thoughts. If you want to jump to a different tag than the first matching one in your tags file, use the proper commands:
:tselect foo " list all tags 'foo'
:tselect /foo " list all tags containing 'foo'
g] " list all tags matching the word under your cursor
Also, note that you can make "your" ctags command a little shorter by whitelisting filetypes:
ctags --tag-relative -Rf.git/tags.$$ --exclude=.git --exclude=tmp --exclude=public --exclude=app/assets --languages=ruby `bundle show --paths` $MY_RUBY_HOME .

How to get Swagger UI to use my titles?

I am using Swagger-php and Swagger-UI and it all works just fine but for one annoyance. The UI that Swagger-UI creates has the expected click-to-expand sections for my API routes, but the title of each one appears to be the JSON file generated and not any name I can give it. After the title, the description is the one I give in my annotation, but the title I seem to have no control over.
So if I have routes that begin with a resourcePath of /foo, and a description that says "Foo API Functions," the UI looks like:
foo.json : Foo API Functions
I don't want "foo.json" I'd much rather specify what this says. Like just "Foo" or even "Foo Functions" and then change my description to something more meaningful like, "This is where you find the foo functions."
Am I missing which annotation to use for this?
If you manually edit the api-docs.json file. You can replace the .json with .{format} and all will display correctly and function correctly. Not sure why the .{format} is not inserted by default. Slightly annoying.
I too ran into this problem, but I couldn't find anything on either theswagger-php or swagger-ui github pages mentioning this. So, I wrote a short build script (assuming a Unix-like OS) as a work around, which will first build all the docs using swagger.phar and then run sed to do an inline string replace on the problematic string. Here is what I did below:
#!/bin/bash
# write API documentation from "src" directory to the "docs" directory
/usr/bin/php swagger.phar src -o docs
# replace instances of "json" with "{format}" to fix swagger-php formatting issue
sed -i -e 's/json/{format}/g' docs/api-docs.json
Fixed in swagger-php 0.9.1
I don't know why swagger-ui strips out ".{format}" but not ".json"
The .{format} was not inserted by default because it might be confusing. It suggests the presence of different formats and swagger-php only supports the json format.

Way to remove all -viewDidUnload from project's view controllers

As Apple's Official Doc said:
-viewDidUnload is deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.
Meanwhile, it's better to remove all -viewDidUnload implements from projects that the deployment target is iOS 6.0 or later. But removing it one by one is boring, especially you've hundreds of controllers & several projects.
So, is there any script or command that can do this batch job?
I figured out a command for Vim, I'm sure it'll make your life easier. ;)
Open Vim in desired root dir (generally, it'll be your root dir of your all controller files), press Shift : to enter command-line mode, run the command below:
:args **/*.m | argdo %s/^-\ (void)viewDidUnload\_.\{-}}\n^$\n//ge | update
This will replace all
- (void)viewDidUnload
{
[super viewDidUnload];
...
}
// a blank line here
to empty, in other words, the -viewDidUnload snippet will be removed. But note, there's a blank line after the implement. If you don't want to include it, use
:args **/*.m | argdo %s/^-\ (void)viewDidUnload\_.\{-}}//ge | update
instead.
Note: This will include lib files as well when you're in your project's root dir. I suggest go to your Controllers' root dir, then all files & you will be happy with the result. ;)
Tip:
After that, you'd better check whether all -viewDidUnload were removed, run the command below on terminal of your desired root dir:
$ find . -type f -iname "*.m" -exec grep -i "viewDidUnload" {} +
It should result with no output. Otherwise, some targets missed, you can locate it easily cause it'll list all files that not managed correctly, then just remove it manually (This might happen if your - (void)viewDidLoad is -(void)viewDidLoad without a space, or something else).
Finally, you can run
$ git diff -b --color-words
to see the result. Enjoy it! ;)
Further reading about the command
args **/*.m: Sets arglist to contain all .m files in current directory and it's subdirectories;
argdo: Run the following command(s) on all arguments in arglist;
%s/^-\ (void)viewDidUnload\_.\{-}}\n^$\n//ge: Vim RegEx to replace -viewDidUnload implement snippet to empty;
\_.: Find any character including end-of-line (linebreak).
\{-}: Match as few as possible (stopping at the first }, e.g. \{-}h will stop at the first h);
}: The last item to match;
\n: Line-break;
^$\n: blank line.
update: Save modification only if the buffer has been modified.
Reference
Search Across Multiple Lines
Search and Replace in Multiple Buffers

How to debug broken vim completefunc in haml files?

I'm using rails.vim and I love how you can use ctrl-x ctrl-u in insert mode to autocomplete long method names like distance_of_time_in_words and accepts_nested_attributes_for. But for some reason it doesn't work in haml files and I can't seem to figure out what's wrong or how to fix it.
:help i_CTRL-X_CTRL-U says the autocompletion is using completefunc. The haml file says its completefunc=syntaxcomplete#Complete (and it's the same in erb and helper files where ctrl-x ctrl-u works fine.) I can't find where the syntaxcomplete#Complete magic is defined, but presumably it has something to do with the filetype syntax. My .vim/syntax/haml.vim comes from vim-haml, so I tried removing it but the problem persists.
Commenting out my entire .vimrc didn't help either. What else can I try?
UPDATE: I searched my vim config files and the only place that looks like it's doing anything with syntaxcomplete#Complete is in autoload/rails.vim and looks like this:
function! s:resetomnicomplete()
if exists("+completefunc") && &completefunc == 'syntaxcomplete#Complete'
if exists("g:loaded_syntax_completion")
" Ugly but necessary, until we have our own completion
unlet g:loaded_syntax_completion
silent! delfunction syntaxcomplete#Complete
endif
endif
endfunction
Functions with # in name are defined in autoload scripts:
A function that can be autoloaded has a name
like this:
:call filename#funcname()
When such a function is called, and it is not defined yet, Vim will search the
"autoload" directories in 'runtimepath' for a script file called
"filename.vim". For example "~/.vim/autoload/filename.vim".
See :help autoload.
Looks like the file you're looking for is part of vim:
$ find /usr/share/vim/ -iname "syntaxcomplete.vim"
/usr/share/vim/vim72/autoload/syntaxcomplete.vim
There's a vim.org page with the latest version of the script.
If you want to customize the completion, you can just write your own completion function. See :help complete-functions
You can also use Ctrl+n to use vim's simple completion. It completes with text from any of the open files, but can be customized. (See :help 'complete')

Resources