Inn ~/script.vim, I have:
set runtimepath+=string(substitute(expand("%:p"), 'script\.vim', '', 'g'))
I have an alias in .bashrc:
alias vimscript="vim -S ~/script.vim"
Running string(substitute(expand("%:p"), 'script\.vim', '', 'g')) works as intended.
The problem is when using it in the set runtimepath expression, it doesn't work when I call vimscript in terminal which calls script.vim. When I run set rtp in vim after being called by vimscript to check the runtimepath, the desired appended string isn't showed (but the other ones are there).
I have some additions to #Laurence Gonsalves answer:
There is also «concat and assign» operator: .=, so
let foo=foo.bar
can be rewritten as
let foo.=bar
Code
let &runtimepath.=','.string(path)
will append ,'/some/path' to &runtimepath, while you probably need ,/some/path.
I guess that you want to append path to your script to runtimepath. If it is true, then your code should be written as
let &runtimepath.=','.escape(expand('<sfile>:p:h'), '\,')
inside a script, or
let &runtimepath.=','.escape(expand('%:p:h'), '\,')
from current editing session (assuming that you are editing your script in the current buffer).
The right hand site of a set command is not an expression, it's a literal string.
You can manipulate options (the things set sets) by using let and prefixing the option name with an &. eg:
let &runtimepath=substitute(expand("%:p"), 'script\.vim', '', 'g')
To append to runtimepath with a let you can do something like:
let &runtimepath=&runtimepath . ',' . substitute(expand("%:p"), 'script\.vim', '', 'g')
(The . is the string concatenation operator.)
by 2022 i used this:
if ( isdirectory($some_shell_variable) )
set runtimepath+=$vi
endif
You can use a plain vim variable or whatever else just as well.
Related
I'm trying to add () around my Python virtual environment name like this:
(my-env) my-user#my-machine:%
and if the env is not set, it will only show:
my-user#my-machine:%
Right now I have:
MYPS1+='($PYENV_VERSION) '
which will show the () if the virtual env is not set:
() my-user#my-machine:%
Is there away I can do something like this:
MYPS1+='($PYENV_VERSION) ' if $PYENV_VERSION exists else ''
So the important thing when setting the prompt like this, is that you (probably) want to reevaluate it every time it prints the prompt. That way if and when the envvar changes, the prompt changes. That's why you have ' characters around what you're adding to the prompt -- it prevents any vars or code in there from being evaluated when you set it. While you could do something like you suggest (with an if in the shell) that would not be reevaluated when the prompt was printed, so could be "stale".
Instead you want to do it all in the variable expansion. sh/bash comes with a variety of ways of expanding variables but the one you want is
${parameter:+word}
Use Alternative Value. If parameter is unset or null, null is substituted; otherwise, the expansion of word is substituted.
That means you want something like
MYPS1+='${PYENV_VERSION:+($PYENV_VERSION) }'
In vimscript, I cannot find the way of saving the return value of the execute function into a variable.
I would like to do the following:
let s = execute(":!echo dani")
echo s
This should return: dani
Vim doesn't accept this. In my setup (using vim-airline and other UI plugins) the screen blanks all content and upon a key press it goes back to normal.
Is it possible in vimscript to save into a variable the return of either a function call, or conversely the return of the execute function?
Thanks SO
execute() is the modern alternative to :redir; it does capture all output of the executed command. Let's look a bit more closely:
:let s = execute(":! echo dani") | echo strtrans(s)
^#:! echo dani^M^#dani^M^#
As you can see, it captures the entire command and its result. If you use plain :echo, the newlines and ^# obscure the full output (you'll see it better with :echomsg, which does less interpretation of special characters).
I think what you really want is just the output of the executed external command (here: echo). You can use system() instead of :! for that:
:let s = system('echo dani') | echo strtrans(s)
dani^#
That trailing newline usually is removed like this:
:let s = substitute(system('echo dani'), '\n\+$', '', '') | echo strtrans(s)
dani
I'm trying to change the exstension of a file passing the arguments by console
system = "rename" , "'s/\#{ARGV[0]}$/\#{ARGV[1]}'", "*#{ARGV[1]}"
The code is correct because it works on console but when I put it in a script I have trouble with
s/\#
because it appears in pink and the console does not get it.
you don't want to send literal single quotes, so remove them.
you want to remove the backslashes so you let Ruby evaluate those expressions.
you're missing the trailing slash.
what's that equal sign doing?
did you want ARGV[0] in the last argument to rename, instead of ARGV[1]?
you want to use * wildcard, which requires a shell to expand into a list of files, which means you can't use the list form of system
Try
system "/usr/bin/rename -n 's/#{ARGV[0]}$/#{ARGV[1]}/' *#{ARGV[0]}"
Remove the -n option if it looks like you're going to rename the way you want.
And, of course, you don't need to call out to the shell for this:
Dir.glob("*#{ARGV[0]}").each {|fname|
newname = fname.sub(/#{ARGV[0]}$/, ARGV[1])
File.rename(fname, newname)
}
My problem lies with my confusion with shell variables.
To my understanding, variables allow me to store a value (String in this case) and to call it later in my code. So if I wanted to have a variable that holds the path to some set of scripts, I could ideally just store it like this:
SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'
//Later on in the script//
cd $SPTH
./script1
What I'm trying to do, with probably the wrong syntax, is to set the path to variable SPTH.
Then I use cd with argument $SPTH.
Ideally this would allow me to run the file there without typing in the path. However it doesn't work. The $SPTH is ignored and the result is as if cd was used alone.
So what am I doing wrong? And what would be a way to do this?
Don't use spaces...
(Incorrect)
SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'
(Correct)
SPTH='/home/Foo/Documents/Programs/ShellScripts/Butler'
To add to the above correct answer :-
For my case in shell, this code worked (working on sqoop)
ROOT_PATH="path/to/the/folder"
--options-file $ROOT_PATH/query.txt
Emacs: C-U (79) # » a pretty 79 character length divider
VIM: 79-i-# » see above
Textmate: ????
Or is it just assumed that we'll make a Ruby call or have a snippet somewhere?
I would create a bundle command to do this.
You can take editor selection as input to your script, then replace it with the result of execution. This command, for example, will take a selected number and print the character '#' that number of times.
python -c "print '#' * $TM_SELECTED_TEXT"
Of course this example doesn't allow you to specify the character, but it gives you an idea of what's possible.
By taking the
python -c "print '#' * $TM_SELECTED_TEXT"
a step further, you can duplicate the examples you gave in the question.
Just make a snippet, called divider or something, set the tab trigger field to something appropriate '--' for example, then enter something like:
`python -c "print '_' * $TM_COLUMNS"`
Then when you type --⇥ (dash dash tab), you should get a divider of the correct width.
True, you've lost some of the terseness that you get from vim, but this is far easier to reuse, and you only have to type it once. You can also use whatever language you like.
Inspired by the other answers. Make a snippet with the following:
`python -c "print ':'.join('$TM_SELECTED_TEXT'.split(':')[:-1]) * int('$TM_SELECTED_TEXT'.split(':')[-1])"`
and optionally assign a key sequence to it, e.g. CTRL-SHIFT-R
If you type -x:4, select it, and call the snippet (by it's shortcut for example), you'll get "-x-x-x-x".
You can also use ::4 to obtain "::::".
The string you repeat is enclosed in single quotes, so to repeat ', you have to use \'.