We’re rewarding the question askers & reputations are being recalculated! Read more.
64

You can display a ruler at a specific line using the :set colorcolumn (:set cc for short) option which is only available in Vim 7.3 or later. set colorcolumn=80 This will set the background color of that column to red, giving you a visual ruler to work from. If you want to use a different color other than red (which really stands out), you can change the ...


39

Unified answer: If you are using tabs I'd try using listchars: :set listchars=tab:\|\ :set list note: there is a space after the last \ above. This method is good because it uses Vim's built-in support for showing this kind of thing. Example output: | if (1) { | | func(); | } However, this doesn't work for leading spaces. As ...


37

You can use the gq or gw operators combined with a motion command. By default, it uses the fmt program (in Linux) to format the given text. However, to the best of my knowledge, it does not justify the lines so you will get ragged right margins. The way I use it is to gwip (normal mode) with the cursor on a paragraph. This will format the current paragraph ...


35

I am the author of the plugin vim-table-mode. I've spent quite a lot of time building & improving this, especially with community feedback and it caters exactly to this use case. A short quickstart: You enter 'table mode' with the :TableModeEnable command, you can then simply type: || | MOVIE | QUOTE| || | Blade Runner | Like tears in the rain | | ...


19

As an addendum to Kevin's answer, you can have multiple colorcolumns. When I code, I sometimes have a "soft" limit at 80 columns and a "hard" limit at 120 columns. So I want a line at 80, and then every column after 120 to be colored. I do this with let &colorcolumn="80,".join(range(120,999),",") Of course, this can be easily modified to other ...


19

Another option I use, which isn't exactly what you asked for, but accomplishes the same purpose of making it easier to see what is in the same column, is to set the following in your vimrc: set cursorcolumn set cursorline This has the trade off that you have to move your cursor to the column you want to see, but you get less visual clutter. Usually when I ...


18

For the sake of completeness, there is also the Indent Guides plugin, which shows the indentation levels by alternating the colors of white spaces. It requires version 7.2 or later. Since it uses the tabstop and shiftwidth variables, it will probably work for your indentation settings right away. Once the plugin is installed, you can simply use:...


16

Here's a slightly more efficient / easier method. Type it with the cursor at the beginning of the line of text (i.e. LOAD) that you want to center. :center 80<cr>: center the text with Vim's built in function hhv0r#: add the #s on the left A<space><esc>40A#<esc>: add plenty of #s on the right d80|: delete excess #s on the right YppVr#...


15

There is a great VimCast on this topic. http://vimcasts.org/episodes/formatting-text-with-par/ Basically, you need to install par: $ brew install par # or $ sudo apt-get install par And then, since you want columns to be wrapped in 80 columns: :set formatprg=par\ -w80 Now you can use the gq operator, like in other answers (e.g. gqip), and it will use ...


11

From :help usr_25: JUSTIFYING TEXT Vim has no built-in way of justifying text. However, there is a neat macro package that does the job. To use this package, execute the following command: :...


11

clang-format is a command line tool that formats c++ code. One of the default formatters is google. clang-format -style=google There are plugins that would allow you to run clang-format in vim. One such plugin is https://github.com/rhysd/vim-clang-format (I have not used it, it was the first I found)


9

I've found the indentLine plugin to be the best for this. You do need a font with the appropriate symbols though, I find Consolas on Windows and Menlo on Mac OS X work well. As is pointed out in the unified answer above, it works by (ab)using vim's conceal feature to put vertical bars (from the font used) in the right place. I'm pretty happy with this ...


9

If you can use the rev command, you could reverse each line, sort and reverse again, using external commands in a pipe: %!rev | sort | rev This can be done entirely in Vimscript, but it's way more verbose: function! RTLSort() range " reversing function for use with map() function! Rev(k, str) " the first argument from map() is an index, so ...


8

Here's a method not using any plugin. The strategy is to use tabs as a temporary column delimiter, then reformat the tabs as spaces. Start by setting :set noexpandtab (noet for short) Type the text, using the Tab key to insert a horizontal tab character after each colon. It will probably look slightly wrong, like this: alpha: 1 beta: 2 gamma: 3 delta:...


7

You can do this with the vim-lion plugin. Install it, then place your cursor on the first line. In normal mode, type glip/. You'll be prompted to type in a pattern, so just type \d to match any digit, and press Enter. It will line up the numbers for you.


7

The Tabular plugin works with regular expressions. So, you can give a word or a pattern, not just a single character. :Tabularize /from/ will align in the way you wanted. You can find a great tutorial about Tabular on Vimcasts.


7

If you're in a pinch and want to get the expressions aligned, without having to install and learn any plug-ins, here is a quick way to do it. Select the lines on a visual selection. For example, if this is your whole buffer, you could use ggVG, but if these lines are in the middle of a file, just select appropriately. Perhaps V4j? Insert enough whitespace ...


6

If you can use sed and column, a command-line solution that comes close would be: !sed 's/[- ]*\([+|]\)/'$'\x01''\1/g' | column -ts $'\x01' | sed '/^[-+ ]*$/s/ /-/g' You could combine this with visual selection and '<,'> or with line numbers. Cons: Uses sed and column. Vim is incidental. The sed commands can probably be changed to vim :substitute ...


6

I'm not sure why you don't want to use search/replace, but here's a reusable command: command! HTMLArgEq %s/\s\@<!=\+\s\@!/ \0 /g It uses search/replace, but you only need to create it once. Explanation: %s/ - Substitute entire buffer \s\@<! - Not preceded by whitespace =\+ - One or more equal characters \s\@! - Not followed by whitespace / \...


6

Here's a simple mapping to do the task: vnoremap <silent> rs "zy:call setreg("z", system("echo \"" . @z . "\" \| sed 's/[ \\t]\\+//' \| rs -j 0 1"), "b")<CR>gv"zp It uses z register to keep output of it's intermediate steps. I had to add call to sed to remove trailing white chars from the input to rs, because rs would produce strange output (...


5

While nano's help call the ^J command "Justify" it actually doesn't justify the text, it reformat the text breaking the lines before the right margin of the screen (or at some configurable column, I guess). You can do the same thing in VIM by setting the variable textwidth, from :help 'textwidth': Maximum width of text that is being inserted. A longer ...


5

If you don't want to install any plugins and you want to edit tables keeping the alignment: -- use the built in VREPLACE mode, activated by gR. (Make sure you has vim latest version, and check if it's compiled with +vreplace feature, typing vim --version on the terminal) (my statusline is set to show REPLACE for both REPLACE and VREPLACE modes) If you ...


5

(EDITED version) "============ in your vimrc ================= command! TogglePush :call TogglePush() inoremap <Insert> <Esc>:TogglePush<CR>a nnoremap <Insert> :TogglePush<CR> command! EnablePush :call EnablePush() command! DisablePush :call DisablePush() "============= in a script file ============= let s:push_enabled = ...


5

Here's what I would do. Visually select all of your HTML you want to do this to, and hit the following: :norm f=i<space><C-v><esc>la<space> Note that <C-v>, <esc>, and <space> are keystrokes, not literal text. Explanation: :norm Means apply the following set of keystrokes to every selected line. f= means move to ...


5

Without entering insert mode (not sure if it's better though): Example text (cursor on C of the first line): A BBCBB A BBCBB Type: bjywkP Decomposing: bj go back to the start of the word, one line down (below the first B) yw yank the next word (being the whitespaces) k move up P past before the cursor Edit: Thanks to antony for ...


4

You can use :<range>Align \s which will use the white spaces as separators. It will give you the following: #define ORING 'r' #define OLRING "ring" To align only the part between the quotes you can use: :<range>Align \('\|"\).* which means "align starting on ' or " and all characters following it". This gives the following: #define ...


4

Vim's comment formatting only handles comment characters that appear at the start of the line. However you could make use of the list formatting facility (:help 'formatlistpat') that kicks in when n is in 'formatoptions'. So, define an appropriate pattern: :set flp=^.*::\\s\\+ (You may need to adjust this: I don't speak Python.) Then add n to '...


3

See accepted answer to Align a block of code on the basis of a single character To recap ggVG to select all :'<,'>normal f:5a you don't have to enter '<,'> and also note space at the end. For each line in a selection it will execute normal command to find : and add 5 spaces after it goto second space after epsilon: <here> and press <C-...


Only top voted, non community-wiki answers of a minimum length are eligible