I am trying to do something pretty simple in my vimrc style. As you can see below I am trying to concat two environmental variables into a local variable and then use that variable.
let cs=$menv_dotfiles_dir."/vimrc_style/".${CODING_STYLE}.".vim"
if filereadable(cs)
source cs
endif
Of course the above doesn't work but I think it expresses what I need to do. What is the correct way to do this?
Similar to this: Vim: sourcing based on a string But I need to do it only if the file exists.
You were very close the {} brackets do not work like they do in bash. You have to use exec if you want source from a string variable.
This works:
let cs=$menv_dotfiles_dir."/vimrc_style/".$CODING_STYLE.".vim"
if filereadable(cs)
exec 'source ' . cs
endif
Related
I am trying to create a macro to use it globaly. I have inserted this command to my .vimrc file
let #c='<esc>:r c.c'
or
let #c=':r c.c'
but in both cases when I use "#c" on any file it only prints 'c>:r c.c' on the the file
Try adding a '^M' at the end of your macro, then "#c" should work. Else ':#c' should work as mentioned by ebenezer. You should use Ctrl+VEnter to insert '^M'.
let #c=':r c.c^M'
Best way would be to record the macro first and then save it to the .vimrc.
If these doesn't work, you can check the content of your register c using "cp and see if there is something missing.
This is described in another answer. Try something like this:
let #c = ':r c.c'
and then, in your file, use
:#c
to execute it.
If you want to include special characters in your macro (like Escape or Enter) then use a non-literal string (double quotes, not single quotes) and the correct syntax. See :help expr-string. You could use raw special characters, as #Amit suggests, but this will make your vimrc file hard to read.
Assuming that you are starting in Normal mode and want to type #c, there is no need to start off with an Escape.
For testing purposes, I tried
:let #c = ":echo 'foo'\<CR>"
and it worked as expected. I even added this line (without the leading :) to my vimrc file just to make sure, and tested it that way. This should work:
:let #c = ":r c.c\<CR>"
I would like something similar too this in my .vimrc.
let dir=“/home/user/Downloads/”
set path=$dir
nnoremap gr :grep '\b<cword>\b' $dir/*<CR>
The code above is wrong of course, but maybe you can understand what I am trying to do. I would like to set path to the value of dir to /home/user/Downloads/, and replace the word dir in the third line with the value of dir. I tried and failed, can anyone tell help me out, any help appreciated!
First of all, there are fancy quotes; you need to use plain (") ones. Other than that, the :let is okay.
let dir = "/home/user/Downloads/"
You could use :execute to evaluate the defined variable with :set, but it's easier to use :let, because it can change Vim options, too, with the special notation &{optionname}:
let &path = dir
For the mapping, if dir doesn't change during runtime, it's easiest to use :execute. Note how the quoted backslashes must be escaped (i.e. doubled):
execute "nnoremap gr :grep '\\b<cword>\\b' " . dir . "/*<CR>"
All that information is part of :help eval. Learn how the excellent and comprehensive help is structured; all the information is in there (you just need to know how to find it)!
You must use this notation:
let variable_name = "value"
and use straight quotes.
To set path:
set path=/home/user/Downloads/
or to append a directory to path.
set path+=/home/user/Downloads/
Path is an Vim variable which does not seem very appropriate to use if you are going to only use it for this one remapping. It would be better to declare you own variable, as path can also have many directories within it which will not work with grep.
let g:search_path="/path/to/your/dir"
nnoremap gr :grep '\<<cword>\>' <C-R>=eval("g:search_path")<CR>
Ctrl+R lets us insert a register here, when then use that to call the expression register, which we use to evaluate g:search_path.
Check out :help expr-register for more on that!
This will evaluate your g:search_path variable on every execution of the mapping, allowing you to change the path and not have to remap gr each time.
In a script, I have the following not working code:
set eval(rules[formatoption])=value
where rules is a dictionary and formatoption and value are a variable. I want to make Vim read the variable name from rules[formatoption] and set it to value. How can I make Vim set a variable this way? I think there should be a function like setvar(name, value)or something similar, that sets name(string) to value. That line of code would save me from writing about 30 lines of code in a 70 lines script.
Use :execute:
execute 'set' rules[formatoption] . '=value'
You can also change Vim options via :let &optionname = ..., but that doesn't help here. There's also the obscure :help curly-braces-names, but that won't work here, neither.
I'm trying to add an autocmd to vim that will execute whenever I open a file in a certain subdirectory and that sets the search path. Unfortunately path name expansion doesn't seem to work inside a set command.
Specifically I'd like to have a line like this in my vimrc:
setlocal path+=**;%:p:h
But this will just give me the literal value. Just calling expand() doesn't work either. Is there a way to get variable expansion to work here?
What about:
execute 'setlocal path +=**;' . fnameescape(expand('%:p:h'))
There's no need for the expansion of the current file's directory; just adding . to path will do. From the help:
To search relative to the directory of the current file, use:
:set path=.
Use
let &l:path.=(empty(&l:path)?(''):(',')).'**;'.escape(expand('%:p:h'), ',\*; ')
. This is much cleaner then using :execute 'setlocal path', especially knowing that fnameescape() was designed to escape paths for commands, not for options and I can say it is not really safe to use it here: it definitely is not going to escape comma and semicolon and add additional escape for space (one for escaping for :set, one for the option itself). (empty(&l:path)?(''):(',')) is here to imitate the behavior of set+=.
When I try to create a bookmark in NERDTree (win7 with emacs installed)
:Bookmark mybookmark
I get this:
E482: Can't create file C:\emacs\home/.NERDTreeBookmarks
NERDTree is trying to write the bookmark to "$HOME/.NERDTreeBookmarks" by default. This is how it looks like in the code:
call s:initVariable("g:NERDTreeBookmarksFile", expand('$HOME') . '/.NERDTreeBookmarks')
While it's possible that you've set the "g:NERDTreeBookmarksFile" variable somewhere in the configuration, it's a lot more likely that emacs has, for some reason, set your "HOME" environment variable to "C:\emacs\home". This explains the slash/backslash mixup as well. You can try two things:
Change the $HOME variable to your home dir, "C:\Users\your-username". A quick google turns up this guide for windows 7: http://www.itechtalk.com/thread3595.html
Just set the "g:NERDTreeBookmarksFile" variable to your home dir ("C:\Users\your-username").
I'd recommend the second option, since it's definitely going to work. You may need to escape the backslashes and spaces, but I can't be sure how at the moment. Try it out in all of these ways and see which one works for you:
let g:NERDTreeBookmarksFile = "C:\Users\Your\ Username"
let g:NERDTreeBookmarksFile = "C:\\Users\\Your\ Username"
let g:NERDTreeBookmarksFile = 'C:\Users\Your Username'