How to disable vim pasting to/from system clipboard? - c++11

Basically, I need advice on how to do the opposite of what is described here:
How to make vim paste from (and copy to) system's clipboard?
I do not want vim to overwrite system clipboard when I delete something within the editor. The usual workflow for me is to select a piece of code (let us call it piece #1) with a mouse, then go to another piece of code (let us call it piece #2), select piece #2 in visual mode in vim, delete it using "c", and paste piece #1 using mouse middle button. With the new behavior, instead of pasting piece #1 I paste back piece #2.
In vim 7.2 I can do that, but in vim 7.4 I can not. I want to use 7.4 due to proper C++11 syntax highlighting but this changed default behavior is killing me.
I think, it has something to do with the "+xterm_clipboard" and/or "+clipboard" features of vim (vim --version), and I need to change them somehow to "-xterm_clipboard" and/or "-clipboard". I tried to recompile vim 7.4 with the option "--with-x=no" but somehow it did not help.
Please, help me to disable copying/pasting in vim to system clipboard by default.
Thank you!

Well, to answer the question in your title, you can just do
:set clipboard=
Which will make vim use it's internal register instead of the system one.
But really, this behavior has nothing to do with the system clipboard. It's more just a general misunderstanding of vim registers.
Vim by default "cuts" rather than "deletes". This means that every delete command (c and d) yanks to the unnamed register at the same time. To get around this, just use the black hole register, e.g. use "_c or "_d instead of just doing c or d.
9. Black hole register "_ *quote_*
When writing to this register, nothing happens. This can be used to delete
text without affecting the normal registers. When reading from this register,
nothing is returned. {not in Vi}
To make this the default behavior for your specific case, you can do
xnoremap c "_d
The 'x' in 'xnoremap' means this mapping applies to visual mode (with v) and select mode (with the mouse)
Related SO link.

check
:h clipboard-unnamed
This extension may be also of use to you http://vimawesome.com/plugin/easyclip

Related

Keyboard-only column block selection in GVim Win32, or why does Ctrl-Q not emulate Ctrl-V when mswin.vim is included?

I want to be able to select columnar blocks using only the keyboard when I use GVim on Windows, but I do not seem to be able to do so when using gvim (7.2) on Windows XP. Edit: For convenience, it is preferable to make the selection with arrow keys as is commonly done in other versions of vi/vim.
In a terminal, when using vim, to define a block, one may select columnar blocks by pressing Ctrl-V and by conveniently moving the cursor with the arrow keys.
When using GVim on mswin, Ctrl-V is mapped to a paste operation. Every reference found on this topic mentions that on mswin, Ctrl-Q is set to act the same way that Ctrl-V works on other platforms, but this does not work for me.
After research indicated that the Ctrl-Q behavior is implemented through the inclusion of mswin.vim, it seemed that perhaps mswin.vim was not being loaded by GVim. mswin.vim is apparently loaded via _vimrc. It seemed that perhaps mswin.vim was not loading, so perhaps _vimrc was not setup correctly, but...
C:\Program Files\Vim\_vimrc exists contains:
source $VIMRUNTIME/mswin.vim
behave mswin
C:\Program Files\Vim\vim72\mswin.vim exists and contains:
" Use CTRL-Q to do what CTRL-V used to do
noremap <C-Q> <C-V>
C:\Program Files\Vim\_vimrc should load since Edit | Settings Window in the GVim graphical menu loads this file for editing, and it is clear that C:\Program Files\Vim\vim72\mswin.vim is loading because one can insert a syntax error in that file and GVim will complain about it when it starts up (it was backed it up for testing and restored to prevent accidental corruption).
When Ctrl-Q is pressed, the cursor changes in the same way that it changes when you block select text using the mouse, but any cursor movement at this point causes the cursor to change back to a normal cursor. It does not matter whether one continues to hold Ctrl, Ctrl-Q, or not.
Edit: Ctrl-Q enters block selection mode as indicated by the cursor change since other cursor movement commands extend the block, however, the original intent of this question was to learn how to use the arrow keys (in the same manner that they are able to be used in other implementations of the editor) though this was not explicitly stated in the original, unanswered revisions of the question.
It is possible to visually select columnar blocks by pressing Alt while holding down the left-mouse button, but Alt during arrow key motion after pressing Ctrl-Q also does not work.
Research also seemed to indicate that pressing v in command-mode would enter visual-mode, and that perhaps this was relevant, but using this does not seem to help the situation.
C:\Program Files\Vim\vimfiles does not contain anything except an empty directory structure. C:\Documents and Settings\username does not contain vim configuration files, nor does C:\Documents and Settings\username\My Documents. Other C:\Documents and Settings\username locations, where application-specific or local settings are commonly stored, also appear not to have vim settings. The same is true for %HOMEDRIVE% and for the environment variables.
One way to visually select column blocks in GVim on Win32 using only the keyboard is to press Ctrl-Q, release it, then press and hold down the Shift key while using the arrow keys to select the column block.
Why Ctrl-Q columnar selection behaves this way is not known, but it does not really matter since the goal was to select column blocks using only the keyboard. In Vim, Shift is not used when using Ctrl-V to select blocks.
:help mswin does not mention this nuance of the CTRL-V alternative.
The best thing to do is to remove these lines from your vimrc. They are totally unecessary and change too many basic Vim features.
The noremap <C-Q> <C-V> in mswin.vim remaps the original command to Ctrl + Q.
If mswin.vim is included, after pressing Ctrl + Q, the indicated mode should switch to Visual Block, and any movement (e.g. j, l, w) extends the visual selection. If that doesn't work for you, you may have something interfering.
Are those movement commands itself remapped, or is there an autocmd on CursorMoved?! Try disabling your plugins (vim --noplugin) and most parts of your .vimrc.
Note that cursor keys do not normally constitute proper movement (and their use in Vim is frowned upon).
What you perceive as the right (and only) way to extend the selection (with shifted cursor keys) is just a consequence of :set selectmode=key, as done by :behave mswin.

How do I switch tabs based on incremental search in vim?

For example, let's say I have three tabs open in vim:
1: nice_program.c
2: something_fun.h
3: super_script.sh
So if I hit some magic modifier key, and then type 'n' and hit enter I change tab to tab 1. Likewise, typing 'su' instead will navigate me to tab 3 instead.
Is such behavior possible? There are so many vim extensions, and I dont really get the whole vim extension lingo.
BTW, I am using gVim on XP and MacVim on OS X. Preferably the solution will work on both...
EDIT:
Note that I only want the incremental search to search across the names of the open tabs. That is, it's not supposed to actually search inside the tabs themselves.
Also, I never use buffers, it's tabs that I want this working for.
From the wording of the question it seems that you take the idea of tabs in
Vim not the way it is supposed to be taken by design of this feature. A Vim
tab page is not a form of a buffer or a window, it is a window layout
container, instead. No wonder there is no built-in way for switching to a tab
by the name of a buffer that is active (or the only one in its tab page, or
special in some other way). Semantically, that is switching to a buffer, not
a tab (but tab could be switched in order to show a buffer, if it is
necessary).
To switch to a buffer by its name use the :sbuffer command (:sb, for
short). It is not necessary to type the whole buffer name each time, since
the command has auto-completion. Usually one have to type only few letters of
a name to uniquely identify a buffer (the same way as you described
incremental search in the question).
By default, Vim open the requested buffer displacing one in the current
window. This behavior is governed by the switchbuf option. One of the
choices (called usetab) provided by that option allows to switch to a window
in another tab page if that window contains the buffer to edit. This is
exactly what suits your manner of work with tab pages.
To summarize, change the switching behavior as follows
:set switchbuf=usetab
and use the :sb command to open a buffer by typing a few letters of its name
and using Tab-completion.
I use this snippet I picked up in vim wiki to switch between open buffers (mapped to F5):
" switch between numbered buffers
:nnoremap <F5> :buffers<CR>:buffer<Space>
(put in your .vimrc file or whichever dotfile you use).
As for incremental search across open buffers, whenever I look up something using either /[something] or with */# on current word, it's automagically also highlighted in other buffers/tabs. Then I can switch buffers and hit n or N to move between matches in the currently viewed buffer. That's already baked into Vim.
Hope that helps.
The :set switchbuf=usetab solution given by ib never worked for me for whatever reason (even without loading plugins or my .vimrc) but :tab drop name-of-file works just the way you want (I found it on the Vim wiki).
Make it a custom mapping to save a few keystrokes with nnoremap <leader>t :tab drop.
Also I second ib's comment on the right and wrong way to use tabs in Vim.

How to use vim in the terminal?

How does one setup and start using vim in the terminal on OS X?
I want to start writing my C code using vim in the terminal rather than a separate text editor. How does one get started on this?
The basics like: opening, creating, saving files via terminal using vim and writing code using vim. Also, does one compile directly using vim in the terminal?
Get started quickly
You simply type vim into the terminal to open it and start a new file.
You can pass a filename as an option and it will open that file, e.g. vim main.c. You can open multiple files by passing multiple file arguments.
Vim has different modes, unlike most editors you have probably used. You begin in NORMAL mode, which is where you will spend most of your time once you become familiar with vim.
To return to NORMAL mode after changing to a different mode, press Esc. It's a good idea to map your Caps Lock key to Esc, as it's closer and nobody really uses the Caps Lock key.
The first mode to try is INSERT mode, which is entered with a for append after cursor, or i for insert before cursor.
To enter VISUAL mode, where you can select text, use v. There are many other variants of this mode, which you will discover as you learn more about vim.
To save your file, ensure you're in NORMAL mode and then enter the command :w. When you press :, you will see your command appear in the bottom status bar. To save and exit, use :x. To quit without saving, use :q. If you had made a change you wanted to discard, use :q!.
Configure vim to your liking
You can edit your ~/.vimrc file to configure vim to your liking. It's best to look at a few first (here's mine) and then decide which options suits your style.
This is how mine looks:
To get the file explorer on the left, use NERDTree. For the status bar, use vim-airline. Finally, the color scheme is solarized.
Further learning
You can use man vim for some help inside the terminal. Alternatively, run vimtutor which is a good hands-on starting point.
It's a good idea to print out a Vim Cheatsheet and keep it in front of you while you're learning vim.
Run vim from the terminal. For the basics, you're advised to run the command vimtutor.
# On your terminal command line:
$ vim
If you have a specific file to edit, pass it as an argument.
$ vim yourfile.cpp
Likewise, launch the tutorial
$ vimtutor
You can definetely build your code from Vim, that's what the :make command does.
However, you need to go through the basics first : type vimtutor in your terminal and follow the instructions to the end.
After you have completed it a few times, open an existing (non-important) text file and try out all the things you learned from vimtutor: entering/leaving insert mode, undoing changes, quitting/saving, yanking/putting, moving and so on.
For a while you won't be productive at all with Vim and will probably be tempted to go back to your previous IDE/editor. Do that, but keep up with Vim a little bit every day. You'll probably be stopped by very weird and unexpected things but it will happen less and less.
In a few months you'll find yourself hitting o, v and i all the time in every textfield everywhere.
Have fun!
if you want to open all your .cpp files with one command, and have the window split in as many tiles as opened files, you can use:
vim -o $(find name ".cpp")
if you want to include a template in the place you are, you can use:
:r ~/myHeaderTemplate
will import the file "myHeaderTemplate in the place the cursor was before starting the command.
you can conversely select visually some code and save it to a file
select visually,
add w ~/myPartialfile.txt
when you select visualy, after type ":" in order to enter a command, you'll see "'<,'>" appear after the ":"
'<,'>w ~/myfile $
^ if you add "~/myfile" to the command, the selected part of the file will be saved to myfile.
if you're editing a file an want to copy it :
:saveas newFileWithNewName
If you want to learn by reading yourself:
Open MacOS terminal app.
Write this and press enter -> vimtutor
For quit write this and click -> :q

Things I wish I could do in VIM while programming Ruby

Im facing some problems, I looked around in the forum and didnt find
any solutions discussed. Im sorry if these have been resolved earlier.
Is there someway I can make the VIM line break after 80 characters. I
dont want the text to wrap around but create a new line. And I wish it would
break off the complete last word. So instead of fo in the previous and o
in the next line, can it break with foo in the next line?
When I end my comment and press enter, I get a # in the new line. This is
cool but when I delete # and want to start a line of code, I dont get syntax
highlighting there. It still thinks what Im typing is a comment. Is this a
bug or am I doing it wrong?
One more thing is that I have set the shiftwidth to 4. But when I press
Ctrl+S to save the document, the cursor jumps to the beginning of the
sentence. I then need to manually go back to my original position to begin
the code. Is there a way I can resolve this?
Thank you for reading this. I am new to Ruby and Vim. I hope you guys help
me out.
Ctrl-S ? This is not known to me. In Vim/Gvim, a file is usually saved by
:w filename.ext (if none's been given yet)
or
:saveas filename.ext
(for all of these commands try ":help :w" or ... the same principle).
I don't know about the comment part, since I don't do Ruby, but it would be pretty wise for you to get yourself a nice commenter plugin (I think I use LineCommenter) - eases up on the commenting. Just write the comment, and add the #'s later (set it to work in normal and in visual mode; it works beautifully).
As for the breaking the text part, that could be solved by adding
:set tw=80
"wrapscan" is the vim feature that wraps a whole word to the next line; it might not be set by default in your configuration - probably isn't. So in addition to
:set tw=78 you probably want to try one of these:
:set wrapscan
:set wrap <- just a shorter version
:set nowrap <- to turn the wordwrap feature back off
Incidentally, rather than setting the text width (tw) to some number of characters (smaller than your window), you could instead set the margin you want to leave on the right side of the window like so:
:set wrapmargin=1
If wrapmargin is set to something other than 0, textwidth should be ignored.
I would use ":w" to save and continue editing (or ":w filename" if it's a new file) and "ZZ" or ":wq" to save-and-exit when you're done - none of those will move the cursor position.
I'm not sure where your "#" continuation is coming from, but I'd also make sure to set these if they aren't already (you can check what variables are set by just typing ":set" with no other options):
:set syntax=ruby
:set filetype=ruby
:syntax enable
If you started with an empty document and then added "#!/usr/bin/ruby" to it, vim won't notice you're editing ruby until you save&exit and reopen the file. There are other cases where syntax coloring isn't very bright or needs a nudge but yes, that sounds like a bug to me.

Why does < C-a> (CTRL+A) not work under gvim on windows?

I'm trying to use the < C-a> (CTRL+A) shorcut under vim to increment a variable under the cursor. This works fine under vim running on Linux. However when I try to do this in gvim under windows it "selects all" (i.e. highlights or visually selects all text in the current window). How can I change this behaviour or alternatively how can I recover the increment variable functionality (e.g. perhaps with a different key mapping)?
This is because of mswin.vim that is sourced by the default _vimrc generated when you install vim.
Just override your _vimrc with the .vimrc you are using under *nix, or delete mswin.vim.
I know I'm late to the party, but I thought I'd share the following:
nnoremap <kPlus> <C-a>
nnoremap <kMinus> <C-x>
This remaps increment to the + key on the numeric keypad and decrement to the - key. It's the solution I've used in my own _vimrc file on Windows. It keeps the Windows compatibility and is easier to remember than the original Ctrl+A/Ctrl+X as well.
Vim on Windows has specialized key mappings by default to make shortcuts more "windows-y". These are specified in the $VIMRUNTIME\mswin.vim file, which is loaded via your vimrc unless you disabled it. You can edit the mswin.vim file (you may want to edit a copy instead, changing your vimrc to use your edited copy instead).
I'm not entirely sure it's a default Vim mapping, since the only reference I can find on Ctrl+A in the help file is this, which doesn't seem to do what you are referring to:
*c_CTRL-A*
CTRL-A All names that match the pattern in front of the cursor are
inserted.
so you may want to check your Linux box to see if any plugins or anything change the key mapping. (Of course, it may be that I just can't find the appropriate part of the Vim help.)
in the current version of mswin.vim provided with gvim, the file checks for the value of a global named skip_loading_mswin; If set, the rest of the file is skipped; thus it is sufficient to add
let skip_loading_mswin=1
to $HOME/_vimrc and normal vim bindings will be restored the next time you start vim.
If you just do not like CtrlA behaviour but are fine with other windows behaviours in VIM (like CtrlZ for undo), just disable that specific line:
Edit said file (c:\Program Files\Vim\vim73\mswin.vim for me)
Find the paragraph starting with CTRL-A is Select all
Prepend all (6) lines of that paragraph with opening brackets (")
Reopen your GVIM windows.
You can still "select all" by typing ggVG (position cursor at first line, select entire line, select until the last line of the document).
Happy incrementing!

Resources