repurpose default key in vim - vim

I heard that there is no internal function to call when typing a key combo in vim, unlike emacs. So I want to repurpose Ctrl-A to ^ (like, nnoremap < C-A> ^); however, the original function of Ctrl-A is also attractive, so I want to map it to some other keys, say Ctrl-S. How can I do this the vim way?
This question might have been asked but I searched around and did not find anything close. Thanks in advance.
PS: the stupid vim-emacs war is spamming everywhere and nowhere can i find a real solution that makes a desired feature to work in another environment.

The following should do:
nnoremap <C-A> ^
nnoremap <C-A>A <C-A>
This works because the noremap tells vim to do not apply any other mapping, thus the rhs of the second mapping above is the default <Ctrl-A>.
You will notice that the first mapping will take a delay to execute, because vim waits a while to allow you to type the second mapping. Vim-FAQ 20.16 may help if this bothers you.
But I'd advice against changing the default mapping because, as explained here, vim has many default mappings that may seems silly when you start using it. When you understand it better you will have a hard time finding another key to map it or to get used to the old/default mappings.

Related

Why can't I change a coc/neoclide key binding in Vim 8.1?

I love having a hotkey to format all of the code I'm using. I'd prefer having it on the lower case "f" button. I tried imitating the code given in the default config (the one which creates a new "Format" command) to bind it to "f", but it doesn't work.
Here's the relevant except from my .vimrc:
" Formatting selected code.
"original code commented out
"xmap <leader>f <Plug>(coc-format-selected)
"nmap <leader>f <Plug>(coc-format-selected)
"these bindings do not work
xmap <leader>f :call CocActionAsync('format')
nmap <leader>f :call CocActionAsync('format')
nnoremap <leader>f :call CocActionAsync('format')
"This does however work.... strangely:
" Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocActionAsync('format')
As a side note, what counts as "selected" for neoclide? I tried using visual selection, but that doesn't seem to work.
Here's the link to the original config:
https://github.com/neoclide/coc.nvim#example-vim-configuration
Many thanks!
So the answer is actually quite simple. Two problems were being generated:
The first one comes from emulation, which basically disabled most of the useful function keys for vim, including CTRL, the START key and others... that made debugging unnecessarily hard. The second one was me confusing the keybinding syntax.
The only thing that seemed to work was to use the existing :Format command and apply it so shift-f:
nmap F :Format<CR>
That's all. I can't imagine that this solution would've taken more than .01% brainpower of anyone experienced with the software. My initial attempts at fixing the problem failed due to the aforementioned emulation problems - I had posted the original not working code in order not to make things too confusing.
Even though the problem is solved now, there is a certain bitterness remaining. Assuming people aren't using software "properly" just because they are learning or do not have your proficiency level seems a very inadequate thing to do. I'd bet that most programmers started by learning from small examples instead of reading a 2000-page documentation first. If you don't want to help, remain silent. I always try to help with what little knowledge I have in, say, C# or TS, or Latin, Spanish or Czech for that matter. Have a good night.

Is there a way to alias vim navigation keys? [duplicate]

After attempting to use vim a number of times, I'm always been put off by its default key bindings. After some research, I decided to remap the default keys to more sane ones (at least for my tastes). I have a few questions about changing bindings that I could not find answers to through Google.
Is there a way to map a key to an action rather than another key. For example:
:noremap a h
will bind the a key to move the cursor left. Is there something along the lines of
:noremap a move-cursor-left
or
:noremap a :move-cursor-left<Enter>
so I can read my config file afterwards?
Is there a way to unbind all of the default key bindings? I've tried :mapclear but it doesn't seem to work, although it might just be clearing non-default bindings. Unmapping every key manually is not an option. If possible, I would like to export the defaults to a file so I can edit them as I need.
Is there another language I can use to write my own commands for vim other than vim-script? I haven't actually looked into this one so much though. If I can bind an external executable to a command, that would be good enough.
I haven't given up on vim although I would like to ask if there are any vim-like alternatives out there. My Google searches came up with nothing actively developed. If the above cannot be done, or reasonably hacked together, I would like to find a fully configurable, modal, command line text editor that can.
The command set of vi / Vim has 40 years of wisdom encoded into it; completely changing it is a bad idea. I can only repeat the reference to this answer from the comments; that should convince you (to accept this, or choose another editor).
Give the default commands another try. It'll take some time to get used to, but most Vim users swear by these. It's fine to make minor adjustments "for personal style": careful evolution, but not revolution. Also, some people with odd keyboard layouts (like Dvorak or Colemak) swap several keys for convenience.
The move-cursor-left is just h; aided by the excellent :help, you should be able to "read" your remappings just fine.
In general, even though it's possible to write extensions / plugins in integrated languages like Perl, Python, Ruby, etc. (based on support for them compiled into the Vim binary), you won't get around learning some key Vim internals and parts of Vimscript. If you don't feel comfortable at all about this, I'd suggest to use another editor. Unfortunately, with your requirements of full extensibility (and probably broad acceptance and platform support), there's only one serious alternative: Emacs (which cannot only be turned into everything but a kitchen sink, but also into a modal editor, too).
(i know this question is old)
you can reset the binds by just over-writing them
on vim you can use many <things> (idk the name)
e.g.
<Up> = Up-arrow-key
<C-f> = Ctrl, f
<A-Down> = Alt, Down-arrow-key
<Esc> = Escape-key
it's possible to remap the hjkl keys to wasd like this:
nmap w <Up>
nmap a <Left>
nmap s <Down>
nmap d <Right>

VIM keybinding to jump back to initial position after indenting the whole file

I have created a keybinding that should indent a whole file.
My first solution looked like this:
map <F4> gg=G
The problem is that after pressing F4, the cursor jumped to the first line of the file. So I have tried to improve my solution with the feature of markers, look like this:
map <F4> mzgg=G'z<CR>
I expected this would have resolved my problem, but the command do the same as the first. When I try to jump to the z marker manually vim told me "marker not set".
After changing the keybinding, I have or course restarted vim! I am using the GVIM 7.3 on a WIN 7 machine.
Thank you in advance for your Help!
Edit:
After trying to get my keybinding working by tipping it directly to vim commandline. I find out that the keybinding was working quite nice. I think problem is that I create a session some times ago (with mksession) and if you load a session I think vim ignores the vimrc-file. Is this assumption right?
Solution:
In these thread I find a soultion to make mksession save less options.
Another lightweight approach: set the ` mark, format the buffer and jump back to the mark afterwards.
:nnoremap <key> m`gg=G``
I would recommend the use of CTRLo and CTRLi which allow to go respectively backward and forward in the jump list. See :help jumps.
The following mapping
map <F4> gg=G2<C-o>
works. (It jumps back two times in the jump list)
But generally, the jump list is a great way to navigate in a file, this is likely the shortcuts that use the most in my daily use. It is also works if you jump to a tag to go back to your original location.
You might also want to use nnoremap rather than map, this way it will only work in normal mode, and you could potentially reuse F4 in combination in another key binding without having recursive mappings.
so
nnoremap <F4> gg=G2<C-o>

Changing default key bindings in vim

After attempting to use vim a number of times, I'm always been put off by its default key bindings. After some research, I decided to remap the default keys to more sane ones (at least for my tastes). I have a few questions about changing bindings that I could not find answers to through Google.
Is there a way to map a key to an action rather than another key. For example:
:noremap a h
will bind the a key to move the cursor left. Is there something along the lines of
:noremap a move-cursor-left
or
:noremap a :move-cursor-left<Enter>
so I can read my config file afterwards?
Is there a way to unbind all of the default key bindings? I've tried :mapclear but it doesn't seem to work, although it might just be clearing non-default bindings. Unmapping every key manually is not an option. If possible, I would like to export the defaults to a file so I can edit them as I need.
Is there another language I can use to write my own commands for vim other than vim-script? I haven't actually looked into this one so much though. If I can bind an external executable to a command, that would be good enough.
I haven't given up on vim although I would like to ask if there are any vim-like alternatives out there. My Google searches came up with nothing actively developed. If the above cannot be done, or reasonably hacked together, I would like to find a fully configurable, modal, command line text editor that can.
The command set of vi / Vim has 40 years of wisdom encoded into it; completely changing it is a bad idea. I can only repeat the reference to this answer from the comments; that should convince you (to accept this, or choose another editor).
Give the default commands another try. It'll take some time to get used to, but most Vim users swear by these. It's fine to make minor adjustments "for personal style": careful evolution, but not revolution. Also, some people with odd keyboard layouts (like Dvorak or Colemak) swap several keys for convenience.
The move-cursor-left is just h; aided by the excellent :help, you should be able to "read" your remappings just fine.
In general, even though it's possible to write extensions / plugins in integrated languages like Perl, Python, Ruby, etc. (based on support for them compiled into the Vim binary), you won't get around learning some key Vim internals and parts of Vimscript. If you don't feel comfortable at all about this, I'd suggest to use another editor. Unfortunately, with your requirements of full extensibility (and probably broad acceptance and platform support), there's only one serious alternative: Emacs (which cannot only be turned into everything but a kitchen sink, but also into a modal editor, too).
(i know this question is old)
you can reset the binds by just over-writing them
on vim you can use many <things> (idk the name)
e.g.
<Up> = Up-arrow-key
<C-f> = Ctrl, f
<A-Down> = Alt, Down-arrow-key
<Esc> = Escape-key
it's possible to remap the hjkl keys to wasd like this:
nmap w <Up>
nmap a <Left>
nmap s <Down>
nmap d <Right>

Making inserting a single character in Vim an atomic operation

I've long used this very useful shortcut in vim:
nmap <space> i <esc>r
this means that if I press spacef, for example, it will insert a single character f at the given position.
unfortunately, however, this is not atomic, ie, if I press spacef and then navigate somewhere else, then press ., I get the equivalent of rf, not spacef.
all this makes sense, but here's the question: is there a way of making this atomic, so that . will repeat the 'insert character' operation, and so that undo etc all treat it as one operation, too?
Awesome! Michael's answer pointed me to the plugin I needed to finish my plugin, which can now do what you want - I had been trying to figure out how to do this for ages!
1) Install Tim Pope's plugin
2) Install my plugin
3) Add a mapping to your .vimrc:
nnoremap <space> :<C-U>call InsertChar#insert(v:count1)<CR>
Does this work for you?
noremap <silent> <space> :exe "normal i".nr2char(getchar())<CR>
You might want to have a look at this plugin script. It may be possible to configure your map so it can be supported. Read the supporting docs
http://www.vim.org/scripts/script.php?script_id=2136
Sorry I can't provide a specific answer to your problem but I will note that I tend to use the . key when I have to reproduce quite a lot of commands (e.g. I want to insert f 5 or more times).
If that is the case here, I don't think the saving of using your macro is worth it. You save one keystroke by using your macro rather than ifesc and that operation is atomic so you could then . to your heart's content.
I would just use the non-macro version if I know I want to repeat it a lot.
P.S. You know I'm starting to like the <kbd> tag quite a bit :-)

Resources