put filename in string

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

put filename in string

rameo
I created a script to delete the current file:

fun! s:Deleter()
    if !exists("g:delete_dialogue")
      let g:delete_dialogue = "Are you sure to delete this file?"
    endif
    if !exists("g:confermdeletefile")
      let g:confermdeletefile = "&Yes\n&No"
    endif
    let f = confirm(g:delete_dialogue, g:confermdeletefile,
"Question")
    if f == 1
          :silent exe "echo delete(@%)"
          echo (@%) "deleted"
          exe "bw!"
        elseif f == 2
          return
    endif
  endfun

----------------------------------

This script works well.
I only would like to insert the current filename in the question:

Are you sure to delete this file? -->
Are you sure to delete "filename.ext"?

How can I realize this?

Tnx in advance.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: put filename in string

Michael Henry-5
On 10/16/2010 05:20 AM, rameo wrote:
> I created a script to delete the current file
[...]
> I only would like to insert the current filename in the question:
>
> Are you sure to delete this file? -->
> Are you sure to delete "filename.ext"?

You can change your variable assignment to the following:

  let g:delete_dialogue = 'Are you sure to delete "' . @% . '"'

The "." operator concatenates strings.  Look for "string
concatenation" in the help under this topic:

  :help expression-syntax
 
Michael Henry

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: put filename in string

rameo
Thank you very much :)

I noted that '@%' does an insert of the whole path.
Do you know what do I have to use to insert only the filename?

Rameo

On Oct 16, 12:31 pm, Michael Henry <[hidden email]> wrote:

> On 10/16/2010 05:20 AM, rameo wrote:
>
> > I created a script to delete the current file
> [...]
> > I only would like to insert the current filename in the question:
>
> > Are you sure to delete this file? -->
> > Are you sure to delete "filename.ext"?
>
> You can change your variable assignment to the following:
>
>   let g:delete_dialogue = 'Are you sure to delete "' . @% . '"'
>
> The "." operator concatenates strings.  Look for "string
> concatenation" in the help under this topic:
>
>   :help expression-syntax
>
> Michael Henry

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: put filename in string

Nikolay Aleksandrovich Pavlov
Reply to message «Re: put filename in string»,
sent 13:18:56 17 October 2010, Sunday
by rameo:

> I noted that '@%' does an insert of the whole path.
> Do you know what do I have to use to insert only the filename?

Use fnamemodify: replace @% with fnamemodify(@%, ':t').

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: put filename in string

Jürgen Krämer-4
In reply to this post by Michael Henry-5

Hi,

Michael Henry schrieb:

> On 10/16/2010 05:20 AM, rameo wrote:
>> I created a script to delete the current file
> [...]
>> I only would like to insert the current filename in the question:
>>
>> Are you sure to delete this file? -->
>> Are you sure to delete "filename.ext"?
>
> You can change your variable assignment to the following:
>
>   let g:delete_dialogue = 'Are you sure to delete "' . @% . '"'

this would only work correctly for the first call of s:Deleter(), because
after the first call g:delete_dialogue would include the name of the first
file to be deleted.

If the test at the start of s:Deleter() was put there to be able to set
the text of the question from the OP's ~/.vimrc I'd suggest to use a
string with a placeholder which can be replaced with the actual filename
when s:Deleter() is called. As there already is the builtin printf()
function, "%s" would be a good choice. Putting this together with
Michael's and ZyX' suggestion the OP's function might look like this:

    fun! s:Deleter()
        if !exists("g:delete_dialogue")
            let g:delete_dialogue = 'Are you sure to delete "%s"?'
        endif
        if !exists("g:confermdeletefile")
            let g:confermdeletefile = "&Yes\n&No"
        endif
        let question = printf(g:delete_dialogue, fnamemodify(@%, ':t'))
        let f = confirm(question, g:confermdeletefile, "Question")
        if f == 1
            :silent exe "echo delete(@%)"
            echo (@%) "deleted"
            exe "bw!"
        elseif f == 2
            return
        endif
    endfun

Now if I wanted to use this function I could put

    let g:delete_dialogue = 'Möchten Sie "%s" wirklich löschen?'

in my ~/.vimrc and would get the question in German.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php