1

So I made this mapping

"%p$?/<CR>d$V"uy:cd <C-r>u<CR>

That is the crux of the mapping but it leaves artifacts in the current file, that is part of what I want to avoid.

My attempt at creating a function is more or less a direct translation thus far.

My problem is that I really don't know vimscript very well. Any help is appreciated, even some good documentation, or a pointer to relevant help files in vimhelp.

3

If I understand your mapping properly it is used to get the directory containing the current buffer and then cd to this directory.

You say that your mapping leaves characters in your current buffer and that is why you want to use a function. I think that using a function here is not necessary you just need to write a better mapping. By "better mapping" I mean a mapping which will use built-in features and will not have to modify your buffer just to change the working directory.

For example something like this would do the job:

nnoremap <yourkey> :execute "cd " . expand('%:h')<CR>

Let's break the executed command down:

execute "cd " . expand('%:h')

execute                         will take a string and execute it as a command
        "cd "                   is the beginning of the command you want to execute
              .                 allows to concatenate two strings
                expand('   ')   will be used to tranform a file name as we want
                        %       is the current file name
                         :h     tells to expand() to return only the directory of the file

You can try to use directly in the command line :echo expand('%:h') to understand better what's happening.

Here are some help topics you might want to read:


Edit As @LuHermitte pointed out in the comments, while the previous mappings works using execute and expand() is clearly overkill since the command line can substitute %:h on its own. So the mapping should be

nnoremap <silent> <yourkey> :cd %:h<CR>

(Use <silent> to avoid echoing the mapping in the command line)

  • 1
    Thanks that did the job. Not only did this solve my problem also pointed to reference materiel. – iobates Apr 10 '18 at 13:37
  • :cd %:h should be enough in that case. – Luc Hermitte Apr 10 '18 at 13:40
  • @LucHermitte Yes clearly I don't know why I overthought this one! – statox Apr 10 '18 at 13:41
  • Well I appreiciate the effort either way :) – iobates Apr 10 '18 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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