Vim Tips Wiki
(Move categories to tip template)
Tag: Visual edit
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=427
 
|id=427
Line 16: Line 15:
 
<pre>
 
<pre>
 
if bufwinnr(1)
 
if bufwinnr(1)
map + &lt;C-W&gt;+
+
map + <C-W>+
map - &lt;C-W&gt;-
+
map - <C-W>-
 
endif
 
endif
 
</pre>
 
</pre>
   
I normally use the scrollpad + and - keys.
+
I normally use the scrollpad + and - keys. (&lt;kPlus&gt; and &lt;kMinus&gt; in [[gvim]])
   
 
The map eliminates the need for time-consuming chording and if you liked chording, you'd be using Emacs instead of Vim anyway...
 
The map eliminates the need for time-consuming chording and if you liked chording, you'd be using Emacs instead of Vim anyway...
   
 
==Comments==
 
==Comments==
Depending on your screen size, and your brain's memory capacity for more vim mappings (caution this will affect your every day speech when vim mappings exceeds 100), you may want to increase the windows' frame size to some preselected settings. I am sure there is a more elegant solution for incrementing the size of this (get the current setting into the buffer, increment it and set it again). I am too lazy so I use full screen and min screen based on my 1400x1050 and guifont=lucida_console:h7:w5 " small (94 lines in one screen)
+
Depending on your screen size, and your brain's memory capacity for more vim mappings (caution this will affect your every day speech when vim mappings exceeds 100 [[citation needed]]), you may want to increase the windows' frame size to some preselected settings. I am sure there is a more elegant solution for incrementing the size of this (get the current setting into the buffer, increment it and set it again). I am too lazy so I use full screen and min screen based on my 1400x1050 and guifont=lucida_console:h7:w5 " small (94 lines in one screen)
   
 
<pre>
 
<pre>
map &lt;M-[&gt; :set co=210&lt;CR&gt;:set lines=180&lt;CR&gt; "fullscreen
+
map <M-[> :set co=210<CR>:set lines=180<CR> "fullscreen
map &lt;M-]&gt; :set co=50&lt;CR&gt;:set lines=50&lt;CR&gt; "minimumscreen
+
map <M-]> :set co=50<CR>:set lines=50<CR> "minimumscreen
map &lt;M-\&gt; :set co=120&lt;CR&gt;:set lines=80&lt;CR&gt; "my default screen size
+
map <M-\> :set co=120<CR>:set lines=80<CR> "my default screen size
 
</pre>
 
</pre>
   
Line 39: Line 38:
 
<pre>
 
<pre>
 
if bufwinnr(1)
 
if bufwinnr(1)
map - &lt;C-W&gt;&lt;UP&gt;
+
map - <C-W><UP>
map + &lt;C-W&gt;&lt;DOWN&gt;
+
map + <C-W><DOWN>
 
endif
 
endif
 
</pre>
 
</pre>
Line 47: Line 46:
   
 
----
 
----
The &lt;C-W&gt;&lt;UP&gt; and &lt;C-W&gt;&lt;DOWN&gt; work well if you're only split horizontally, but if you split vertically, this might make rotating between windows a bit easier:
+
The <C-W><UP> and <C-W><DOWN> work well if you're only split horizontally, but if you split vertically, this might make rotating between windows a bit easier:
   
 
<pre>
 
<pre>
 
if bufwinnr(1)
 
if bufwinnr(1)
map - &lt;C-W&gt;&lt;C-W&gt;
+
map - <C-W><C-W>
map + &lt;S-C-W&gt;&lt;S-C-W&gt;
+
map + &lt;S-C-W>&lt;S-C-W>
 
endif
 
endif
 
</pre>
 
</pre>
Line 60: Line 59:
 
Here are some of them:
 
Here are some of them:
   
map + &lt;c-w&gt;+
+
map + <c-w>+
map - &lt;c-w&gt;-
+
map - <c-w>-
   
 
What was posted as the original tipp. I had the same idea, i think.
 
What was posted as the original tipp. I had the same idea, i think.
   
map &lt;c-n&gt; &lt;c-w&gt;&lt;
+
map <c-n> <c-w><
map &lt;c-m&gt; &lt;c-w&gt;&gt;
+
map <c-m> <c-w>>
   
 
Similar to the mappings above, but for horizontal resizing.
 
Similar to the mappings above, but for horizontal resizing.
   
map &lt;c-w&gt;F &lt;c-w&gt;_&lt;c-w&gt;&lt;bar&gt;
+
map <c-w>F <c-w>_<c-w><Bar>
   
 
Fullscreen the actual window. (not depending on the terminal's size)
 
Fullscreen the actual window. (not depending on the terminal's size)
   
map &lt;Up&gt; &lt;c-w&gt;k&lt;c-w&gt;_&lt;c-w&gt;&lt;bar&gt;
+
map <Up> <c-w>k<c-w>_<c-w><Bar>
map &lt;Down&gt; &lt;c-w&gt;j&lt;c-w&gt;_&lt;c-w&gt;&lt;bar&gt;
+
map <Down> <c-w>j<c-w>_<c-w><Bar>
map &lt;Left&gt; &lt;c-w&gt;h&lt;c-w&gt;_&lt;c-w&gt;&lt;bar&gt;
+
map <Left> <c-w>h<c-w>_<c-w><Bar>
map &lt;Right&gt; &lt;c-w&gt;l&lt;c-w&gt;_&lt;c-w&gt;&lt;bar&gt;
+
map <Right> <c-w>l<c-w>_<c-w><Bar>
   
 
Walking through windows and make them fullscreen at the same time.
 
Walking through windows and make them fullscreen at the same time.
Line 88: Line 87:
 
" Fast window resizing with +/- keys (horizontal); / and * keys (vertical)
 
" Fast window resizing with +/- keys (horizontal); / and * keys (vertical)
 
if bufwinnr(1)
 
if bufwinnr(1)
map &lt;kPlus&gt; &lt;C-W&gt;+
+
map <kPlus> <C-W>+
map &lt;kMinus&gt; &lt;C-W&gt;-
+
map <kMinus> <C-W>-
map &lt;kDivide&gt; &lt;c-w&gt;&lt;
+
map <kDivide> <c-w><
map &lt;kMultiply&gt; &lt;c-w&gt;&gt;
+
map <kMultiply> <c-w>>
 
endif
 
endif
   
Line 100: Line 99:
   
 
" Window resizing mappings <nowiki>/*{{{*/</nowiki>
 
" Window resizing mappings <nowiki>/*{{{*/</nowiki>
nnoremap &lt;S-Up&gt; :normal &lt;c-r&gt;=Resize('+')&lt;cr&gt;&lt;cr&gt;
+
nnoremap &lt;S-Up> :normal <c-r>=Resize('+')<CR><CR>
nnoremap &lt;S-Down&gt; :normal &lt;c-r&gt;=Resize('-')&lt;cr&gt;&lt;cr&gt;
+
nnoremap &lt;S-Down> :normal <c-r>=Resize('-')<CR><CR>
nnoremap &lt;S-Left&gt; :normal &lt;c-r&gt;=Resize('&lt;')&lt;cr&gt;&lt;cr&gt;
+
nnoremap &lt;S-Left> :normal <c-r>=Resize('<')<CR><CR>
nnoremap &lt;S-Right&gt; :normal &lt;c-r&gt;=Resize('&gt;')&lt;cr&gt;&lt;cr&gt;
+
nnoremap &lt;S-Right> :normal <c-r>=Resize('>')<CR><CR>
   
 
function! Resize(dir)
 
function! Resize(dir)
 
let this = winnr()
 
let this = winnr()
 
if '+' == a:dir || '-' == a:dir
 
if '+' == a:dir || '-' == a:dir
execute "normal \&lt;c-w&gt;k"
+
execute "normal \<c-w>k"
 
let up = winnr()
 
let up = winnr()
 
if up != this
 
if up != this
execute "normal \&lt;c-w&gt;j"
+
execute "normal \<c-w>j"
 
let x = 'bottom'
 
let x = 'bottom'
 
else
 
else
 
let x = 'top'
 
let x = 'top'
 
endif
 
endif
elseif '&gt;' == a:dir || '&lt;' == a:dir
+
elseif '>' == a:dir || '<' == a:dir
execute "normal \&lt;c-w&gt;h"
+
execute "normal \<c-w>h"
 
let left = winnr()
 
let left = winnr()
 
if left != this
 
if left != this
execute "normal \&lt;c-w&gt;l"
+
execute "normal \<c-w>l"
 
let x = 'right'
 
let x = 'right'
 
else
 
else
Line 126: Line 125:
 
endif
 
endif
 
endif
 
endif
if ('+' == a:dir &amp;&amp; 'bottom' == x) || ('-' == a:dir &amp;&amp; 'top' == x)
+
if ('+' == a:dir && 'bottom' == x) || ('-' == a:dir && 'top' == x)
return "5\&lt;c-v&gt;\&lt;c-w&gt;+"
+
return "5\<c-v>\<c-w>+"
elseif ('-' == a:dir &amp;&amp; 'bottom' == x) || ('+' == a:dir &amp;&amp; 'top' == x)
+
elseif ('-' == a:dir && 'bottom' == x) || ('+' == a:dir && 'top' == x)
return "5\&lt;c-v&gt;\&lt;c-w&gt;-"
+
return "5\<c-v>\<c-w>-"
elseif ('&lt;' == a:dir &amp;&amp; 'left' == x) || ('&gt;' == a:dir &amp;&amp; 'right' == x)
+
elseif ('<' == a:dir && 'left' == x) || ('>' == a:dir && 'right' == x)
return "5\&lt;c-v&gt;\&lt;c-w&gt;&lt;"
+
return "5\<c-v>\<c-w><"
elseif ('&gt;' == a:dir &amp;&amp; 'left' == x) || ('&lt;' == a:dir &amp;&amp; 'right' == x)
+
elseif ('>' == a:dir && 'left' == x) || ('<' == a:dir && 'right' == x)
return "5\&lt;c-v&gt;\&lt;c-w&gt;&gt;"
+
return "5\<c-v>\<c-w>>"
 
else
 
else
 
echo "oops. check your ~/.vimrc"
 
echo "oops. check your ~/.vimrc"
Line 145: Line 144:
   
 
" Maps Alt-[h,j,k,l] to resizing a window split
 
" Maps Alt-[h,j,k,l] to resizing a window split
map &lt;silent&gt; &lt;A-h&gt; &lt;C-w&gt;&lt;
+
map <silent> <A-h> <C-w><
map &lt;silent&gt; &lt;A-j&gt; &lt;C-W&gt;-
+
map <silent> <A-j> <C-W>-
map &lt;silent&gt; &lt;A-k&gt; &lt;C-W&gt;+
+
map <silent> <A-k> <C-W>+
map &lt;silent&gt; &lt;A-l&gt; &lt;C-w&gt;&gt;
+
map <silent> <A-l> <C-w>>
   
 
" Maps Alt-[s.v] to horizontal and vertical split respectively
 
" Maps Alt-[s.v] to horizontal and vertical split respectively
map &lt;silent&gt; &lt;A-s&gt; :split&lt;CR&gt;
+
map <silent> <A-s> :split<CR>
map &lt;silent&gt; &lt;A-v&gt; :vsplit&lt;CR&gt;
+
map <silent> <A-v> :vsplit<CR>
   
 
" Maps Alt-[n,p] for moving next and previous window respectively
 
" Maps Alt-[n,p] for moving next and previous window respectively
map &lt;silent&gt; &lt;A-n&gt; &lt;C-w&gt;&lt;C-w&gt;
+
map <silent> <A-n> <C-w><C-w>
map &lt;silent&gt; &lt;A-p&gt; &lt;C-w&gt;&lt;S-w&gt;
+
map <silent> <A-p> <C-w>&lt;S-w>
   
 
I find it very useful as my shift and ctrl key are overused for mapping and I don't like moving my hand to the arrow keys (yes I am very very lazy)
 
I find it very useful as my shift and ctrl key are overused for mapping and I don't like moving my hand to the arrow keys (yes I am very very lazy)
  +
  +
The above bindings do not work in *nix environments. To make them work, see: http://stackoverflow.com/questions/16599867/vim-can-i-have-the-last-key-of-a-multi-key-binding-repeat-the-command
   
 
----
 
----
  +
vim

Revision as of 18:27, 29 September 2014

Tip 427 Printable Monobook Previous Next

created February 14, 2003 · complexity basic · author Bob Heckel · version 5.7


Depending on your willingness to occasionally consume the + and - keys, here's a fast way to resize the active window if you have more than one window open:

if bufwinnr(1)
  map + <C-W>+
  map - <C-W>-
endif

I normally use the scrollpad + and - keys. (<kPlus> and <kMinus> in gvim)

The map eliminates the need for time-consuming chording and if you liked chording, you'd be using Emacs instead of Vim anyway...

Comments

Depending on your screen size, and your brain's memory capacity for more vim mappings (caution this will affect your every day speech when vim mappings exceeds 100 citation needed), you may want to increase the windows' frame size to some preselected settings. I am sure there is a more elegant solution for incrementing the size of this (get the current setting into the buffer, increment it and set it again). I am too lazy so I use full screen and min screen based on my 1400x1050 and guifont=lucida_console:h7:w5 " small (94 lines in one screen)

map <M-[> :set co=210<CR>:set lines=180<CR> "fullscreen
map <M-]> :set co=50<CR>:set lines=50<CR> "minimumscreen
map <M-\> :set co=120<CR>:set lines=80<CR> "my default screen size

I'm not much for resizing windows.. However, I stole your tip and made a slight modification:

if bufwinnr(1)
  map - <C-W><UP>
  map + <C-W><DOWN>
endif

Now I can go through my open windows very easily with + and - :)


The <C-W><UP> and <C-W><DOWN> work well if you're only split horizontally, but if you split vertically, this might make rotating between windows a bit easier:

if bufwinnr(1)
  map - <C-W><C-W>
  map + <S-C-W><S-C-W>
endif

I have several mappings for window movement/operations in my .vimrc, too. Here are some of them:

map + <c-w>+
map - <c-w>-

What was posted as the original tipp. I had the same idea, i think.

map <c-n> <c-w><
map <c-m> <c-w>>

Similar to the mappings above, but for horizontal resizing.

map <c-w>F <c-w>_<c-w><Bar>

Fullscreen the actual window. (not depending on the terminal's size)

map <Up> <c-w>k<c-w>_<c-w><Bar>
map <Down> <c-w>j<c-w>_<c-w><Bar>
map <Left> <c-w>h<c-w>_<c-w><Bar>
map <Right> <c-w>l<c-w>_<c-w><Bar>

Walking through windows and make them fullscreen at the same time.

In addition, I've set winminwidth and winminheight to 0.


Using the ideas above I come up with this version :)

" Fast window resizing with +/- keys (horizontal); / and * keys (vertical)
if bufwinnr(1)
  map <kPlus> <C-W>+
  map <kMinus> <C-W>-
  map <kDivide> <c-w><
  map <kMultiply> <c-w>>
endif

+ / - didn't work for me, it moved the cursor up and down, but with kPlus, etc. it's OK.


I have added the following to my vimrc file. Holding down the SHIFT key and using the arrow keys is quick and efficient for resizing the windows.

" Window resizing mappings /*{{{*/
nnoremap <S-Up> :normal <c-r>=Resize('+')<CR><CR>
nnoremap <S-Down> :normal <c-r>=Resize('-')<CR><CR>
nnoremap <S-Left> :normal <c-r>=Resize('<')<CR><CR>
nnoremap <S-Right> :normal <c-r>=Resize('>')<CR><CR>
function! Resize(dir)
  let this = winnr()
  if '+' == a:dir || '-' == a:dir
    execute "normal \<c-w>k"
    let up = winnr()
    if up != this
      execute "normal \<c-w>j"
      let x = 'bottom'
    else
      let x = 'top'
    endif
  elseif '>' == a:dir || '<' == a:dir
    execute "normal \<c-w>h"
    let left = winnr()
    if left != this
      execute "normal \<c-w>l"
      let x = 'right'
    else
      let x = 'left'
    endif
  endif
  if ('+' == a:dir && 'bottom' == x) || ('-' == a:dir && 'top' == x)
    return "5\<c-v>\<c-w>+"
  elseif ('-' == a:dir && 'bottom' == x) || ('+' == a:dir && 'top' == x)
    return "5\<c-v>\<c-w>-"
  elseif ('<' == a:dir && 'left' == x) || ('>' == a:dir && 'right' == x)
    return "5\<c-v>\<c-w><"
  elseif ('>' == a:dir && 'left' == x) || ('<' == a:dir && 'right' == x)
    return "5\<c-v>\<c-w>>"
  else
    echo "oops. check your ~/.vimrc"
    return ""
  endif
endfunction
" /*}}}*/ 

This is what I use.

" Maps Alt-[h,j,k,l] to resizing a window split
map <silent> <A-h> <C-w><
map <silent> <A-j> <C-W>-
map <silent> <A-k> <C-W>+
map <silent> <A-l> <C-w>>
" Maps Alt-[s.v] to horizontal and vertical split respectively
map <silent> <A-s> :split<CR>
map <silent> <A-v> :vsplit<CR>
" Maps Alt-[n,p] for moving next and previous window respectively
map <silent> <A-n> <C-w><C-w>
map <silent> <A-p> <C-w><S-w>

I find it very useful as my shift and ctrl key are overused for mapping and I don't like moving my hand to the arrow keys (yes I am very very lazy)

The above bindings do not work in *nix environments. To make them work, see: http://stackoverflow.com/questions/16599867/vim-can-i-have-the-last-key-of-a-multi-key-binding-repeat-the-command


vim