how can I map 'esc' key to 'jk' or 'jj' in spacevim? What is the configuration file for spacevim. Is there any legendary spacevim config you can share?
As #jubnzv mentioned, you need to find a way to add inoremap jj <Esc> in your config. In spacevim, you can do it with the Bootstrap functions.
SpaceVim provides two kinds of bootstrap functions for custom configurations and key bindings, namely bootstrap_before and bootstrap_after.
First, enable the bootstrap in your config, the default path is ~/.SpaceVim.d/init.toml
[options]
bootstrap_before = 'myspacevim#before'
bootstrap_after = 'myspacevim#after'
Next, create file ~/.SpaceVim.d/autoload/myspacevim.vim with the following contents
function! myspacevim#before() abort
endfunction
function! myspacevim#after() abort
inoremap jj <Esc>
endfunction
More detail in the official document here: https://spacevim.org/documentation/#concepts
Related
There might be a simple solution for this. I couldn't find any solution to it (I might be searching with wrong context)
Here is my requirement.
I wrote the below key mapping in vimrc. It should print the line "Hello user_name." n times, where n and user_name are the user input once the key is pressed.
autocmd FileType ruby nnoremap <expr> <C-h> :call FuncPrnt(<-syntax to pass input from user->)
function! FuncPrnt(count, uname)
let c=a:count
let i=0
while i<c
call append(line("."), "Hello ".a:uname.".")
let i+=1
endwhile
endfunction
On Pressing the key user enters 3 and 'Ironman'. The output would be like
Hello Ironman.
Hello Ironman.
Hello Ironman.
Thanks in advance
A simpler approach is to just use a non-<expr> mapping to prepare the Ex command without executing it:
nnoremap <C-h> :call FuncPrnt(,"")<left><left><left><left>
If you associate that with a filetype (such as "ruby" in your case), make sure you create a local mapping using <buffer>. Otherwise the mapping will be global and will work on every buffer and not only those with Ruby source files.
If you use an autocmd in your vimrc, make sure you wrap it in an augroup to prevent getting duplicated commands if you reload your vimrc.
Alternatively, you can add filetype mappings for Ruby in a file ~/.vim/ftplugin/ruby.vim which gets automatically loaded by Vim whenever a file of type Ruby is loaded. (That way you don't need to use explicit autocmds, Vim will take care of those details on your behalf.)
Got the answer. Just in case someone else is searching for similar solution
autocmd FileType ruby nnoremap <expr> <C-h> input("", ":call FuncPrnt(,\"\")<left><left><left><left>")
will print the command and waits for the user to edit. Once the user edits and enters it is executed
Thinking outside of the box a little bit (questioning the premise), perhaps the best in this case is to define a user-defined command instead of a mapping.
You can define one here with:
command! -buffer -bar -count=1 -nargs=1 FuncPrnt
\ call FuncPrnt(<count>, <q-args>)
That way you can use it with:
:3FuncPrnt Ironman
If you omit the count, 1 will be used. (You can pick a different default as the argument to -count=N.)
You can use Tab completion for FuncPrnt, so perhaps :3Fu<Tab> or even :3F<Tab> might be enough to complete the command.
This might end up being quicker or more convenient to type than <C-H>3<right><right>, since it doesn't involve moving your hand to the arrow keys.
I'm using a plugin that performs multiple *noremap operations in its initialization. After I added the following mapping to YCM/UltiSnips:
inoremap <expr> <CR> pumvisible() ? "<C-R>=<SID>ExpandSnippetOrReturn()<CR>" : "\<CR>"
I've broken the other plugin's ability to see the <Enter> key, or rather I overrode/destroyed the original inoremap. I want to reenable that functionality, but without disabling ability to pick snippets from the auto-complete menu using Enter key. This keymap is effectively saying "if the menu is visible, select the option, otherwise simulate <Enter> key". What I need it to say instead is "if the menu is visible, select the option, otherwise perform whatever command <Enter> key is already mapped to". So basically, at the time of declaration of this inoremap, I need to expand existing inoremap for this key into the else.
I've looked at <C-R>=feedkeys('<CR>')<CR> and <C-R>=mapcheck('\<CR>', 'i')<CR> but couldn't get either to work. Can someone help me out?
Finally figured it out... this was way more vim troubleshooting than I needed for one day.
function! LoadPumvisibleEnter()
let smartmap = maparg("<Enter>", "i")
execute printf('inoremap <script> <expr> <CR> pumvisible() ? "<C-R>=<SID>ExpandSnippetOrReturn()<CR>" : %s', smartmap)
endfunction
au VimEnter * :execute LoadPumvisibleEnter()
throwing it in a function allowed me to stay sane with escapes and which mode I was in
autocommand is needed because vim apparently runs vimrc BEFORE loading any plugins (Does Vim load plugins after loading vimrc?), so without it the map will not yet be set
Here is the code that is a practice for writing vim plugin. I write it as per the vim docs: :help usr_41.txt section 41.11 write a plugin.
let s:save_cpo = &cpo
set cpo&vim
if exists("g:loaded_echoplugin")
finish
endif
function s:EchoWord()
echo expand('<cword>')
endfunction
if !exists(":EchoWord")
command -nargs=0 EchoWord :call s:EchoWord()
endif
if !hasmapto('<Plug>EchoWord')
map <F8> <Plug>EchoWord
endif
noremap <script> <Plug>EchoWord <SID>EchoWord
noremap <SID>EchoWord :call <SID>EchoWord()<CR>
let g:loaded_echoplugin = 1
let &cpo = s:save_cpo
unlet s:save_cpo
The code is for displaying the word under the cursor when clicking <F8>. Here is the mapping sequence: <F8> -> <Plug>EchoWord -> <SID>EchoWord -> :call <SID>Echoword() and it works.
Here, however, I have 2 questions:
1. I have used the noremap here, why it is still able to remap or recursive mapping?
2. If I change the mapping from map <F8> <Plug>EchoWord to noremap <F8> <Plug>EchoWord, it will do NOT work.
Could anybody please help to figure it out? thanks!
<Plug>Foo is a mapping. Whether it is itself recursive or not doesn't matter.
When you do a recursive mapping, Vim uses whatever is the current meaning of the commands in the right hand side:
map b B
map <key> db " works like dB
When you do a non-recursive mapping, Vim uses the original meaning of the commands in the right hand side:
map b B
noremap <key> db " works like db
<Plug>Foo doesn't mean anything by default so there's no point mapping it non-recursively.
You want recursiveness, here, so you are supposed to use map, imap, nmap, etc.
I have read the vim docs for the related command carefully, and I have found the root cause. I add it here just for anybody who may concern.
Type the command :help :map-<script> and here is the reason:
Note: ":map <script>" and ":noremap <script>" do the same thing. The
"<script>" overrules the command name. Using ":noremap <script>" is
preferred, because it's clearer that remapping is (mostly) disabled.
I know you can add custom keymappings to vim with imap in the .vimrc file. My question is, if one is to create additional custom operators, motions, commands and map them to keys, what is the best way to add them in so they don't conflict with existing bindings?
An analogy, in emacs, a lot of custom key commands are added via the C-c sequence. To be more technical, are there unbound keys in vim that allow for user customization? In other words, is there a free keyspace available to users?
:imapEnter will show a list of your insert mode mappings.
My question is, if one is to create additional custom operators,
motions, commands and map them to keys, what is the best way to add
them in so they don't conflict with existing bindings?
In my opinion the best way is creating custom command, they start with uppercase, native ones with lowercase.
Additionally, you can use a <leader> I use , some use \.
e.g. :let mapleader = ","
then, you can use the leader to combine it with other keys, like ,p to call a command, native or custom, see below:
:map <leader>p :MyCustomCommand<CR>
A custom motion example. Delete the third word after the cursor.
nnoremap <leader>x :normal 2wdw<CR>
Several commands can be used to create new, remove and list the mappings, here is a list of working modes, from :help map-overview
Normal Visual+Select Operator-pending ~
:map :noremap :unmap :mapclear yes yes yes
:nmap :nnoremap :nunmap :nmapclear yes - -
:vmap :vnoremap :vunmap :vmapclear - yes -
:omap :onoremap :ounmap :omapclear - - yes
further info
:help map
Here's an example function, converted to a custom command
function! MoveLastLines(f)
exe '$-9,$w ' . a:f "write last ten lines to the passed filename
$-9,$d "delete last ten lines
endfunction
command! -nargs=1 -range MoveTo :call MoveLastLines(<f-args>)
Now, both lines below are equivalent
:call MoveLastLines('newFile.txt')
:MoveTo newFile.txt
I'd like to map a key to toggle between foldmethod=indent and no folding. How can I do that?
I'd say zi (toggle foldenable) does the job.
No mapping required. (see also :he folding)
(You could also look at zM and zR)
Since you want to map it to a single key, proceed as follows:
:nnoremap <F10> zi
To force the foldmode to indent each time (not really recommended for me), you'd need a function:
Add the function to your vimrc[2]:
function ForceFoldmethodIndent()
if &foldenable
se foldmethod=indent
endif
endfunction
nnoremap <F10> :normal zi^M|call ForceFoldmethodIndent()^M
inoremap <F10> ^O:normal zi^M|call ForceFoldmethodIndent()^M
Let me know if that works for you. I appreciate if you accept this answer if it does :)
Cheers
[1] with behave mswin
[2] To enter the special keys (e.g. ^O) in commandline or insertmode use e.g.
Ctrl-VCtrl-O or
on windows[1] Ctrl-QCtrl-O