Use vim as an IDE without plugins¶
Environment¶
Update Vim¶
The default version of Vim in ubuntu 18.04 LTS is 8.0, and the newest version of Vim is 8.2.
1 2 3 4 | |
Config¶
1 | |
General Usage¶
New To Vim¶
Start Vim¶
To start Vim, type the command Vim file.txt at any command prompt. Because this is a new file, you get a blank window:
1 2 3 4 5 6 7 8 | |
#is the cursor position;~indicates lines not in the file;- at the bottom of Vim is a message line that indicates the file is named file.txt and shows you are creating a new file.
Insert Text¶
The Vim editor is a modal editor, which means that the editor behaves differently depending on which mode you are in. The two basic modes are:
- Normal mode, the characters you type are treated as commands;
- Insert mode, the characters you type are treated as text.
To get in insert mode you should type i(for Insert), and to get in normal mode you should type <ESC>.
Moving Around¶
In Normalmode, to move the cursor, press the h, j, k, l keys as indicated.
1 2 3 4 5 | |
Delete Charactors¶
In Normal mode, you can delete characters with following commands:
x: delete a character. Move the cursor over a character and typexto delete it. (This is a throwback to the old days of the typewriter, when you deleted things by typing xxxx over them.)dd: delete a line.J: delete a line break.
Undo And Redo¶
If you delete too much, you can type u to undo the last edit.
And if you undo too much, you can press CTRL-r to redo them.
Other Editing Commands¶
a: sinceiinserts a character before the cursor, you can useato append a character after the cursor.o: creates a new and empty line below the cursor and puts Vim inInsertmode.O: creates a new and empty line above the cursor and puts Vim inInsertmode.[cnt]-command: you can add a numbercntbefore command to repeat the commandcnttimes. For example, you want to move up 9 lines, you can either typekkkkkkkkkor you can type9k.
Getting Out¶
After modifying the file, you can use:
:w: to write the file;:q: to quite the Vim;:wq: to write the file and then quit the Vim;:q!: to ignore the changes and force quit Vim.
Find Help¶
Everything you always wanted to know can be found in the Vim help files. To get help on something, use the command:
:help {something}
Move Faster¶
Charactor Based Movement¶
One of the most useful movement commands is the single-character search command fx(Find x) which search forward in the line for the character x.
For example, you are at the beginning of the following line. Suppose you want to go to the h of human, just execute the command fh and the cursor will be positioned over the h:
1 2 3 | |
1 2 3 | |
F, to find backward:
1 2 3 | |
t and T, works like the f, but it stops one character before the searched character:
1 2 3 | |
Word Based Movement¶
You can also move the cursor based on words:
w, to move forward a word;
1 2 3 | |
b, to move backward a word;
1 2 3 | |
e, to the end of a word;ge, to the end of a previous word.
1 2 3 | |
Line Based Movement¶
0, to move to the start of a line;^, to move to the first character of the line;$, to move to the end of the line;gg, to move to the first line of the file;G, to move to the last line of the file;:[num], to move to [num] line.
Parenthesis Based Movement¶
When writing a program you often use pairs like (), [] and {}, you can use % to jump between them.
If the cursor is on a ( it will movet to the matching ). If it's on a ) it will move to the matching (.
1 2 3 4 5 | |
Scrolling Around¶
CTRL-U, to scroll up half a screen of text;CTRL-D, to scroll down half a screen of text;CTRL-F, to scroll forward a screen of text;CTRL-B, to scroll backward a screen of text;zz, to move the cursor line to the center of the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
Search¶
/{string}, to search forwardstringin the whole file;*, pressing*at the word you want to search works just like/{string};?{string}, to search wordstringin the whole file;#, pressing#at the word you want to search works just like/{string};
Once you searched something, you can use n to jump to next item, and N to jump to previous item.
Marks¶
Vim enables you to place your own marks in the text:
mx, to mark a the place under the cursor asx, x can bea to z;`x, to go to the marked place;marks, to place all the marks you can go to.
Change Smarter¶
Operators With Range¶
You can use the pattern [operator][count][range] to change more characters. For example, d4w means [delete][4][word]:
1 2 3 4 5 | |
d2e, means delete 2 words' end;
1 2 3 4 5 | |
d$, means delete to the end of the line;
1 2 3 4 5 | |
Change Text¶
Another operator is c, change. It acts just like the d operator, but it leaves you in Insert mode:
cw, changes a word;
1 2 3 4 5 | |
cc, changes a line and leaves you inInsertmode;
The r is not an operator, it waits for you to type a character, and will replace the character under the cursor with it.
1 2 3 4 | |
1 2 3 4 | |
Repeating A Command¶
The . may be the most simple yet powerful commands in Vim. It repeats the last change. For instance, suppose you are editing an HTML file and want to delete all the () :
1 2 3 4 5 6 7 8 | |
Visual Mode¶
Visual mode is a flexible and esay way to select a piece of text for an operator. It is the only way to select a block of text.
Select Characters¶
To delete simple items the operator-range works quite well. But often it's not so easy to decide which command will move over the text you want to change. Then you can use press v to enter the Visual mode.
You move the cursor over the text you want to work on. While you do this, the text is highlighted. Finally, you type the operator command.
1 2 3 4 5 | |
Select Lines¶
If you want to work on whole lines, use V to start Visual mode.
1 2 3 4 5 6 7 | |
Select Blocks¶
If you want to work on a rectangular block of characters, use CTRL-v to start Visual mode. This will be really useful when you comment several code lines.
Go To Other Side¶
If you have selected some text in Visual mode, and discover that you need to change other end of seleqction, use o to go to other side.
Copy And Paste¶
Yanking is a Vim name for copying, and you can use the operator yw to copy a word, a count is possible as usual.
1 2 3 4 5 6 7 8 | |
yy command yanks a whole line, just like dd deletes a whole line.
And stil, you can first use visual mode to select some characters and then yank them.
Other Useful Commands¶
~: Change case of the character under the cursor;u(visual mode): Make selected characters lower case;U(visual mode): Make selected characters upper case;I: StartInsertmode after moving the cursor to the first no-blank in the line;A: StartInsertmode after moving the cursor to the end of the line;di(ordi): Delete all characters between();di[ordi]: Delete all characters between[];di{ordi}: Delete all characters between{};da(orda): Delete all characters between()and();da[orda]: Delete all characters between[]and[];da{orda}: Delete all characters between{}and{};
Record¶
You can record your multiple operators to a register {0-9a-zA-Z}.
q{0-9a-zA-Z}to start recording operators and commands to register{0-9a-zA-Z}a;qto stop recording;@{0-9a-zA-Z}to replay the operators and commands saved in register{0-9a-zA-Z}.@@to repeat previous record.
Replace¶
Use the pattern :[range]s/origin_str/replace_str/[flag] to replace origin_str with replace_str in [range].
For example, you can use :%s/one/two/g to replace all one in the file with two.
The [range] can be:
%, means in all lines;1, 15, means in1-15lines;., +5, means fromcurrentline tocurrent + 5line;5, $, means from line5to the end of file.
And the [flag] can be:
(empty), means only replacing once;g, means replacing all;c, means you need to comfirm each replacement;gc, means replacing and you need to comfirm each replacement.
Advanced Features¶
Edit Multiple Files¶
No matter how many files you have, you can edit them without leaving Vim.
Edit Another File¶
So far you had to start Vim for every file you wanted to edit. To edit another file, use :edit path/to/foo.txt to open the file foo.txt.
Jump Between Files¶
After editing another file, the file you edited just now is not closed. Instead, it's stored in a buff, you can use:
:buffersor:lsto show all the buffers;:bnextto jump to next buff;:bpreviousto jump to previous buff;:blastto jump to the last buff;:bfirstto jump to the first buff;:buff[num]to jump to buff [num];
Rename¶
After modifying the file, if you need to save the file under a new name,
1 | |
1 | |
Split Windows¶
Display two different files above files above each other, or view two locations in the file at the same time. See the difference between two files by putting them side by side. All this is possible with split windows.
Split Window On One File¶
The easiest way to open a new window is to use the command
1 | |
1 2 3 4 5 6 7 8 9 10 | |
You can use the command
1 | |
:only to close all other windows.
Split Window On Different Files¶
You can use the command
1 | |
1 2 3 4 5 6 7 8 9 10 | |
You can use
1 | |
1 | |
Move Between Windows¶
CTRL-w hto move to the window on the left;CTRL-w lto move to the window on the right;CTRL-w jto move to the window below;CTRL-w kto move to the window above;CTRL-w tto move to the top window;CTRL-w bto move to the bottom window;
Resize Window¶
CTRL-w =to make all windows equally high and wide;CTRL-w [num]+to increase the window's height [num] lines;CTRL-w [num]-to decrease the window's height [num] lines;CTRL-w [num]<to decrease the window's width [num] lines;CTRL-w [num]>to increase the window's width [num] lines;
Moving Window¶
Now you have split a few windows, but they may be in the wrong place. Then you need a command to move the window somewhere else.
- CTRL-w K to move window to the top;
- CTRL-w J to move window to the bottom;
- CTRL-w H to move window to the far left;
- CTRL-w L to move window to the far right;
Tab Pages¶
You will have noticed that windows never overlap. That means you quickly run out of screen space. The solution for this is called Tab pages.
Assume you are editing thisfile, to create a new tab page use the command:
1 | |
1 2 3 4 5 6 7 8 9 10 | |
:tabclose to close a tab page;
- :tabonly to close all other tab pages;
- gt to jump to next tab;
- gT to jump to previous tab;
- [num]gt to jump to [num]th tab.
Fold¶
Structured text can be separated in sections. Folding allows you to display a section as one line, providing an overview.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Try:
zcto close a fold;zoto open a fold;zrto release a fold and its sub-fold;zmto make a fold and its sub-fold;zRto release all folds and sub-folds;zmto make all folds and sub-folds;
Code Complete¶
Vim can auto complete words according to text.
CTRL-nto complete anything;CTRL-x CTRL-nto complete in this file;CTRL-x CTRL-fto complete filenames;CTRL-x CTRL-]to complete in tags;- Once the matching items appear, you can use
CTRL-nto jump to next one andCTRL-pto previous one.
Tag Jump¶
This feature is based on the software ctags and makes you jump to the defination of the function.
1 | |
You can use ctags -R . to generate tags of a repo.
Try:
CTRL-]to jump to tag under the cursor;CTRL-oorCTRL-tto jump back;:tsto show all matching items.
Communicate With Terminal¶
Execute Bash Commands¶
You can excute a bash command with the pattern :! [command]:
:! bash build.shto build the project;:! git statusto check the file change.
Builtin Commands¶
Some builtin commands are available and useful.
grep¶
:grep [pattern] -r . will grep all pattern lines and put them in quickfix window.
You can use
:cwto openquickfixwindow;:cnto jump to next matching item;:cpto jump to previous matching item;
read¶
:read ! [command] will read the command executing result to cursor position.
Try:
:r ! lsto copy all filenames to current file;:r ! dateto copy date to the file.
Jump Between Terminal And Vim¶
- Use
:shellto start a shell; when the shell exits(afterexitcommand orCTRL-d) you return to Vim. - Use
CTRL-zto suspend current process; after your work, usefgto bring it to the foreground.
Terminal Mode¶
The terminal feature is supported after Vim 8.2. This feature is for running a terminal emulator in a Vim window.
You can use:
:terminalto create a new terminal window;:vertical terminalto create a new vertical terminal window;CTRL-w h/j/k/lto jump between terminal windows;CTRL-w H/J/K/Lto move terminal window;CTRL-w Nto back to normal mode;
Compile(C++)¶
Generally, if you want to compile a single cpp file, you can use g++. For a large project with lots of source file, you can use the builtin command make.
g++¶
1 | |
%represents for current file, you can replace it with its real name;-gto generate symbols forgdb;-o outto place output to the fileout;- you can add other flags.
make¶
The following command runs the program make(supplying it with any argument you give) and captures the results:
1 | |
For example, :make -directory=build will make the project in the folder build and put the warnings and errors to quickfix window.
You can use
:cwto openquickfixwindow;:cnto jump to next error;:cpto jump to previous error;:cfirstto jump to the first error;:clastto jump to the last error;
Debug¶
Vim has a useful builtin debugger plugin, termdebug, which provides a visual interface for interacting with gdb.
Load The termdebug Plugin¶
After loading source code in current window, you can load the plugin with:
1 2 | |
This will open two other windows:
- gdb window: A terminal window in which
gdbis executed. - program window: A terminal window for the executed program. The output of program will appear here.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
General Debug(gdb)¶
Firstly, make sure the program you generated contains symbols. If you compile the program with:
- g++, you should add the
-goption; - make, you should add the
-ggdboption.
Then, you can load the program in gdb window with the command:
1 | |
This works the same as any command a gdb running in a terminal.
Some gdb commands:
b: set a break point;d: delete a break point;set args: set running args;show args: show args;r: run the program;start: run the program and stop at themainfunction;c: continue current process;n: next step;s: step in;finish: stop current program;until: jump out of current loop;until+linenumber: run util thelinenumber;info locals: show current local variables;p+variable: print the value of a variable;set var key=value: set the variable key to a new value;p key=value: same asset var key=value;bt: show trace of where you are currently, which functions you are in. Prints stack backtrace.CTRL-c: stop current gdb command;q: quit gdb.
Core Dump¶
A core dump is the printing or the copying to a more permanent medium(such as hard disk) the contents of RAM at one moment in a time.You can think it as a full length "snapshot" of RAM.
If your program got a core dump bu no file generated, you should set the core dump file size limit:
1 | |
You can use the command:
1 | |
Or you can use the command:
1 | |
gdb window to load the core dump file.
Crash¶
You can use the command bt to backtrace the stack status when crash appears in gdb window.
Debug Cyberrt Module¶
You can use gdb to either launch a module or attach to a running module.
Use Termdebug To Load Module¶
In gdb window, use
1 | |
mainboard, and use
1 | |
1 | |
Attach To A Running Module¶
- Get PID of prediction module:
1ps aux | grep prediction - Load
mainboardsymbols:1file /home/caros/opt/bin/mainboard - Attach to prediction process:
1attach PID
Use Vim Mode In Other apps¶
bash¶
set -o viwill use thevicommands;set -o emacswill use theemacscommands.