I'm trying to remap q and q!. Here's what I'm trying:
cnoremap q :call TabCloseLeft('q')
cnoremap q! :call TabCloseLeft('q!')
That properly remaps :q but doesn't capture :q!. I've read various help sections, but I'm obviously overlooking something.
I see some problems:
You are using a cnoremap dangerously e.g. /q
You are using trying to override vim's native :quit function
Looks like you are trying to force vim into a tab behavior
cnoremap
Do not use cnoremap to try and create commands, use :command. If you want to override a command then use a clever cabbrev expression or plugin. See vim change :x function to delete buffer instead of save & quit
Native quit
There are many ways for quiting a buffer in vim. Some do slightly different things. As vimmers learn more commands they integrate them into their workflow. e.g. <c-w>c to close a split/window and ZZ to update and quite the current buffer. If you go down this path you will be overriding many command or you will be disregarding useful commands.
Tabs
Learn to use buffers effectively. Using tabs in Vim is great however they are not the only way and will certainly cause you pain down the road. IMHO it is best to curtail these behaviors and encourage better habits.
Related
Hi I use vimgrep to find text in the project.
However its a little slow because i have to write this each time:
vimgrep /text/ **/*.* | cwindow
Is there any way to map it in for example C-shift-F and show the text box.
After that when I hit enter it will execute the vimgrep commands.
That will save much time
Thank you
Vim is highly customizable (which is one big reason for its popularity), so there are several possibilities to provide a shortcut:
incomplete mapping
A mapping does not necessarily contain a complete command. You can just prepare the command-line and leave the cursor in a certain spot, so that you can fill out the rest and trigger the (Ex) command with Enter. For example:
:nnoremap <Leader>f :vimgrep // **/*.* <Bar> cwindow<Home><Right><Right><Right><Right><Right><Right><Right><Right><Right>
The trailing keys position the cursor in between the //, so you can fill in text, and then execute the command.
Note: Your suggested <C-S-f> mapping would be the same as <C-f> (an unfortunate limitation in the current implementation), and therefore override a useful built-in command. I've instead used <Leader>f (where the default for that key is \; cp. :help <Leader>).
custom command
This is already nice, but assuming we'll never have a need to edit the other parts of the command, we can shorten this to a custom command, e.g. :FindText:
command! -nargs=1 FindText vimgrep /<args>/ **/*.* | cwindow
With this, you can search via :FindText text. If you still prefer a mapping, this would become
:nnoremap <Leader>f :FindText<Space>
Other benefits of custom commands are that these are easier to recall from the command-line history (as they are different from other :vimgrep commands you might use), and you're building a library of higher-level editing commands, which over time can be reused to build even more higher-level commands, which makes your editing more efficient.
I started using Vim recently, just installed NERDTree (a plugin to navigate files).
The command to access that plugin is :NERDTree so I though it's a good idea to start learning mappings by assigning one to that command.
So I added to my .vimrc file the following line: map :nt :NERDTree - but when I type :nt in a vim file (even after restarting) I receive the following error message: not an editor command: nt
I also tried to add the mapping directly while editing a file by typing :map :nt :NERDTree but it returned the same error when I tried to use the command.
I checked that answer:What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in vim?, so it seems to me that :map (opposed to noremap etc.) is the good command for that.
The plugin works fine when typing the original command.
What am I doing wrong? (sorry for the noob question)
:NERDTree is a command, not a mapping, so there's no reason for creating a recursive mapping, here.
:map is too overreaching. You should use :<mode>map (for recursive mappings) or :<mode>noremap (for nn-recursive mappings).
You are missing a <CR> at the end of your mapping to tell Vim to actually execute the :NERDTree command.
In this specific case, the right mapping would be:
nnoremap :tn :NERDTree<CR>
But mapping something to :<anything> is not a good idea because it will introduce a timeout whenever you try to execute an Ex command. This means that you need to find another combo. Why not <Space>n?
nnoremap <Space>n :NERDTree<CR>
With the mapping that you have, it will be require multiple keystroke. Will it be okay for you to use a single key like F2?
nnoremap <F2> :NERDTreeToggle<CR>
This will toggle open/close NERDTree upon pressing F2 and save you some key stroke.
Here
you can figure out, how vim's mapping work and look like ;). Don't forget to source your new .vimrc before using.
I am currently trying out Clion from intellij but I am not a big fan of keybindings but it offers vim support. For example Clion has a keybinding ctrl+shift+n to open a fuzzy search.
Is it possible to bind a keybinding to a custom command in vim?
Something like
command :fuzzy <C-N>
IdeaVim is not Vim. There is no reason whatsoever to expect anything to work in IdeaVim like in Vim or vice-versa so… do you want that mapping to work in Vim or in IdeaVim? If your question is about Vim, your CLion explanation and tags are totally irrelevant.
In Vim, you would put this line in ~/.vimrc:
nnoremap <key> :Command<CR>
See :help key-notation for <key> and note that Vim doesn't make a difference between <C-N> and <C-n>.
Now, Vim has no "fuzzy" capability on its own, so you will need a third party plugin for that.
I don't see how you'll enjoy Vim without a preference for (fast and efficient) keybindings, but this is certainly possible, if somewhat odd. Usually, one defines keybindings for custom commands (so the other way around), in order to speed up frequently used commands.
To do the opposite, you have to consider the modes: (Custom) commands take Ex commands, whereas keybindings usually are in normal mode. Fortunately, there's the built-in :normal command to bridge between the two of them. And to use the special keycodes (like <C-N>), you need :execute:
:command FuzzySearch execute "normal \<C-N>"
Note that this probably only works in Vim itself, not in emulations (like in Eclipse or IntelliJ IDEA), as those typically only offer a subset of the full Vim functionality.
Many of the navigation and other commands I use frequently, I try to make key maps for. For instance,
nnoremap <silent> <leader>f :find<Space>
However, where I expect a prompt starting with :find, I get no text, until I start typing, or switch applications, it is blank (or sometimes .....).
Is this normal? Is there a way around this?
By the way, I am on kubuntu, and got my GVim via apt-get install vim-gtk.
You should remove <silent> as its purpose is precisely to silence the ex commands in the right hand side of your mapping.
<silent> is obviously counterproductive in this case but it can be useful when your mapping does a lot of stuff with possibly annoying echos.
Tried using the following in my .vimrc, but somehow it isn't working. Also, is there any down sides to using "Q" also along side the usual q to quit the editor. Apparently, I find stumbling on this issue a lot, but don't see this tweak is most .vimrcs.
noremap Q :quit<CR>
If you want to quit Vim with :Q, a simple mapping won't do, because you then won't be able to type Q in the command-line (which is the mode you would have to map, using :cnoremap).
You can either just define an uppercase command:
:command! -bar -bang Q quit<bang>
or use the technique described in aliasing a command in vim, which also works for lower-case. Or have a look at cmdalias.vim - Create aliases for Vim commands.
If you want to quit vim with :Q just add this to your vimrc
:command Q q