Use Vim as a simple IDE

Facebooktwittergoogle_plusredditpinterestlinkedinmail
Vim already has all functionalities to use it as an IDE.

Vim already has all functionalities to use it as an IDE.

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
    • Debugging
  • Create own settings:
    • Line numbers, syntax highlighting, highlight cursor line, …
    • Auto completion in a drop-down menu

These are all adjustments to vim.

Basic usage

Even if you should be familiar with the modes in vim, here’re are some basic commands:

Open a file (without tabs): :e /path/to/file
Save a file: :w
Close a file: :q and without saving :q!
Show information on a command: :help 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 :q.

Here some variants of :Explore:

  • :Sexplore/:Sex Splits the screen horizontally
  • :Lexplore/:Lex Splits the screen vertically and shows the explorer on the left side
  • :Texplore/:Tex Creates a new tab where the file will be opened

You can navigate through the list of tabs with CTRL+Page-up and CTRL+Page-down.

Split the screen

Vim offers the opportunity to horizontally split the current screen with :split/:sp or to vertically split it with :vsplit/:vsp. The current file will be displayed twice.

You can now jump through all screens by hitting CTRL+W CTRL+W.

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/g replace every „old“ in the current line by „neu“
  • :%s/old/new/g replace every „old“ in the current file by „neu“
  • :%s/old/new/gc replace every „old“ in the current file by „neu“, ask for confirmation on every occurrence

Jump

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
  • gD takes the word under the cursor and goes to the first occurrence in the file
    (easy to remember: gD = goto Definition)
  • gg jumps to the beginning of the file
  • G jumps 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
  • 0 jumps 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 :!make. The ! 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.

Tipp:

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:

syntax enable Enables syntax highlighting.
:set tabstop=4 Sets the tab width to four spaces (default is eight).
:set number Shows line numbers.
:set wildmenu Shows a menu that displays auto completion results. This happens for instance when you type :tabfind and press TAB multiple times. A small menu will show you suggestions.
:set cursorline Underlines the line where the cursor is. Useful, when you search and don’t know where the cursor is.
set colorcolumn=80 Shows a bar at the 80-character-mark. Useful, when the code conventions define a maximal character number per line.
imap <C-@> <C-n> A menu will show auto completion results when pressing CTRL+SPACE. This works directly in the insert-mode.
nmap <C-D> :w <bar> !make all && gdb<cr> Saves, executes make and starts gdb when pressing CTRL+D in normal-mode.
nmap <C-C> :!bash<cr> Starts a simple bash when pressing CTRL+C in normal-mode. You cannot run a bash in a single vim-tab, so this is a simple way to open a shell without closing vim. Type exit in the shell to get back to vim.

Conclusion

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 🙂

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Comment

Your email address will not be published.