vim: Bind leader key to open .vimrc in a different path - vim

I have my .vimrc in a different path, that I source from my main ~/.vimrc (so I can share same settings across Windows, bash on Windows, etc).
I'm trying to write something in the .vimrc in question, that would make a hotkey for editing said .vimrc, without hard coding the path.
What I currently have is this:
let g:vimrc_path = expand('<sfile>')
:map <Leader>v exec(":e " + g:vimrc_path + "<CR>")
But this doesn't seem to do anything. I've verified that g:vimrc_path is the right value, and that the <Leader>v ends up being called by subbing in echo messages, but I'm not wrapping my head around why the variable I'm trying to define doesn't get expanded correctly.

String concatenation is done with ., not with +, which performs coercion into numbers and addition. But :execute takes multiple arguments (which it space-separates), so you don't actually need this here.
You should use :noremap; it makes the mapping immune to remapping and recursion.
Also, I doubt you need visual and operator-pending modes (:help map-modes), so define this just for normal mode.
:exec[ute is an Ex command, so for a normal-mode mapping, you need to first enter command-line mode. So :exec 'edit' instead of exec ':edit'.
Also, this is not a function (though Vim 8 now also has execute()), so the parentheses are superfluous.
The <silent> avoids the printing of the whole command (you'll notice the loading of the vimrc file, anyway); it's optional.
The fnameescape() ensures that pathological path names are also handled; probably not necessary here.
let g:vimrc_path = expand('<sfile>')
nnoremap <silent> <Leader>v :execute 'edit' fnameescape(g:vimrc_path)<CR>
Alternative
As the script path is static, you can move the variable interpolation from runtime (mapping execution) to mapping definition, and get rid of the variable:
execute 'nnoremap <Leader>v :edit' fnameescape(expand('<sfile>')) . '<CR>'

Strings in vimscript are concatenated with ., not with +. For example:
:echo "Hello"." world!"
will echo
Hello world!
If you were to type
:echo "Hello" + " world!"
vim would echo
0
This is because the + operator is only for numbers, so vim attempts to cast these strings to numbers. If you were to run
:echo "3" + "1"
vim would echo "4".
So basically, you just need to change
:map <Leader>v exec(":e " + g:vimrc_path + "<CR>")
to
:map <Leader>v exec(":e ".g:vimrc_path."<CR>")
Another problem you might have not seen is that "<CR>" evaluates to the literal text "<CR>", so it only messes up your function. If you want a literal carriage return, you would need a backslash. However, you definitely do not want to do this! Seriously, try it out and see.
You can see the issue. It looks for a file that has a literal carriage return at the end of the filename! There is a very simple fix though. Remove the "\<cr>" completely. Since :exec runs ex commands by default, the carriage return (and the colon too for that matter) are unnecessary.
Also, as a nitpick,
The parenthesis are not needed for the "exec" function, and
Use nnoremap instead to avoid recursive mappings.
Taking all of this into consideration, I would simplify it to
:nnoremap <Leader>v :exec "e ".g:vimrc_path<cr>

Related

Is it possible to map to execute normal command?

The following command appears to invoke the desired function.
:execute "normal \<Plug>VimwikiAddHeaderLevel<CR>"
However, putting it inside a mapping seems to cause problems.
:nmap <buffer> = execute "normal \<Plug>VimwikiAddHeaderLevel<CR>"
Here's the output when I type =.
E114: Missing quote: "normal \<Plug>VimwikiAddHeaderLevel
E15: Invalid expression: "normal \<Plug>VimwikiAddHeaderLevel
Is there some special syntax that would allow me to perform this mapping?
You don't enter command-line mode (for :execute) from the normal mode mapping; the : is missing.
The <Plug> and <CR> are already evaluated by the mapping; the double quotes do not protect them. The <CR> submits the (incomplete, as Vim hasn't seen the trailing ") command-line, and that causes the E114.
After escaping < as <lt>, you still need another <CR> to conclude the :execute command.
:nmap <buffer> = :execute "normal \<lt>Plug>VimwikiAddHeaderLevel\<lt>CR>"<CR>
As I've noted in your other question, you need a :for loop if you really want to work around the plugin's failure to accept a count. Though it would be possible to do all that inline in the mapping's right-hand side, separating the loop into a separate :function is highly recommended, exactly to avoid such escaping issues. Inside a function, the plugin invocation is a simple
:execute "normal \<Plug>VimwikiAddHeaderLevel"

don't stop mapping on not found

There is a important part of vim mapping:
:map <C-j>r f{^
I noticed, that when the { character is not found in the line, then the rest of the mapping is not executed.
Is there a way, how to force the mapping to continue even though the search character is not found? (In this case execute the return to the beginning of the file)
Yes, Vim aborts on an error in a mapping. You can use :silent! normal! ... to continue regardless of errors. With a sequence of :normal! commands, you can even check (e.g. whether the cursor moved) and react to it. Sketch:
:map <C-j>r :exe 'normal! maf{'<Bar>if getpos("'a") == getpos(".")<Bar>echo "no move"<Bar>endif<CR>
Note that this doesn't scale well. You're better of moving the commands into a :function.
Also, you should probably use :noremap; it makes the mapping immune to remapping and recursion.
The literal answer to this question would be:
:map <C-j>r :silent! exe "normal f{^"<CR>

How to jump to a search in a mapped :normal command?

What do you need to properly jump to a matched search result?
To reproduce, make a macro with a search in it after you've run vim -u NONE to ensure there's no vimrc interfering. You'll need to make a file with at least 2 lines and put the cursor on the line without the text TEST_TEXT.
map x :norm gg/TEST_TEXT^MIthis
My intention is that when I press x, it goes to the top of the file, looks for TEST_TEXT and then puts this at the start of the line that matches the search. The ^M is a literal newline, achieved with the CtrlQ+Enter keypress. What's happening instead is either nothing happens, or the text gets entered on the same line as when I called the macro.
If I just run the :norm gg/TEST_TEXT^MIthis command without mapping it to a key, the command executes successfully.
I had an initially longer command involving a separate file and the tcomment plugin, but I've gotten it narrowed down to this.
What is the correct sequence of keys to pull this off once I've mapped it to a key?
The problem is that the ^M concludes the :normal Ex command, so your search command is aborted instead of executed. The Ithis is then executed outside of :normal.
In fact, you don't need :normal here at all. And, it's easier and more readable to use the special key notation with mappings:
:map x gg/TEST_TEXT<CR>Ithis
If you really wanted to use :normal, you'd have to wrap this in :execute, like this:
:map x :exe "norm gg/TEST_TEXT\<lt>CR>Ithis"<CR>
Bonus tips
You should use :noremap; it makes the mapping immune to remapping and recursion.
Better restrict the mapping to normal mode, as in its current form, it won't behave as expected in visual and operator-pending mode: :nnoremap
This clobbers the last search pattern and its highlighting. Use of lower-level functions like search() is recommended instead.
There are many ways of doing this however this is my preferred method:
nnoremap x :0/TEST_TEXT/norm! Itest<esc>
Explanation:
:{range}norm! {cmd} - execute normal commands, {cmd}, on a range of lines,{range}.
! on :normal means the commands will not be remapped.
The range 0/TEST_TEXT start before the first line and then finds the first matching line.
I have a few issues with your current mapping:
You are not specifying noremap. You usually want to use noremap
It would be best to specifiy a mode like normal mode, e.g. nnoremap
It is usually best to use <cr> notation with mappings
You are using :normal when your command is already in normal mode but not using any of the ex command features, e.g. a range.
For more help see:
:h :map
:h :norm
:h range
try this mapping:
nnoremap x gg/TEST_TEXT<cr>Ithis<esc>
note that, if you map x on this operation, you lost the original x feature.

How to create an alias for ctags in vimscript

I have a function that is invoked based on a simple key mapping
function! JumpToDefinition()
let filetype=&ft
if filetype == 'coffee'
exe '<C-]>'
endif
endfunction
This works when I manually do <C-]> but when I try to "exe" it above I get a "trailing whitespace" error.
How can I invoke this in a standalone function like I have above?
Note that :execute runs the resulting expression as an Ex command,
which isn't probably what you want since there is no <C-]> Ex command. You
should be using :normal.
However, to be able to use these "special keys", instead of the characters
they represent, you have to pay attention to three things:
The correct way to represent them is with a backslash \<xxx>. Check the
help for expr-string.
Use double quotes, not single quotes
:normal accepts commands, not an expression like :execute
So, from items 1 and 2 above we know that "\<C-]>" should be used, but you
can't put this in front of :normal. Well, you can, but then it will be
executed as "quote, backslash, C, ...". The solution is to go back to using
:execute, this time to build the string with the visible "\<xxx>" in front
of :normal, which will be expanded to the actual character and executed.
:exe "norm \<C-]>"

How to call vim function from within map and ":split"

I would like to map a key using "map" from within my vimrc file like this:
map <C-I> :split ~/some/file
That command actually works fine.
My question is: how do I call a vim function (in this case, "resolve()") on that file path from within the map/split line. This doesn't work, but hopefully you get the point:
map <C-I> :split =resolve("~/some/file")
Perhaps it uses call()? I'm obviously confused about vim scripting in general. Thanks for your help!
There are two additional ways of doing this which will work outside of a mapping and are safer then using <C-r> (POSIX allows filenames with any byte but \x00, including control codes):
nnoremap <C-i> :execute "split" fnameescape(resolve("~/some/file"))<CR>
nnoremap <C-i> :split `=resolve("~/some/file")`<CR>
In second case no escaping is needed, but filename must not contain newline (it won't hurt, just will produce an error).
Another things to consider:
Use nnoremap, it will enable you to, for example, exchange meanings of ; and : without changing maps and also prevent your map from being spoiled by plugins unless they redefine <Tab> mapping (<C-i> is same as <Tab>). Forced normal mode is here because in other modes it will produce unexpected results.
Escape arguments: fnameescape(resolve("~/some/file")), it will prevent errors for filenames with spaces.
You can write <C-r> where #Austin Taylor suggested to write raw control code. I don't like having any of them inside a file because it will make diffs not viewable in a terminal.
map <C-I> :split ^R=resolve("~/some/file")<cr><cr>
If you are putting this in .vimrc, you type C-v C-r to type the ^R character.

Resources