19

My question comes from this two commands I added on my _vimrc file:

nnoremap <C-[> :set columns+=1<CR>
nnoremap <C-]> :set lines+=1<CR>

As is known, it will resize your gvim (I am using gvim on windows) with one more column when you hit Ctrl-[, and one more line when you hit Ctrl-]. That is really cool since you can achieve that resizement as though you are using your mouse.

I wonder if I can map those two command with only one key to make it more comfortable.

nobe4
  • 16,033
  • 4
  • 48
  • 81
Alex
  • 1,205
  • 2
  • 10
  • 12
  • Beware that <C-[> is the same as <ESC>. It's a really bad idea to mess with that. You might want to choose a different key combination. – lcd047 Jul 08 '15 at 11:20
  • @lcd047 oh yeah that is right. thanks for that – Alex Jul 08 '15 at 11:26

3 Answers3

17

You can do this with \| on unix in the .vimrc file, or with <bar> on windows in the _vimrc file.

e.g.

" unix
nnoremap <C-[> :set columns+=1 \| lines+=1<CR> 
" windows
nnoremap <C-[> :set columns+=1 <bar> lines+=1<CR> 

See the documentation : :help :bar

ref: https://stackoverflow.com/a/3249303/2558252

ericbn
  • 235
  • 2
  • 8
nobe4
  • 16,033
  • 4
  • 48
  • 81
  • 1
    the | is good, but that doesn't work in _vimrc file. It would be ok if you replace it with <bar> (as the answer in stackoverflow suggest) – Alex Jul 08 '15 at 11:08
  • Why doesn't | work in Windows? – Martin Tournoij Jul 08 '15 at 12:45
  • | doesn't work, you either need \| or <bar>. – romainl Jul 08 '15 at 12:48
  • on the documentation, there is no mention of this incompatibility. But as @michaelmichael said on he's answer : NB: You may find that your ~/.vimrc doesn't support mapping |, or \|. In these cases, try using <bar> instead. – nobe4 Jul 08 '15 at 12:49
  • 1
    This answer is very much incorrect. Even without escaping \| for nnoremap, it would only work if columns+=1 was a command, but it's actually a setting. set lines+=1 columns+=1 would be correct (changing multiple settings from the same :set command. – filbranden Dec 26 '20 at 17:14
  • When using in <expr> both mac and windows have to use <bar><bar> instead of || to express logical or. @filbranden – NeoZoom.lua Apr 04 '21 at 09:02
11

I would recommend you to use for example <C-P>, since <C-[> is the same as <Esc> and <C-]> is used to jump to a definition of the keyword under the cursor.

You can just do this:

nnoremap <C-P> <Esc>:set columns+=1 lines+=1<CR>

The another way is to use the command separator |, but you have to escape it or use <Bar> to use it as an argument for the nnoremap command, otherwise it would be recognized as command separator in the vimrc file, not in the mapping.

nnoremap <C-P> <Esc>:set columns+=1 \| set lines+=1<CR>

So when you press <C-P>, this command is executed:

set columns+=1 | set lines+=1
MichalH
  • 265
  • 1
  • 10
  • But also note that the <C-p> is used to autocomplete text in vim, which is a built-in utility. I haved used a <M-p> instead. But anyway, the<Esc> in front of the command is a very good addition. – Alex Jul 08 '15 at 13:42
  • @Alex You have mapped it only in normal mode. So you can still use <C-P> in insert mode to autocomplete text. – MichalH Jul 08 '15 at 13:51
  • oh yes you are right. – Alex Jul 08 '15 at 23:17
3

I was mapping two commands to one F-key to execute 2 plugin commands and none of the above worked for me. The only option that worked for me was with to <CR>:

nnoremap <F10> :Doctest <CR> :lwindow<CR>
Biggybi
  • 2,740
  • 9
  • 29
dmayilyan
  • 31
  • 1
  • That's because user-commands such as :Doctest typically don't accept | as a separator (they take it as an argument.) See :help :bar which explains a lot of that. One solution is to use :execute: nnoremap <F10> :execute 'Doctest' \| lwindow<CR>. But yeah using two separate <CR>s would work for sure. – filbranden Dec 26 '20 at 16:16
  • Thx this helped! – Tim Strijdhorst Jul 28 '21 at 12:58