Things I wish I could do in VIM while programming Ruby - 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.

Related

How to use vim as less -R? [duplicate]

Recently I've discovered a wonderful terminal multiplexing tool called gnu-screen.
I'm satisfied with it completely. But I've encountered one inconvenience I'd like to improve.
'C-a H' command makes screen log everything to a log file called named 'screenlog.*'.
But encodes control characters in a weird way. For example if you open the log file with 'less' you might see some cryptic characters and the log file is unreadable. You have to run 'less -r' or 'less --raw-control-chars' which helps to encode those control characters correctly.
So far so good. But if you want to edit the log or read it with vim then you encounter the same problem with control characters.
I've googled this problem and looked up at SO but I've been overwhelmed because there's so much info about vim and screen. Unfortunately I haven't found the solution yet.
Perhaps you know the solution for this problem or some workaround.
UPD
Thanks to Frédéric Hamidi's comment I discovered those characters are the terminal escape sequences of font color, etc. Vim as an editor sees them and by default edits them. The plugin Frédéric suggested tells vim to interpret them.
To provide an answer here and finally mark this fixed: The AnsiEsc plugin adds syntax highlighting for ANSI color sequences. So instead of seeing ^[[30m; the following text will be highlighting in the corresponding color, just like when using less --raw-control-chars.

How Do I Make Backspace Send ^H In Vim?

I have a problem that is my backspace works in the terminal but not in Vim. It sent '^?' instead of '^H' and I have been trying to figure it out. I have checked my .bashrc and there is stty erase '^?' to remove the '^?' but somehow it is not working.
I have this in my .vimrc:
set backspace=indent,eol,start
set backspace=2
fixdel
This is how I would normally fix it according to my knowledge but it doesn't work in this case. I'm using xterm on Arch Linux server if that helps, I know there is a bug for xterm but I don't think that's the case. It works in Unix command but not Vim, so just the Vim that isn't working.
Any help is greatly appreciated.
Edit:
Um to clarify, I just want my backspace to delete things. My backspace doesn't delete things right now and it is giving me ^? instead of deleting. The deleting is the same as any other system that is ^H but I want to make backspace to send ^H which is delete not ^?. So instead of having to type ^H to delete, I want to do it with backspace, on MobaXterm there is a function that makes backspace to send ^H. But how do I do that on SSH?
Thanks
There's two ways to go. To the left, you see the control-H character you crave. The path that way seems clear, just a little
echo xterm.vt100.backarrowKey: true >> ~/.Xresources; xrdb ~/.Xresources
followed by either restarting all of your xterms, or pressing control and your primary mouse button on each window to bring up the menu that lets you change it per window; the menu item itself is the second one in the third section, it's right under the 8-bit controls option. That way seems to be viable. Except, beware, there's a trap door under the control-H itself, as then backspace stops working properly everywhere else except bash itself. Of course, you have part of the way out of that maze already, with your stty fu. But I kind of recall running into all sorts of other issues with that path. One of them being that the Linux console also uses ^? instead of ^H, and that's not so easy to change.
Or, the other way is to get vim to be able to do what you're trying for, using ^?. Which I think is just
:map ^V^? x
Note that I mean control-V backspace, not caret V caret question. You would also want to add this to your ~/.vimrc if that does what you need. That having been said, you didn't say exactly where in vim isn't working quite right, so it's possible you're needing a different tweak instead.

How to disable vim pasting to/from system clipboard?

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

ConqueTerm bash in Vim, key input delayed

I've just found out that I can use ConqueTerm to bring up a terminal window in Vim. But I have an annoying issue regarding this.
Lets say I go into insert mode to type commands, and then want to exit insert mode, it will not work unless I press Esc key and a different key. It seems to be a set timeout or delay of some sort. Symbols and characters show up in the bottom right corner of the terminal window.
I had the same problem when editing files in vim, but added "set timeoutlen=1000 ttimeoutlen=0" to my .vimrc which sorted this out for me. But I've been looking around and I cannot seem to find anything similar for ConqueTerm. My guess is that something is to be put/edited in "~/.vim/autoload/conque_term.vim"?!
Any help would be most apprechiated!
Thanks

.vimrc to behave like Textmate?

I am making the switch to VIM from Textmate and I wanted to know what should be included in my .vimrc to get the similar behavior for:
auto-close on brackets, paranthesis, etc.
auto tabbing after a bracket
when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line
pressing <tab> on for will generate a common 'for' use
Thanks.
The first thing to be aware is that you can't turn Vim into TextMate. The second, is that you shouldn't even try.
Instead, focus your efforts on learning Vim, progressively, and grow/shrink your collection of settings and plugins as you need. Using Janus or some other "distro" will only occult Vim behind other people's arbitrary choices.
That said,
"auto-close on brackets, paranthesis, etc."
can be done in a "dumb" way by adding this line to your ~/.vimrc:
inoremap ( ()<Left>
inoremap is for mapping in insert mode (note the i)
( is the key you want to press, that's your trigger
()<Left> means input a pair of parenthesis then go back one character, between the parenthesis
If you need a "smarter" way, there are a many plugins to choose from.
You might want to try surround which was in and of itself a very compelling reason for me to switch from TextMate.
"when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line"
can be done in many ways as well, for example:
inoremap <C-Return> <CR><CR><C-o>k<Tab>
inoremap, again
<C-Return> means Ctrl+Return, that's your trigger
<CR><CR> means 2 carriage returns to push the closing bracket 2 lines below
<C-o> to leave insert mode only for one command
k to go up one line
<Tab> to put the insertion cursor at the right place
Mappings in Vim can be just that: shortcuts for sequences of keypresses or more serious scripts.
"pressing <Tab> on for will generate a common 'for' use"
can be done with a dedicated plugin like SnipMate. There are others.
auto-close on brackets, paranthesis, etc.
There are a number of plugins for this functionality: autoclose, closepairs, simplepairs
auto tabbing after a bracket
By this, I believe you mean automatic indentation. This is a default Vim behavior when you're working on a supported filetype. If you're just starting on a file and haven't saved it, you can manually set the filetype with :set ft=<whatever filetype you want> and you'll get indentation and syntax highlighting.
when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line
This will be taken care of with the above auto-close plugins.
pressing tab on for will generate a common 'for' use
These are called "snippets" in TextMate. Snipmate is a commonly used Vim plugin for snippets. Other people prefer xptemplate.
If you're moving from TextMate to Vim, I would try installing Janus and thoroughly reading the documentation.
Janus is a great starter distribution of plugins and mappings for Vim, gVim, and MacVim. It features sane defaults and aims to provide a minimal working environment using the most popular plug-ins and the most common mappings. It was developed and is maintained by Carl Lerche and former TextMate user Yehuda Katz.
My own personal experience/advice is that you can really drive yourself crazy trying to mimic every feature of your favorite editor exactly as it was. Each feature you're trying to replicate is probably just a quirk you became used to through muscle memory. You'll be much more productive if you just start "accepting" Vim, learning it, and re-train your muscle memory for it.

Resources