0

How to remove 2 characters from the beginning of a string after using filename-modifiers? So assume that I have a files that looks like this:

/home/drskoolie/projects/examples/ex40.c

/home/drskoolie/projects/examples/ex1.c

What I want is to get the number 40 and 1 out of the file names respectively using filename-modifiers (this is later going to a function that will send it to another tmux window). Writing this command gets me almost there: :!echo '%:p:t:r'

ex40

ex1

How do I now remove the first two characters here to get:

40 

1
1
  • 1
    You can't use filename modifiers for this; you can use something like trim('ex') or matchstr('\d\+') to get the number. If you want it to work in the shell context with :! you'll have to use something like | grep -o '\d+'. Sep 17, 2022 at 14:25

2 Answers 2

1

Any of the following would work with your specific examples:

:echo expand('%:p:t:r')->matchstr('\d\+$')
:echo expand('%:p:t:r')->trim('ex', 1) " the third direction argument might be new?
:echo expand('%:p:t:r')[2:]
0

Probably not the best solution, but should work :)

Command

!echo $(var=%< && echo ${var/ex/})

Explanation

  1. starts subshell and asigns %< to var (%< means current file without extenction)
  2. echo the variable var with a command substitution, 'ex' -> ' '

In short: Echo current filename without extenction and replace 'ex' with nothing.

Endword

Hope this helped you :)

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.