Structural Highlighting in Vim - visual-studio

I have seem some cool Structural Highlighting in Visual Studio (might be using CodeKana). Can I achieve same type of Structural Highlighting in vim ? For any language. I am a python developer I would love to have it for Python.

You can almost emulate this via listchars by using hard tabstops instead of spaces for indentation.
Something like this should suffice:
set noexpandtab
set list
set listchars=tab:\|
Note that there is a space after the | character.
You can pick a better character to get a connected line, but this is just an example. Also note that this will not continue the lines with no indentation, so you may end up with something like:
if foo:
| bar
| baz
...unless you add a tab on the blank line.

What OP asks for is definitely not built in. I assume someone could add it as a plugin extension. An existing plugin does do multi-color highlighting of parens that is somewhat similar, maybe it could be used as a base to build a more full-fledged solution:
RainbowParenthesesInVim

Related

Is it safe to use cat <<-EOF

cat <<-EOF
#lots of text
EOF
This syntax sure makes it simpler to manage lots of lines and also is just a single call to cat rather than a bunch of echos.
But is it safe to use <<-?
There are many text editor settings that may mess with spaces and tabs and its annoying to keep track of those. (portability is a concern)
Using only << would make the source code look dull without indentation.
Is there any safer and clean way to do this?
I have made a small edit and now it doesn't seem to be opinion based in any way.
You should realize that some users set their editors to automatically convert tabs into spaces. So when you work with multiple users on the same script, it is paramount to set up some base rules for this, which is annoying as you will end up with a one-day tab-vs-spaces war. Furthermore, I often consider tabs evil. They are good as a field separator, but they are awful when visualization depends on it. Everything depends on where the tabstops are defined and this can change from editor to editor and terminal to terminal. Sometimes. the tabstops are defined as a multiple of 8, sometimes as a multiple of 4. There is even the Posix command tabs which allows you to set the tabstops in any way you want
$ tabs 1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67
So ... tabs are evil!
My suggestion to you is to use the following:
printf "%s\n" 'fantastic line1' \
'awesome line2' \
'amazing line3' \
'unrivaled line4'

Windows 'choice' command messing up Ruby 'gets' method

Open up irb and
type gets. It should work fine.
Then try system("choice /c YN") It should work as expected.
Now try gets again, it behaves oddly.
Can someone tell me why this is?
EDIT: For some clarification on the "odd" behavior, it allows me to type for gets, but doesn't show me the characters and I have to press the enter key twice.
Terminal input-output handling is dark and mysterious art. Anyone trying to make colorized output of bash work in windows PowerShell via ssh knows that. (And various shortcutting habits like Ctrl+Backspace only make things worse.)
One of the possible reasons for your problem is special characters handling. Every terminal out there can type characters in number of different modes, and it parses its own output in search for certain character sequences in order to toggle states.
F.e. here one can find ANSI escape code sequences, one of possible supported standards among different kind of terminals.
See there Esc[5;45m? That will make all the following output to blink on magenta background. And there is significantly more stuff like that out there.
So, the answer to your question taken literally is — your choice command messes something with output modes using special escape sequences, and ruby's gets breaks in that quirk special mode of terminal operation.
But more useful will be the link to HighLine gem documentation. Why one might want to implement platform-specific and obtrusive behavior when it is possible to implement the same with about 12 LOC? All the respect for the Gist goes to botimer, I've only stumbled into his code using search.

What can I do in VIM that I can't already do in Visual Studio? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I heard it takes 30 days minimum to get comfortable with vi. I'm on day 2 hehe. Right now, I seem to be merely memorizing different shortcuts for things I already did in Visual Studio (incremental search, prev/next word, etc.).
So far the most powerful aspect seems to be the numeric keys combined with commands (5 * next line), and the idea of normal/insert modes.
There are a few things I miss from Visual Studio. Ctrl-Click'ing the mouse for quick copy and pasting is probably the biggest.
So that I don't get discouraged, can you guys walk me through some things in vi that you do regularly that can't be done in Visual Studio? It'll help me focus on what to learn and help me develop better habits.
I'll just leave a link to this SO answer here.
VI means never ever having to take you fingers off the keyboard.
Note that I don't use Visual Studio, and know little about the available features in it. The following are examples of what I find useful in Vim, not a list of missing features in Visual Studio.
Macros
It's easy to create macros for complex (but repetitive) operations. To illustrate with a simple example, let's say we start with:
Line1
Line2
Line3
Line4
Line5
Now we want to envelop each line in a print(""); statement.
Place the cursor on the first line, and enter:
qx to start recording a macro to the register x
Shift+I print(" Esc to insert text at the beginning of the line
Shift+A "); Esc to append text at the end of the line
j to go down one line
q to stop recording the macro
4#x to execute the macro in register x 4 times
See :help complex-repeat for more info on Vim macros.
Text objects
Note that this is one of the improvements Vim has over the traditional Vi. If it doesn't work, you're probably running in Vi compatibility mode; use :set nocompatible to enable the full functionality of Vim.
Text objects allow you to easily select regions of text. Let's say we start with the following text, and place the cursor on some text:
<b><i>some text</i></b>
Now we want to delete everything between <i> and </i>. This can be done by simply typing the command dit (d'elete i'nner t'ag)! Or if we want to include the tags themselves in our selection, use dat (d'elete a t'ag). To delete everything inside the <b> tags, use d2it (d'elete two i'nner t'ags).
You can similarly use daw (delete a word), dap (delete a paragraph), di" (delete inside double-quotes), etc; see :help text-objects for the complete list.
Another useful example of text objects:
v2ap"+y
v toggles visual mode. This makes it easier to see what you're selecting, and lets you adjust your selection with a series of multiple motions before you execute a command.
2ap selects this paragraph and the next one
"+ selects the system clipboard as register for the next operation
y yanks the selection to the given register
In other words, that command would copy two paragraphs from your text to the system clipboard (e.g. for pasting them here at StackOverflow).
Global editing
The global command is used to apply an Ex command to all lines matching a given regular expression. Examples:
:global/test/print or :g/test/p would print all lines containing the phrase test
:global/test/delete or :g/test/d would delete said lines
:global/test/substitute/^/#/ or :g/test/s/^/#/ would search for lines containing the phrase test, and comment them out by substituting the regexp anchor ^ (beginning-of-line) with the symbol #.
You can also do some cool stuff by passing the search motions /pattern or ?pattern as ranges:
:?test?move . searches backwards for a line containing test, and moves it to your current position in the file
:/test/copy . searches forwards for a line containing test, and copies it to the current position in the file
Good luck and have fun learning Vim!
Edit a file on a Solaris machine that only allows SSH access.
This article is what got me started on Vim, and I never looked back:
http://www.viemu.com/a-why-vi-vim.html
It has some great examples on Vim's power.
Use screen to keep a session running on a remote machine accessed over ssh
Visual Studio's regular expressions are a little bit Mickey Mouse. Vim has the full POSIX regular expression language at your fingertips.
As far as I can tell (in Visual C# express 2010) ctrl-click just selects whatever word you click on. To do the same in VIM, you can combine the yank command with a movement command.
So you press "y" for yank (copy) then "e" or "w" to copy to the end of the word.
There is many differences.
Block (and column) wise copy, paste, edit
the dot command! (after duck tape the second most powerful tool on the planet, seriously)
I suggest you watch some screencasts at http://vimcasts.org/ to get a feeling of the power of vim.
e.g.:
http://vimcasts.org/episodes/creating-the-vimcasts-logo-as-ascii-art/
http://vimcasts.org/episodes/selecting-columns-with-visual-block-mode/
You could always use the Vim emulator/add-on for Visual Studio and get some of the power of vim mixed with the features of VS. If you're already using Visual Studio, I assume you're using a .NET language, which without VS, would be much more painful to use.
Vim Essentials is a nice set of slides.
Personally, I got used to vi a long time ago, when we didn't have the luxury of a mouse in student's Unix terminals. Since then, I used vi/vim for everything safe for writing emails.
To this day, I probably use only 1/20 of the commands, but never felt the need to write code with another text editor, and reaching for a mouse in an IDE feels very clumsy to me.
Using high level and expressive languages, that do not require an IDE (mainly python, sql, javascript) really helps. I suppose it wouldn't be as easy with Java or C++.
Not having to move and point with the mouse when coding (safe for using the browser) also helps preventing Carpal tunnel syndrome.
BTW, I suppose Vim integrates better with Unix than with Windows... and who said 30 minutes was a little optimistic :)
Edit documents over SSH. Vim's really nice for that.
Edit: looks like a lot of people have already said that :)
teco is your answer. You only need a PDP-10 and an ASR-33 and you're on your way!

What are the advantages of using vim to program Ruby (over Notepad++)? (other languages, too, but specifically Ruby) [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I've been using Notepad++ for a while; in fact, I've even started using Launchy for that "load this resource into the editor right now" functionality that many fuller IDE's like Eclipse has. It has syntax highlighting, split window view, code collapsing, parentheses (and other delimiter) paring, automatic indent, block commenting.
However, it seems that everywhere I go, people are using vim for their programming needs.
I know the basics of vim; it's my main "basic text editor" when I'm on a linux machine; I use it like I'd use the basic Notepad on Windows. I get the controls, most of the shortcuts, the repetition eliminators, etc.
What I'm not quite understanding are these killer apps that people are espousing about. How "debugging Ruby in vim was a life-changing experience", or how, when using it, magic occurs.
Am I missing some essential plug-ins? Is there a dimension I'm not seeing? Should I just shut up and start using it for a bit, to see? How do I get syntax highlighting?
I'm specifying Ruby specifically because I'd like to find some plugins for it. Note that I am not using Rails, and answers should be rails-independent of possible. But I would appreciate some general vim-ness koans about programming in other languages, as well.
Thank you, and I hope my question isn't too vague or inspires any nasty editor wars.
I use vim for all my Ruby programming, and I think its customizability is its killer feature. With vim you can do just about everything to text you can imagine, if you're willing to invest the time to hunt down the plugins that do what you want or write a few scripts yourself.
I'll just list a few things I like about vim for programming (in no particular order):
Syntax Highlighting
The ruby syntax highlighting is very nice. One thing in particular that seems to be somewhat unique is that keywords can be colored differently depending on context. This isn't used as much as I would like, but you can easily see whether that end statement closes, say, an if-statement or a function definition.
Also nice is that, since vim knows which parts of the text are comments, you get spell checking for these only.
Automatic indenting.
When you're writing code, vim will automatically place the cursor at the right indentation level, so you don't have to worry about that. I also find myself invoking this functionality manually by selecting a block and pressing = to automatically (re-)indent everything I highlighted.
Autocompletion
I use a plugin that automatically pops up completions.These are very versatile. They know the methods of classes from the standard library, look at other files you have open (good for variable names and class methods), recognize when you're typing a filename, etc.
Snippets
There is a plugin called snipMate that provides shortcuts for often used text snippets. It's a big help with writing tests and the like.
Code folding
Scriptability
As I said, vim's scripting is very powerful. Want extraneous whitespace at the end of the line deleted automatically? Just write a one-line script.
Plugins, Plugins, Plugins!
There are a ton of plugins that help you with all kinds of things. Git integration, Rails integration, Rspec integration, autoclosing parentheses, matching keywords that open a block {def,do,if,while, etc.} to their end... the list is practically endless.
It's an exceedingly powerful editor out of the box, it integrates well with version control, and there are bucketloads of good add-ons available. (See the scripts page as well as the tips wiki.) Those are good reasons to consider Vim, but there are plenty of other good editors available for various platforms. (Look, Ma, no religious editor wars!)
In terms of very Ruby-specific add-ons, check out endwise by Tim Pope, as an example. (It automatically inserts end after do, if, etc.) Actually nearly all of Tim Pope's scripts are potentially useful for Rubyists.
How do I get syntax highlighting?
You need at least a minimal .vimrc or .gvimrc to get syntax highlighting and automatic indentation (assuming you want that). Vim ships with examples that can get you started, and if you search for 'vimrc' or 'gvimrc', you will get plenty of hits. That said, here's some of mine to get you started:
" Most general settings first
set nocompatible " Set Vim rather than Vi settings; must go first
set noeb " Set no audio or visual error beep
set bs=indent,eol,start " Backspace over everything in insert mode
set history=500 " Keep 50 lines of command line history
" Set items for view # bottom of windows
set ruler " Show the cursor position all the time
set showcmd " Display incomplete commands
set showmode " Display current mode
set ls=2 " Always show status bar
" Syntax basics
syntax on
filetype indent on
set autoindent
set smartindent
filetype plugin on
" Text basics
set textwidth=80 " Set text to wrap at 80 columns
set expandtab " Convert tabs to spaces
set tabstop=4 " Tabs = 4 spaces
set shiftwidth=4 " Indent/outdent 4 spaces
set softtabstop=4 " Tab key indents
set shiftround " Indent/outdent to nearest tabstop
set smarttab " Uses shiftwidth # start of lines
set fo=trcn
" An exception for Ruby files
autocmd FileType ruby set tabstop=2
autocmd FileType ruby set shiftwidth=2
autocmd FileType ruby set softtabstop=2
autocmd FileType ruby set number
" Search basics
set incsearch " Do incremental searching
set showmatch " Show matching brackets
set hlsearch " Highlight all matches in a search
" Don't use Ex mode, use Q for formatting
map Q gq
" Pick a colorscheme
colorscheme Dim
My personal killer feature of vim is the humble . command. This command repeats the last edit at the current cursor position. This can save oodles of time.
For me, one of the "killer features" of vim is it's ad-hoc macros.
Press q then a key name to store the macro in (I often use m for a macro mnemonic, but any letter is fine) and you'll notice the recording status at the bottom. Now, any key strokes you press will be recorded until you press q again, and you've recorded a macro in the letter m. Now type #m and your keystrokes will be played back, with all their implications, starting from the current cursor position. Press 20#m and you'll replay the macro 20 times, and now you've got a powerful tool for programmatically editing text without the overhead of writing a larger program (or configuration file).
For Ruby specifically, the syntax highlighting (:syn on), automatic indent (:set cindent), and paren/bracket paring (% to move the cursor to the matching brace) and other features can be found in other editors, as you mention. But really, the general text processing macros in vim are a big advantage for any text file.
I think the main advantage VIM has is the fact it's cross platform. Now you're using notepad++, that is a great editor(even if it hasn't good macros capabilities). Tomorrow, you could be obliged to use another OS, and you should learn to use another text editor.
I know where you are coming from. I used IDEs and even notepad++ for longer than I care to remember. If all you use VIM for is basic navigation, you aren't going to be using it to its full potential. while there are several very powerful plugins, I don't believe that finding the right plugin is what makes VIM so powerful.
What does it for me is that my hands do not move from the keyboard and it allows me to stay in the zone. Every time I go to the mouse, it invariably leads to a concentration break - which as a programmer is the unforgivable sin.
The other killer feature for me is searching and replacing using regex. I highly recommend learning more about it. Or in the words of Jamis Buck, "Know Thy Tools".
ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
is a great way to get started, but I think I would recommend the O-Reilly book over this one if you want to spend the money. It is easy to get the basics in VIM, but mastering it takes time - but they payoff is immense.

What setting in vim counteracts smartindent's refusal to indent # comments in shell scripts?

I recently started using vim 7 (previously vim 6) and the smartindent setting. For the most part, it works well, though I'm so used to typing a tab after an open brace that it is almost counter-productive.
However, there is one piece of maniacal behaviour. When editing a shell script, I try to create a comment at the current indent level, but smartindent will have nothing to do with it. It insists that the comment must be at level 0 (no indent). What's worse, it breaks shift-right ('>>' and friends) so that they do not work. This is outright insubordination, and I'd like to know what's the best way to fix it?
(I'm also not keen on smartindent's ideas about indenting then after if.)
Preferred solutions will save me manual bashing - I'm being lazy. One option would be 'turn off smartindent when editing shell scripts (leave it on for the rest)'. Another option would be guidelines on how to find the control script for smartindent and what to edit to change the characteristics I don't like. The final option (which I don't need advice on how to do - just the hint that it is the best, or only, way to restore sanity) is to leave smartindent unset.
I saw the vaguely related question on "(PHP and) annoying vim unindent rules"; it doesn't provide me with the direct answer, though maybe the cindent and related items mentioned in there are in fact part of the answer.
Find the indent file, (e.g. /usr/share/vim/vim71/indent/sh.vim on my system)
This line looks like the problem:
setlocal indentkeys-=:,0#
Perhaps you can fix this in your .vimrc or load a custom indent file manually.
edit: It looks more complicated than I thought, but maybe there is something specifically set in the indenting file that you would need to fix.
2nd edit: Looks like I was completely wrong, Check out:
Restoring indent after typing hash
or
howto-configure-vim-to-not-put-comments-at-the-beginning-of-lines-while-editing
Well, after exploring some options, including using ':set cindent' instead of ':set smartindent', I've ended up reverting to just using ':set autoindent'. There probably are ways to make this stuff work exactly as I want it to, but it is messy enough and fiddly enough that I can't be bothered. I've worked fine with autoindent for the previous 20-odd years, and the benefits from the extra bells and whistles provided by smartindent are outweighed by the what I regard as its misbehaviour.
Thank you, Juan, for your assistance. Believe it or not, it did help - quite a lot.
I also discovered a couple of other neat commands, though, while following up on this:
>i}
>a}
These right-shift the block of code you are in. The 'i' version indents the body and not the closing braces (my preferred style), and the 'a' version indents the closing braces to (the version that is required at work).
Also, you can apply qualifiers to '%' in commands executed at the shell:
:make %:r.o
This would run make on the 'root' of the current file name (that's '%:r') followed by '.o'. Or, in other words, if I'm editing somefile.c, this executes make somefile.o.
Add the line below in your .vimrc
filetype indent on
(it will set the right indent mode depending on the filetype)
I had this same issue for a long time, until I realized that autoindent and smartindent are both unnecessary if "filetype indent on" is set in your vimrc - 'filetype indent on' uses the indent/sh.vim (or whatever language) file in your vim directory to figure out the indentation rules, and autoindent and smartindent both can interfere with it.
I haven't tested this with sh, but perl suddenly started behaving properly when I switched.
Sidenote: Juan's redirect, "Restoring indent after typing hash", is not a good solution - while it does correct the problem in one situation (typing code in), it doesn't change how the editor thinks it should be indented, so a re-indent (visual =, or normal ==) will shove it back to the left.
The previous answer suggesting:
:inoremap # X^H#
is excellent. It is the answer suggested by the VIM documentation at ":help smartindent". Note that ^H is entered using CTRL-V CTRL-H. The relevant section from the documentation is below.
When typing '#' as the first character in a new line, the indent for
that line is removed, the '#' is put in the first column. The indent
is restored for the next line. If you don't want this, use this
mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H.
When using the ">>" command, lines starting with '#' are not shifted
right.
I have the following lines in my .vimrc and I don't observe the problem.
set smartindent
inoremap # X^H#
I used to have set autoindent after these two lines but it seems that it has no effect.
Yes that is very annoying. smartindent is really only for C like languages.
See how I enable the appropriate indenting based on language at:
http://www.pixelbeat.org/settings/.vimrc

Resources