Vim: remap key to toggle line numbering - vim

I added:
set number
nnoremap <F2> :set nonumber!
to my vimrc file. Basically what it's supposed to do is let me press F2 to toggle line numbering but it's not working. What have I done wrong?

In your .vimrc, add this:
set number
nnoremap <F2> :set nonumber!<CR>
Then pressing F2 will toggle line numbering.

This is what I use (with a different key binding):
nmap <f2> :set number! number?<cr>
The "number!" toggles the setting and "number?" reports the state.

nmap <silent> <F11> :exec &nu==&rnu? "se nu!" : "se rnu!"<CR>
In new vim you can set both relative number and number at once, this way:
set nu rnu

This is one method:
map <silent> <F2> :if &number <Bar>
\set nonumber <Bar>
\else <Bar>
\set number <Bar>
\endif<cr>
(this one is nice 'cause I usually put foldcolumn in there as well)
This is another:
map <silent> <F2> :set invnumber<cr>
(direct method)

I use this to toggle between relativenumber ( with current absolute line number) and no line numbering
nnoremap <silent> <leader>l :set relativenumber! <bar> set nu!<CR>

Related

Specific key mapping in Vim

How can I configure Vim to set
"require 'pry'; binding.pry"
in Ruby and
"debugger;"
in JavaScript when pressing F2 via key mapping?
You can set this in your .vimrc as follows:
autocmd FileType ruby map <F2> orequire 'pry'; binding.pry<ESC>
autocmd FileType javascript map <F2> odebugger;<ESC>
When the F2 key is pressed in a *.rb file, "require pry" will be set and "debugger" is set in a *.js file.
The other answer is correct, but not completely correct. You should use the noremap variant of map (see :h noremap), and the proper noremap for whatever mode your are in. If that's insert mode, then it's inoremap <F2> require..., or nnoremap for normal mode, etc.
You can also put those mappings into their own file instead of your vimrc so that you don't need to use autocommands (see :h ftplugin). And (thanks to the comments for reminding me) use <buffer> mappings so they only apply to the file you set them on (see :h <buffer>). In all, this is a good setup for you:
In ~/vim/after/ftplugin/ruby.vim, put the line:
inoremap <buffer> <F2> require 'pry'; binding.pry
and in ~/vim/after/ftplugin/javascript.vim, put the line:
inoremap <buffer> <F2> defbugger;
On windows, the vim directory is instead the vimfiles directory. If you want those mappings in normal mode instead of insert mode, you need to put i or O or another character like that at the front to go into insert mode and put <Esc> on the end to exit insert mode.

Vim: Shortcut to toggle between NERDTreeCWD and NERDTreeClose

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.

How to make 'intelligent' key mappings in Vim?

At present I have the following mappings in my Vimrc:
" Quick Buffer switch mappings {{{
" The idea is to press <leader> and then the number from normal mode to switch
" e.g. `,2` would switch to the second buffer (listed at the top of the
" airline strip
nnoremap <Leader>1 :1b<CR>
nnoremap <Leader>2 :2b<CR>
nnoremap <Leader>3 :3b<CR>
nnoremap <Leader>4 :4b<CR>
nnoremap <Leader>5 :5b<CR>
nnoremap <Leader>6 :6b<CR>
nnoremap <Leader>7 :7b<CR>
nnoremap <Leader>8 :8b<CR>
nnoremap <Leader>9 :9b<CR>
nnoremap <Leader>10 :10b<CR>
nnoremap <Leader>11 :11b<CR>
nnoremap <Leader>12 :12b<CR>
nnoremap <Leader>13 :13b<CR>
nnoremap <Leader>14 :14b<CR>
nnoremap <Leader>15 :15b<CR>
nnoremap <Leader>16 :16b<CR>
" Quick Buffer wipe/delete keys. Press <Leader> and then d and buffer number
" e.g. `,d2` would wipe buffer 2
nnoremap <Leader>d1 :Bdelete 1<CR>
nnoremap <Leader>d2 :Bdelete 2<CR>
nnoremap <Leader>d3 :Bdelete 3<CR>
nnoremap <Leader>d4 :Bdelete 4<CR>
nnoremap <Leader>d5 :Bdelete 5<CR>
nnoremap <Leader>d6 :Bdelete 6<CR>
nnoremap <Leader>d7 :Bdelete 7<CR>
nnoremap <Leader>d8 :Bdelete 8<CR>
nnoremap <Leader>d9 :Bdelete 9<CR>
nnoremap <Leader>d10 :Bdelete 10<CR>
nnoremap <Leader>d11 :Bdelete 11<CR>
nnoremap <Leader>d12 :Bdelete 12<CR>
nnoremap <Leader>d13 :Bdelete 13<CR>
nnoremap <Leader>d14 :Bdelete 14<CR>
nnoremap <Leader>d15 :Bdelete 15<CR>
nnoremap <Leader>d16 :Bdelete 16<CR>
" }}}
They work great but I can't help thinking this should be smarter/DRYer in the vimrc. What about if I open a buffer with number 17 for example.
Is there a way of intelligently mapping these so that a user could enter and then any buffer number to have the buffer open?
You can use meta-programming with :execute to automate the creation of those mappings:
for i in range(1, 99)
execute printf('nnoremap <Leader>%d :%db<CR>', i, i)
endfor
It is also possible to define a single mapping (with just a prefix), that then queries the number via getchar(). The challenge here is to determine when to end this, something that you get for free (due to 'timeout') with the separate mappings. That's why I would prefer the first solution here.
<c-6> switches to the previous buffer. However you can also provide a count which will be used to switch to that buffer. E.g. 6<c-6> is equivalent to :b 6.
I still can't help but think these buffer commands are a bit awkward because have to keep buffer numbers and files straight in your head. I think using some of :b native features could be of some help to you:
:b command can take a partial filenames. e.g. :b foo
:b can use globs so you can add some fuzziness. e.g. :b foo*bar.c
<tab> to complete the filenames
<c-d> for listing out the buffer names
split with the :sb command which takes all the same arguments as :b
I find :bdelete a bit dangerous they way you have it. I would suggest you just switch to a buffer and then do :bd to delete the current buffer. However :bd can take partial filenames and globs as well just like :b.
I have also seen ~/.vimrc files where people use a mapping to call :ls and then start the prompt with :b. Think of more of a menu based approach.
nnoremap <leader>b :ls<cr>:b<space>
For more help see:
:h ctrl-6
:h :b
:h :sb
:h :bd
:h :ls
Here is a slightly different strategy that only uses a single mapping:
:nnoremap <silent> <key> :<C-u>try \| execute "b" . v:count \| catch \| endtry<CR>
Now you can do 3<key> to go to buffer number 3. I'll leave it up to you to find the right <key>.

vim removes indentation from the current line

Every time I enter the Insert mode in vim using A and press Enter to go to the next line, vim automatically removes all indentation from the current line. For instance, ideally, it should be like this -
14 echo $sample;
15 $ai = system("python ai.py " + $sample, $retVal);
If I press A on line 15 and then <CR>, this is what my vim looks like -
14 echo $sample;
15 $ai = system("python ai.py " + $sample, $retVal);
16
This is my .vimrc -
"General
set nu " Set line numbers
set hlsearch " Set search highlight
set shiftwidth=2
set tabstop=2
"Pathogen
execute pathogen#infect()
call pathogen#helptags()
syntax on
filetype plugin indent on
"ConqueTerm
command Cterm split | ConqueTerm bash
nnoremap <silent> sh :Cterm<CR>
"NerdTree -
"Fs open Nerdtree on the same tab. Nfs opens NerdTree in new tab.
command Fs NERDTree
command Nfs tabedit | NERDTree
nnoremap <silent> fs :Fs<CR>
"TagBar
nnoremap <silent> tt :TagbarToggle<CR>
"Omni Completion
filetype plugin on
filetype indent on
inoremap <C-Space> <C-X><C-o>
"Editor
command Ide NERDTree | TagbarToggle
"Move between split windows
nmap <silent> <A-Up> :wincmd k<CR>
nmap <silent> <A-Down> :wincmd j<CR>
nmap <silent> <A-Left> :wincmd h<CR>
nmap <silent> <A-Right> :wincmd l<CR>
"Drag words like in Windows
nnoremap <C-Left> b
vnoremap <C-S-Left> b
nnoremap <C-S-Left> gh<C-O>b
inoremap <C-S-Left> <C-\><C-O>gh<C-O>b
nnoremap <C-Right> w
vnoremap <C-S-Right> w
nnoremap <C-S-Right> gh<C-O>w
inoremap <C-S-Right> <C-\><C-O>gh<C-O>wA
" Start Syntastic in passive mode
let g:syntastic_mode_map = { 'mode': 'passive' }
nnoremap <silent> ch :SyntasticCheck<CR>
Any idea what's going on?
You probably want to set autoindent in your vimrc file:
:set ai
You can also verify if autoindent was previously set (for example through the filetype plugins) by checking its value with
:set ai?

Plugins breaking vim mappings?

I just installed DelimitMate and CloseTag plugins for MacVim and they seem to have broken these two mapping in my .vimrc:
"F7 WordProcessorOn
:map <leader>w :set linebreak <CR> :set display+=lastline <CR> :set wrap <CR> :setlocal spell spelllang=en_gb <CR>
"F8 WordProcessorOff
:map <leader>c :set nowrap <CR> :set nospell <CR>
Does anyone have any ideas as to what the problem could be? Thanks.
You can find out what is 'hijacking' the mappings by doing
:verbose map <leader>c
:verbose map <F8>
etc.
But likely your problem is with submappings firing: use
:noremap <leader>c .......
to prevent remapping
PS: consider mapping for specific modes (nnoremap over noremap); Being explicit avoids funny interfering mappings a lot of the time

Resources