I want to create a shortcut ctrl+c to toggle comment for .vhd files, so thing should be like this:
when I press Ctrl+c, in vim normal mode, it should add "-- " at the begining of the actual line.
I tried to add "nmap < C-c > <--space> " in my vimrc file,but it didn't work.
You probably want to use a plugin like vim-commentary
That said, you can achieve what you want with the following mapping:
nmap <C-c> I-- <esc>
Related
I'd like to be able to use the same shortcut to jump to a file in NERDTree and close NERDTree. I have no experience with VimL and could use some help.
Add the following to you vimrc
set autochdir
noremap <F2> :NERDTreeTabsToggle<cr>
The first command will automatically change the current directory to be the same the same as the file you are editing.
The second command will then set F2 to toggle toggles NERDTree on/off for all tabs. you can use any key i prefer F2.
Just say you want to do this with <leader>n:
" This in your ~/.vimrc
nnoremap <leader>n :NERDTreeFind<CR>
" This in ~/.vim/ftplugin/nerdtree.vim
nnoremap <buffer> <leader>n :NERDTreeClose<CR>
Substitute whatever you want for <leader>n.
As stated in the question, my vim starts out in delete mode. If I open a file and hit j to start navigating, that'll delete the first line.
I've isolated the problem down to this line in my .vimrc:
nnoremap <silent> <esc> :noh<return><esc>
I don't understand why this would even trigger delete mode. On top of that, I believe I added <silent> to instruct vim to make this binding without executing it, which doesn't seem to be the case.
What's the explanation for why this is happening?
(side note, this mapping is to tell vim to clear search highlights when I hit esc)
If you run this command in terminal:
$ vim file.txt -c 'nnoremap <silent> <esc> :noh'
It'll show this at the bottom:
:noh[>1;3201;0c
vim enters change-mode somehow.
You can change <esc> to <F5>(or other keys).
If you use gvim, there's no problem.
It's caused by terminal special key escaping.
Lets say I am copying a file with the vim command mode and my cursor is at the end of the line.
:!cp path/to/original/file path/to/new/file
Is there a way I can jump back a word like I can in the shell by typing Esc b?
You cannot use "Esc b" because, obviously, that would discard the command you where typing. However you can bind some keys to move around.
The question as already be answered here : Navigating in Vim's Command Mode
The easy way is just to add :
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>
In your .vimrc
For entering and editing complex commands, you may like working directly in the command line window which is accessed with the normal mode command q:. See :h 20.5 and :h q:. Or if you are already in command mode, you can access the command line window with C-f.
For example, in normal mode type q: to get into the command line window. (or type C-f from command line mode.
You can move around previous commands using standard motions and you can edit as usual.
When you want to execute a command that you just edited, press enter in normal mode in
this command line window. The line your cursor is on will be executed as a command in
the window you were in before you opened the command line window.
Another option to consider is to edit/yank the command from another buffer. You can do this by yanking the desired text and pasting it in command mode by typing C-R n, where n is the register you yanked to.
BTW: I like the mappings that #rks provided. But if you don't have these mappings, you can use the out of the box commands. Look up :h c_<S-Left> and :h c_<S-Right> and :h 20.1.
A nice vim feature is ctrl-f. Typing ^f (or whatever key is specified in the cedit option, with ctrl-f being the default) from command line mode has the same effect as typing q: from normal mode; it pulls your entire command history into a window and lets you edit it as a buffer. Try :help cmdwin for more details.
In vim's command mode, I just use ctrl-left and ctrl-right arrows. The same works in bash - I wasn't aware of the esc-b method there.
No editing of the .vimrc file is required for this on my Ubuntu and Debian systems, but YMMV on others. It's presumably based on the standard configuration that's packaged for the OS
I've been making a transition to GVim lately, because I find it to be more aesthetically pleasing and a little bit faster then vim in the terminal. I have this really bad habit that I'm trying to break. When I used vim from the command line my work flow was like this:
vim filename.txt
# make some edits
ZZ
# do other stuff
vim otherfile.txt
# make some edits
ZZ
Now using GVim, I end up closing the editor far too frequently. I'm wondering if there is a way to force just GVim to either prompt me or open an empty buffer when I do a :wq or ZZ. Any ideas?
EDIT: I know how to remap keys, but I'm wondering if there is a way to force GVim to have a different behavior then when vim is called from the command line.
Call a function on ZZ and if there is only one tab and window left, prompt whether to close or not (default is to close). See :help confirm().
nnoremap ZZ :call QuitPrompt()<cr>
fun! QuitPrompt()
if has("gui_running") && tabpagenr("$") == 1 && winnr("$") == 1
let choice = confirm("Close?", "&yes\n&no", 1)
if choice == 1 | wq | endif
else | wq | endif
endfun
Putting the following in your vimrc could be used to completely disable the ZZ shortcut altogether:
nnoremap ZZ <Nop>
Or you could remap them to the standard behaviour of :q :
if has("gui_running")
nnoremap ZZ :q
nnoremap :wq :q
endif
Add this in your .vimrc:
set confirm
Gvim and vim handle quit a little differently. Option 1 works as you would expect in both. Option 2 works nice in vim, but in gvim you can get around it (in part) by pressing the "X" to close in X Windows. It will still confirm if the file is unedited, but it will just quit everything when everything is saved. Maybe someone else knows how to deal with that issue.
Option 1: Confirm quit only when a file is unsaved
Add this in your .vimrc:
set confirm
Option 2: Confirm quit all the time (when you do ZZ or q)
Use this plugin
Plugin 'vim-scripts/confirm-quit'
Or hand code it, following along with this to see what you need to do (BTW -- this plugin was born from this SE Question)
I input :map! <C-q> :q <CR> in command line mode, then return to normal mode, and press ctrl-q, but vim does not quit. Why?
As Johnsyweb suggested, Ctrl-Q does not reach Vim in the first place. This holds for the popular Ctrl-S as well. Both can be fixed by adding
silent !stty -ixon > /dev/null 2>/dev/null
to your .vimrc as it forces these control sequences to reach the application.
Why anyone would want to add yet another way of quitting Vim to the plethora already available is beyond me. However...
On Fedora 15, Ctrl-Q is likely being captured by your terminal as XON (resume) and therefore not reaching Vim to quit the application.
You can check if there's an error in your mapping or a clash like so:
:verbose map! <C-Q>
:map! is for specifing mappings in insert and command-line modes.
normal-mode mappings are (usually) specified like so:
:nmap <C-Q> :q<CR>