Currently I have this binding to copy current file name, path, or dir.

" Copy current buffer path relative to root of VIM session to system clipboard
nnoremap <Leader>yp :let @*=expand("%")<cr>:echo "Copied file path to clipboard"<cr>
" Copy current filename to system clipboard
nnoremap <Leader>yf :let @*=expand("%:t")<cr>:echo "Copied file name to clipboard"<cr>
" Copy current buffer path without filename to system clipboard
nnoremap <Leader>yd :let @*=expand("%:h")<cr>:echo "Copied file directory to clipboard"<cr>

I want to create binding to copy current php file class name with or without namespace.

Suppose I have php file like this:


namespace CRMFoundation\Infrastructures\Contracts;

interface PersistenceStorageRepositoryInterface

This would should give me PersistenceStorageRepositoryInterface as classname and CRMFoundation\Infrastructures\Contracts\PersistenceStorageRepositoryInterface with namespace.


namespace CRMFoundation\Infrastructures;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use CRMFoundation\Infrastructures\Traits\SyncHasManyTrait;

abstract class EloquentAbstract extends Model

This would should give me EloquentAbstract as classname and CRMFoundation\Infrastructures\EloquentAbstract with namespace.


namespace CRMFoundation\Domains\Company;

use CRMFoundation\Domains\Company\Contracts\CompanyRepositoryInterface;
use CRMFoundation\Infrastructures\Company\Contracts\PersistenceCompanyRepositoryInterface;
use CRMFoundation\Domains\Company\Contracts\CompanyInterface;
use CRMFoundation\Domains\RepositoryAbstract;

class CompanyRepository extends RepositoryAbstract implements CompanyRepositoryInterface

This would should give me CompanyRepository as classname and CRMFoundation\Domains\Company\CompanyRepository with namespace.

Let say I want to bind it to <leader>yc to get current class name and <leader>ycn to get current class name with namespace.

How to do that?

1 Answer 1


Here's a start, it's definitely not perfect but should gives you enough to increment on

function! GetNamespaceAndClassFn()
  " Save some registers
  let l:r_a = @a
  let l:r_b = @b

  " Start at the top of the file
  " Search for the first "namespace" occurence
  " Get the namespace string into the regsiter a
  normal! f l"ayt;

  " Search for the class definition
  " Get the class string into the regsiter b
  normal! f l"bye

  " Print the result
  echo @a . '\' . @b

  " Restore registers
  let @a = l:r_a
  let @b = l:r_b

command! GetNamespaceAndClass call GetNamespaceAndClassFn()

I've commented the code, but feel free to ask if you want more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.