How to change all selected chars to _ in Vim - vim

I try to draw a class diagram using Vim.
I fill the editor window with white-spaces.
Type :match SpellBad /\s/ to highlight all the white-spaces.
Ctrl+Q to select vertical white-spaces.
Shift+I to insert Bar(|) and then Esc
...........................
v+l +... + l to select horizontal white-spaces
But I don't know how to change all selected horizontal white-spaces to underscore(_).
I have to hit _ serval times.
When comes to long horizontal line, it's bad.
___________ ___________
| | | |
| BaseClass |/__________| Client |
|___________|\ |___________|
/_\
|
|____________________________________
| | |
_____|_____ _____|_____ _____|_____
| | | | | |
| SubClass1 | | SubClass2 | | SubClass3 |
|___________| |___________| |■■■■■■■■■■■|
I want a quick method to do this.
Select it -> Change it -> Done!
Maybe map F6 to do it.
Thanks!

After everything you want to change is highlighted, type r_.

There are plenty of tools for making ascii diagrams, and converting them to graphical UML. Why don't you try one of those?
e.g.
http://www.jave.de/
http://ditaa.sourceforge.net/
http://www.diku.dk/hjemmesider/studerende/firefly/stud.html

Related

Can we combine grid layout and terminal panel in vscode?

There is the grid layout in vs-code, and there is the terminal panel. Is there a way to combine both?
Lets say I want this :
--------------------------------------------------------------
| | |
| | |
| | |
| | something |
| | here |
| editor here | |
| | |
| | |
| |---------------------------|
| | |
| | |
| | terminal |
| | here |
| | |
| | |
| | |
--------------------------------------------------------------
I know we can put the terminal on the right, but I do not know if we can put it in the bottom of the right column. It works with other panel though, like editor.
To my best understanding, you cannot put the whole terminal panel (thinking of the combined: problems, output, debug console, terminal, ... group) in the grid layout.
However, you can put individual terminal shells into the editor and they will behave like opened files, able to be moved between editor panes.
After you have opened the terminal panel, you can drag and drop the shell's name (located just left of the little + sign where you can create a new terminal. It will have a name like bash or any other shell you are using) into the editor.
To make it easier, there is a setting for terminal.integrated.defaultLocation where you can specify the value to be editor. You split the editor panel as you see fit and then press workbench.action.terminal.new (default keycode is Ctrl+`) and you terminal will appear in the selected grid section.

Vim: do not move window content when opening preview / quickfix

When splitting Vim window horizontally, contents of the "old" window are scrolled so as to preserve relative cursor line position. This happens even for small "helper" buffers, like quickfix or preview window.
The text movement caused by this becomes annoying when a split is repeatedly opened and closed - e.g. preview window used by completion plugin. Is it possible to disable this feature, and don't scroll old window when splitting (unless it's necessary to keep cursor line visible)?
Example - current behavior:
+--------------+ +--------------+
| a | | b |
| b | copen | c (cursor) |
| c (cursor) | --> | d |
| d | +--------------+
| e | | (preview) |
+--------------+ +--------------+
Desired behavior:
+--------------+ +--------------+
| a | | a |
| b | copen | b |
| c (cursor) | --> | c (cursor) |
| d | +--------------+
| e | | (preview) |
+--------------+ +--------------+
I reached this question when searching for a solution to this problem myself. I couldn't find a good solution and it really bugged me so I ended up writing a small vim plugin that will solve this.
https://github.com/gillyb/stable-windows
I think it does exactly what you want it to do. I only wrote it recently, so if there's any bugs feel free to open an issue and I will try to fix them.
Hope it helps! :)
Try something like
map <F12> mcHmh:split<cr>'hzt`c
store position in c mc
H move to top
store the position to h mh
your command here split for example
got to h 'h
put this to lien to top zt and got to c

How to use vi to edit a command in terminal on Linux?

When typing a very long command, I'd like to first edit the command in a text editor(e.g. vi) then execute in case of typos. Is there a way to edit the command directly in a terminal and run instead of invoke vi by typing vi then type the command?
If you're using bash, try the edit-and-execute-command command. By default, this is assigned to Ctrl-x Ctrl-e (type ctrl-x, then ctrl-e).
This should open whatever editor is specified in your environment. Whatever is in the buffer when you exit will execute in the shell - including multiple-line commands.
You can by setting vi editing mode. If you are using bash, you can enter the following or put it in your shell configuration files:
set -o vi
You can then, just like in vi use command mode and insert mode.
When a command is already on the line (called up by pressing the up arrow key, CTRL-R, fzf, etc.), a useful command is v when in normal mode on the shell, because it will start the default editor to edit the command.
A cheat sheet from this gist:
.---------------------------------------------------------------------------.
| |
| Readline VI Editing Mode |
| Default Keyboard Shortcuts for Bash |
| Cheat Sheet |
| |
'---------------------------------------------------------------------------'
| Peteris Krumins (peter#catonmat.net), 2008.01.08 |
| http://www.catonmat.net - good coders code, great reuse |
| |
| Released under the GNU Free Document License |
'---------------------------------------------------------------------------'
======================== Keyboard Shortcut Summary ========================
.--------------.------------------------------------------------------------.
| | |
| Shortcut | Description |
| | |
'--------------'------------------------------------------------------------'
| Switching to COMMAND Mode: |
'--------------.------------------------------------------------------------'
| ESC | Switch to command mode. |
'--------------'------------------------------------------------------------'
| Commands for Entering INPUT Mode: |
'--------------.------------------------------------------------------------'
| i | Insert before cursor. |
'--------------+------------------------------------------------------------'
| a | Insert after cursor. |
'--------------+------------------------------------------------------------'
| I | Insert at the beginning of line. |
'--------------+------------------------------------------------------------'
| A | Insert at the end of line. |
'--------------+------------------------------------------------------------'
| c<mov. comm> | Change text of a movement command <mov. comm> (see below). |
'--------------+------------------------------------------------------------'
| C | Change text to the end of line (equivalent to c$). |
'--------------+------------------------------------------------------------'
| cc or S | Change current line (equivalent to 0c$). |
'--------------+------------------------------------------------------------'
| s | Delete a single character under the cursor and enter input |
| | mode (equivalent to c[SPACE]). |
'--------------+------------------------------------------------------------'
| r | Replaces a single character under the cursor (without |
| | leaving command mode). |
'--------------+------------------------------------------------------------'
| R | Replaces characters under cursor. |
'--------------+------------------------------------------------------------'
| v | Edit (and execute) the current command in the text editor. |
| | (an editor defined in $VISUAL or $EDITOR variables, or vi |
'--------------'------------------------------------------------------------'
| Basic Movement Commands (in command mode): |
'--------------.------------------------------------------------------------'
| h | Move one character right. |
'--------------+------------------------------------------------------------'
| l | Move one character left. |
'--------------+------------------------------------------------------------'
| w | Move one word or token right. |
'--------------+------------------------------------------------------------'
| b | Move one word or token left. |
'--------------+------------------------------------------------------------'
| W | Move one non-blank word right. |
'--------------+------------------------------------------------------------'
| B | Move one non-blank word left. |
'--------------+------------------------------------------------------------'
| e | Move to the end of the current word. |
'--------------+------------------------------------------------------------'
| E | Move to the end of the current non-blank word. |
'--------------+------------------------------------------------------------'
| 0 | Move to the beginning of line |
'--------------+------------------------------------------------------------'
| ^ | Move to the first non-blank character of line. |
'--------------+------------------------------------------------------------'
| $ | Move to the end of line. |
'--------------+------------------------------------------------------------'
| % | Move to the corresponding opening/closing bracket. |
'--------------'------------------------------------------------------------'
| Character Finding Commands (these are also Movement Commands): |
'--------------.------------------------------------------------------------'
| fc | Move right to the next occurance of char c. |
'--------------+------------------------------------------------------------'
| Fc | Move left to the previous occurance of c. |
'--------------+------------------------------------------------------------'
| tc | Move right to the next occurance of c, then one char |
| | backward. |
'--------------+------------------------------------------------------------'
| Tc | Move left to the previous occurance of c, then one char |
| | forward. |
'--------------+------------------------------------------------------------'
| ; | Redo the last character finding command. |
'--------------+------------------------------------------------------------'
| , | Redo the last character finding command in opposite |
| | direction. |
'--------------+------------------------------------------------------------'
| | | Move to the n-th column (you may specify the argument n by |
| | typing it on number keys, for example, 20|) |
'--------------'------------------------------------------------------------'
| Deletion Commands: |
'--------------.------------------------------------------------------------'
| x | Delete a single character under the cursor. |
'--------------+------------------------------------------------------------'
| X | Delete a character before the cursor. |
'--------------+------------------------------------------------------------'
| d<mov. comm> | Delete text of a movement command <mov. comm> (see above). |
'--------------+------------------------------------------------------------'
| D | Delete to the end of the line (equivalent to d$). |
'--------------+------------------------------------------------------------'
| dd | Delete current line (equivalent to 0d$). |
'--------------+------------------------------------------------------------'
| CTRL-w | Delete the previous word. |
'--------------+------------------------------------------------------------'
| CTRL-u | Delete from the cursor to the beginning of line. |
'--------------'------------------------------------------------------------'
| Undo, Redo and Copy/Paste Commands: |
'--------------.------------------------------------------------------------'
| u | Undo previous text modification. |
'--------------+------------------------------------------------------------'
| U | Undo all previous text modifications. |
'--------------+------------------------------------------------------------'
| . | Redo the last text modification. |
'--------------+------------------------------------------------------------'
| y<mov. comm> | Yank a movement into buffer (copy). |
'--------------+------------------------------------------------------------'
| yy | Yank the whole line. |
'--------------+------------------------------------------------------------'
| p | Insert the yanked text at the cursor. |
'--------------+------------------------------------------------------------'
| P | Insert the yanked text before the cursor. |
'--------------'------------------------------------------------------------'
| Commands for Command History: |
'--------------.------------------------------------------------------------'
| k | Move backward one command in history. |
'--------------+------------------------------------------------------------'
| j | Move forward one command in history. |
'--------------+------------------------------------------------------------'
| G | Move to history line N (for example, 15G). |
'--------------+------------------------------------------------------------'
| /string or | Search history backward for a command matching string. |
| CTRL-r | |
'--------------+------------------------------------------------------------'
| ?string or | Search history forward for a command matching string. |
| CTRL-s | (Note that on most machines Ctrl-s STOPS the terminal |
| | output, change it with `stty' (Ctrl-q to resume)). |
'--------------+------------------------------------------------------------'
| n | Repeat search in the same direction as previous. |
'--------------+------------------------------------------------------------'
| N | Repeat search in the opposite direction as previous. |
'--------------'------------------------------------------------------------'
| Completion commands: |
'--------------.------------------------------------------------------------'
| TAB or = or | List all possible completions. |
| CTRL-i | |
'--------------+------------------------------------------------------------'
| * | Insert all possible completions. |
'--------------'------------------------------------------------------------'
| Miscellaneous commands: |
'--------------.------------------------------------------------------------'
| ~ | Invert case of the character under cursor and move a |
| | character right. |
'--------------+------------------------------------------------------------'
| # | Prepend '#' (comment character) to the line and send it to |
| | the history. |
'--------------+------------------------------------------------------------'
| _ | Inserts the n-th word of the previous command in the |
| | current line. |
'--------------+------------------------------------------------------------'
| 0, 1, 2, ... | Sets the numeric argument. |
'--------------+------------------------------------------------------------'
| CTRL-v | Insert a character literally (quoted insert). |
'--------------+------------------------------------------------------------'
| CTRL-r | Transpose (exchange) two characters. |
'--------------'------------------------------------------------------------'
===========================================================================
.---------------------------------------------------------------------------.
| Peteris Krumins (peter#catonmat.net), 2008.01.08. |
| http://www.catonmat.net - good coders code, great reuse |
| |
| Released under the GNU Free Document License |
'---------------------------------------------------------------------------'
If you are using zsh the shell-command is called edit-command-line. It is not bound by default, so add something like this to your configuration:
bindkey "^X^E" edit-command-line
Now Ctrl+xCtrl+e will work the same way as in bash except that the command is not executed before Return is struck.
Have you tried the fc ("fix command") shell built-in?1
By default, it opens the very last command in your editor, but you can discard that and replace it with whatever you wish, and it gets executed on exit. See help fc.
Idea suggested by Tom Ryder in his blog post, Vi mode in Bash.
(1) Available in fish, zsh, bash, dash, but probably in others as well.
If you are using the fish shell instead of bash you can open the current command in an editor with:
Alt-v (which uses the editor set in the VISUAL environment variable)
Alt-e (which uses the editor set in the EDITOR environment variable)

How to move vertical split window to horizontal split, when there is an existing horizontal split?

In Vim I tend to open buffers in new vertical splits (with the occasional horizontal split). I keep my code to 80 chars wide, so this works pretty well on large monitors.
I often end up with this window arrangement:
---------------------
| | | | |
| | | | |
------ | A | B |
| | | | |
| | | | |
---------------------
At four or five columns wide, it can start getting a bit too narrow, so then I want to move the windows around so it looks like this:
----------------
| | | A |
| | | |
------ ------
| | | B |
| | | |
----------------
As far as I know, this is impossible to do by moving the windows in Vim.
The only way to get that window arrangement I've found, is to close window A, and then re-open A as a new horizontal split from window B.
Is that correct, or is there a way to move/re-arrange windows like that in Vim? Maybe a plugin?
I'm yet to find anything, so I thought I would ask because I find the opening/closing of windows anoying and breaks my flow.
FWIW, I find the Ctrl-W + J / Ctrl-W + K shortcuts useless, because they make the new horizontal split as wide as the whole screen, rather than splitting with the neighbouring window. I.e. Ctrl-W + J would give me this:
----------------
| | | |
| | | |
------ | A |
| | | |
| | | |
----------------
| |
| B |
----------------
Which is generally never what I want.
If anyone has some ideas, let me know!
There's a plugin that can do exactly what you want. Here's the link : https://github.com/fabi1cazenave/suckless.vim.

How to search for the Nth match in a line in Vim?

I am editing a wiki file and would like to add a new column in between of two existing columns.
| *No* | *Issue* | *File* | *Status* |
| 1 | blah | foo | open |
| 2 | blah1 | foo1 | close |
Say, I want to insert a new column between the 3rd and 4th columns above. If I could search for the fourth match of the | character in a given line, I could replace that with | |. But how one can do that in Vim?
The end result would look like so:
| *No* | *Issue* | *File* | | *Status* |
| 1 | blah | foo | | open |
| 2 | blah1 | foo1 | | close |
How about recording a macro into register q by entering qq3f|a|<ESC>q in command mode (ESC means pressing the Escape key). Now you can apply this macro to each line by :%norm#q.
Additional bonus:
With this pattern you can add more complex actions, for example replicate the first column as column 3 (if cursor is at first column):
qqf yf|;;;p0q
Oh, and the answer to your question: Search 4th occurrence of | on a line is done by 3f| (if the cursor is at position 0 and on a | character as in your example).
Consider the following substitution command.
:%s/\%(.\{-}|\)\{4}\zs/ |/
:%s/\(|[^|]*\)\{3\}/&| /
Which means: on each line (%), find three occurrences (\{3\}) of a string that starts with | followed by any number of non-| ([^|]*), and replace that with itself (&) followed by |.
You can call sed in vim as a filter:
:%!sed 's/|/| |/4'

Resources