Everybody can install an IDE and use it, but how to use vim as an IDE? I’ll show you settings, shortcuts and features of vim, which turns it into an IDE without using plugins or magic, just plain vim features.
These are the features I’ll focus on:
- Features out of the box:
- Basic usages: Open, save, close, …
- File explorer
- Create tabs and use them
- Split the window
- Navigation in vim:
- Find, replace, …
- Jump (e.g. to the next occurrence, definition, …)
- Build and run software
- Create own settings:
- Line numbers, syntax highlighting, highlight cursor line, …
- Auto completion in a drop-down menu
These are all adjustments to vim.
Even if you should be familiar with the modes in vim, here’re are some basic commands:
Open a file (without tabs):
Save a file:
Close a file:
:q and without saving
Show information on a command:
Built-in File explorer
Vim already has its own terminal based but somehow graphical file explorer which you can open it with
:Explore (not the capital E) – or for short
:Ex – and navigate within it. You can now either choose a file, which will be opened, or close the explorer with
Here some variants of
:SexSplits the screen horizontally
:LexSplits the screen vertically and shows the explorer on the left side
:TexCreates a new tab where the file will be opened
You can navigate through the list of tabs with
Split the screen
Vim offers the opportunity to horizontally split the current screen with
:sp or to vertically split it with
:vsp. The current file will be displayed twice.
You can now jump through all screens by hitting
Combining this with tabs works, but only the current tab page will be split, the other pages remain unchanged.
Search and replace
Linux often uses the command
/pattern (e.g. in the pager less) to search for the given
pattern, which is also supported by vim in the normal-mode. Just start typing it, confirm with
Enter and vim searches from the cursor position downwards in the file. It’ll jumpt to the first occurrence and you can use
n to jump to the next or
N to jump to the previous one.
Replacing string works quite similar:
:s/old/new/replace the first „old“ in the current line by „neu“
:s/old/new/greplace every „old“ in the current line by „neu“
:%s/old/new/greplace every „old“ in the current file by „neu“
:%s/old/new/gcreplace every „old“ in the current file by „neu“, ask for confirmation on every occurrence
Besides searching a string, there’re some useful shortcuts to find certain things:
*takes the word under the cursor and searches for the next occurrence
#takes the word under the cursor and searches for the previous occurrence
gDtakes the word under the cursor and goes to the first occurrence in the file
(easy to remember: gD = goto Definition)
ggjumps to the beginning of the file
Gjumps to the end of the file
$jumps to the end of the line
^/_jumps to the first character in the line that is not a white-space-character
0jumps to the absolute start of the line
Build, execute and debug
Due to the fact that I don’t use any plugins in this article, I’ll show some tricks to simplify the usage of tools like gdb.
Vim can execute bash commands for example to build your software with make by typing
! executes the subsequent command as an “external command” which will open a bash and execute the command there.
Let’s say the make command creates a file called
main, then the command
:!make && ./main will build and run your application. As a result of the command vim will open a new bash and executes the command there. Same thing happens by typing
:!gdb, a bash will open running gdb.
You should save before building your software, so combine
:w with make into
:w | !make && gdb. This will save, build and start gdb. The pipe (meaning the
| character) concatenates commands like the
&& does it in shell scripts.
Finally making it even more simple, you can create a shortcut in the
.vimrc file (located in your home folder):
nmap <C-D> :w <bar> !make all && gdb<cr>Therefore you can use
CTRL+D in normal-mode to save, build and start gdb.
By using the terminal multiplexer
screen, you can use gdb and vim next to each other. This is handy when you code in the terminal only.
Adjustments to vim
As mentioned above, the .vimrc file (located in the home folder) can be used to change or add functionality to vim. These are my default settings for a simple vim-IDE:
||Enables syntax highlighting.|
||Sets the tab width to four spaces (default is eight).|
||Shows line numbers.|
||Shows a menu that displays auto completion results. This happens for instance when you type
||Underlines the line where the cursor is. Useful, when you search and don’t know where the cursor is.|
||Shows a bar at the 80-character-mark. Useful, when the code conventions define a maximal character number per line.|
||A menu will show auto completion results when pressing
||Saves, executes make and starts gdb when pressing
||Starts a simple bash when pressing
You can achieve 70% of IDE functionality without using plugins and further 29% with shortcuts and adjustment in the
.vimrc. Plugins may just add the remaining one percent of functionality. Perhaps this one percent adds the final touch to vim but think about it, before installing tons of plugins. The default vim functionality is much more slim and causes less problems.
Let me know what vim-IDE-tricks you use 🙂