Vim: is there a command to switch windows - vim

I need a : command to switch windows because I'm using it in a function. so <C-W>W won't do.

Using:
wincmd k
I got what I needed

I had a problem with using normal <C-w>w in a function as well.
My problem was that I ran it within execute where you need to escape the first angle bracket, resulting in execute("normal \<C-w>w"). Hope this still helps some people understand it.
For more help use :help normal and :help execute in Vim

Related

vim copy into clipboard with colon cmd

I want to yank multiple lines with a colon command into the UNIX-clipboard without visual mode, like this:
:1,4"+y
or
:1,$"+y
The clipboard is active --> echo has('clipboard') returns 1
I'm on raspberry pi and tried gvim and vim.
Can anybody shed some light onto this?
Thank you and kind regards!
This is possible with the :yank command, which can be abbreviated :y. It takes as its argument the register without the ". So, your examples could be written as so:
:1,4y +
:1,5y +
While it's possible to do this with the :normal command to do this with something like :normal 1G4"+yy, this would be a lot more complex and needlessly so. :yank is simpler and more flexible if you're fine with linewise operations.
I think the other answer is amazing! I didn't know of the :y ex command.
Just for completeness, an alternative a bit more verbose is that you can yank in the default register " (i.e. as you would do normally in Vim-only workflow) and then just copy the content into the other register via :let #+ = #".

How do I map a key combination to perform a command in Vim?

I want the key combination <C-X> to run the command :! python application.py. From all my understanding of mapping, I tried the following: :map <C-X> :! python application.py which almost works. It just opens the command line and types the command in, but I still have to press return for the command to launch.
Is there any way I can get it to run the command as well?
I found an answer in an unrelated post while doing my last searches before posting the question. It seems that putting <CR> behind the command executes it, making this do what I wanted:
:map <C-X> :! python application.py<CR>
According to the comments (thanks to melpomene) <CR> means "carriage return", which in effect means "press enter" and is just what I needed in this case.
Slightly related, I also found out that :!! repeats the last shell command executed, making my map somewhat superfluous.
Don't forget you can escape any key sequence with CtrlV (in Insert mode). So another way of getting the keycode for "Return" is CtrlVEnter. It will come out looking like ^M.

How to go back (ctrl+z) in vi/vim

In normal text editors [with all due respect to Vim] there is a shortcut Ctrl+Z when you have done something nasty and want to return to the previous version of the text. Like BACK button in Word. I wonder how can you achieve this behaviour in Vim.
You can use the u button to undo the last modification. (And Ctrl+R to redo it).
Read more about it at: http://vim.wikia.com/wiki/Undo_and_Redo
The answer, u, (and many others) is in $ vimtutor.
Just in normal mode press:
u - undo,
Ctrl + r - redo changes which were undone (undo the undos).
Undo and Redo
Here is a trick though. You can map the Ctrl+Z keys.
This can be achieved by editing the .vimrc file. Add the following lines in the '.vimrc` file.
nnoremap <c-z> :u<CR> " Avoid using this**
inoremap <c-z> <c-o>:u<CR>
This may not the a preferred way, but can be used.
** Ctrl+Z is used in Linux to suspend the ongoing program/process.
On a mac you can also use command Z and that will go undo. I'm not sure why, but sometimes it stops, and if your like me and vimtutor is on the bottom of that long list of things you need to learn, than u can just close the window and reopen it and should work fine.
I had the same problem right now and i solved it. You must not need it anymore so I write for others:
if you use gvim on windows, you just add this in your _vimrc:
$VIMRUNTIME/mswin.vim behave mswin
else just use imap...

Limit command mode mappings to start of commands

By default, I want to open the help windows vertically. I'm able to do this with :vert bo help. To make things easier, I tried to create a map it by :cnoremap vh vert bo help. However, this replaces all instaces of vh. How do I limit it to changing only if is at the beginning?
I also tried using cabbrev. It is slightly better and waits till a space but suffers from the same problem
Instead of using cmap, you can use command which support TAB-completion:
for example:
:com! -nargs=1 -complete=help H :vert bo help <args>

Using makefiles and vim without prompt

I'm using vim and im doing a lot of
:make
within vim. The only thing that is really annoying is that I have to press ENTER twice to jump back to the editor. I just want to go directly back if everything worked out fine. And I want to see the error once and press a key to jump directly to the error line.
Any ideas?
This is known as the hit-enter prompt:
If you accidentally hit or and you want to see the displayed
text then use |g<|. This only works when 'more' is set.
To reduce the number of hit-enter prompts:
Set 'cmdheight' to 2 or higher.
Add flags to 'shortmess'.
Reset 'showcmd' and/or 'ruler'.
Also, I'm sure you are aware of the quickfix window (:copen) to navigate errors/messages?
This can happen when the 'cmdheight' varible is < 2.
I had the same problem. This is a simple solution that seems to work:
map <F2> :silent make^M
Now I just hit the F2 key to compile and the annoying prompt is not so annoying.
Not sure if it would help you, but if you're on Windows you could try:
:set makeprg=start\ make
I think this will break the errorfile setting though.
See:
:help make
:help !start

Resources