Execute Script Without Exiting Vim

Two ways,

Say you are writing a python or bash script, you can say

:!scriptname

This assumes you have your shebang line at the beginning of the file. This would work well.

Else do this for more options,

:set makeprg=python\ %
:make
Advertisements

Matlab – Without GUI – Part 1.5/2

I said I would return in a week or so to talk about my experiences with using Matlab without a GUI in part 1/2 of this series. I also said I would get back with a report on how debugging works in a command line environment because it could be pretty important for a lot of us. But it seems Matlab has a pretty good setup to debug using the command line. This is just awesome and I am so happy.

With the following commands, you will be well setup to do some basic debugging. You can refer the help docs for your specific needs. This will give you a quick review of what you can do.

Set breakpoints at different places in the script using dbstop. Mention the script name and the line number where you need to stop, using the following line of code on the command line. dbstop in scriptname at linenumber

Look at all the places you have used a breakpoint using, dbstatus. Just type in dbstatus in the command line and it will let you know.

When you are running the program, it would stop at the places where you have added breakpoints. When you are done analyzing your variables and what not, you can type in dbcont to continue execution until it encounters the next breakpoint, or the end of the program.

You can use dbstep to step one line at a time just like how you would with the GUI. You can also say something like dbstep 5 to step through five lines of code.

Remove breakpoints in a line or, all of it in a file or, all of it in all files using dbclear. dbclear all will clear all of it in all places. dbclear in script/filename will clear all breakpoints in a file. You can also add line number to remove something specific from a specific place.

Last, use dbquit to exit debugging mode.

For more interesting ways to debug, take a look at the help files. They have a load of information along with examples. You can use some of these commands with an if statement. Like for example, adding a break point when some condition is satisfied. This could be very useful. A lot of actual debugging does happen this way when you are working with a lot of code. Happy debugging.

It has not been one week yet. So I named this post 1.5/2. You can only really get a good picture of things over time. So it is not just a sensation but rather the actual fact. I will be back again in a week or so with my conclusions.

Matlab – Without GUI – Part 1/2

Matlab can be invoked without its GUI on linux systems by typing in the following on the terminal.

matlab -nodesktop

This opens up matlab just like an Octave or Python prompt on the terminal. What is the use you may ask. You might be perfectly happy with the IDE like environment. But personally, there is something nice about using the terminal. Less fuss and more work. Makes you feel at home. If you are like me. Then you should try it.

I can use vim to edit my scripts. I can run my matlab scripts by typing in run scriptname.m. I can see all the variables by typing in who. I can see all the details of the variables by typing in whos. It gives me the type, shape and name of the variables. I can just type in the name of the variable to print it out.

Wouldn’t that be ‘a lot’ of what we would require? It feels really similar to using python scripts with scipy and numpy packages for signal processing. Sometimes I am using vim even when i am using the Matlab GUI as well. I use the gui just to run the code and look at the variables.

Wouldn’t it be possible to just use a text editor and then run the code alone on the terminal without starting up matlab in the first place? Yeah, technically Yes, with some other arguments you can do that. But every time you run it, matlab seems to load everything and you’d probably have to wait ten to twenty seconds for an output. That is not efficient.

Yeah, line by line debugging would also be really helpful sometimes. I will see if there are any workarounds for that, by the time i write part 2 of this blog. I will work with this for a week or so and get back with some updates about whether I am going to continue using it this way.

Executing Shell commands inside Vim

Shell commands can be executed inside vim. You can use ! after the : and type in your command. You need to know just one more thing and you can be off. The current file is specified by the % character. For for example, if you want to find the word count of the current file, type in, :! wc %. If you want for example to execute the script you are writing then :! bash %, will run the current bash script you are writing. The display will momentarily go out of vim to display the results but will be back to vim as soon as you hit Enter.

Vim – Quick Start

Although I am typing this blog post on vim with html. I sometimes do not use Vim. I am all around the place with Atom sometimes, or Gedit or pluma or Visual Studio Code sometimes. But then I come back to vim for the simplicity of things. When I do, i sometimes forget what i need to know to get back to my old Vim state. This one is for me to read this blog post in two minutes and getting back to work.

I don’t think i have to say what mode these commands have to be used in.

  • i to insert text
  • Esc to exit edit mode
  • v for visual select
  • shift + v or V for visual lines
  • ctrl + vfor visual block
  • y to yank/copy
  • x to cut/delete
  • p to paste on the next line.
  • shift + p or P to paste right where the cursor is.
  • :w to write
  • :q to quit
  • :wq to save and quit
  • :e filename to open a new buffer with file specified
  • :ls to list all the buffers. # is the previous buffer. %a is the active buffer
  • :b buffernumber to switch to that buffer
  • :b # to switch to the previous buffer
  • :bd buffernumber to delete a particular buffer.
  • u to undo

This should be enought to get you going.

Vim – Manual Syntax Highlighting

Sometimes I write scripts without the usual extension “.sh” or “.scr” as some people do. I am also writing this blog post on vim on just a text file. But i use html tags on it. I don’t like the text file to have the “.html” extension on it. So i manually turn on the syntax highlighting. Vim also picks up the syntax from the shebang line, but this is for cases when even that isn’t present.

Pretty simple this one. :set syntax=html or what ever your code is. Done!

Vim – Edit Multiple Files – Multiple Buffers

If you want to work with multiple files at the same time and if you are new to vim you might not know what buffers are about. They’re really useful. Here is a short blog on how to use them.

  • When you start vim you can start with multiple file arguments in the first place. Then all of them are opened together in different buffers.
  • When you want to open another file as another buffer you can say, :e filename/path
  • You can say :ls to list the active buffers. They are ordered and numbered.
  • You can use the list number of the buffer to switch to that buffer, by saying :b #. Replace # with the number.
  • You can also switch using the file name by saying,:b filename/path. Partially filling the name of the file and pressing tab also works.
  • To delete a buffer say :bd #. As previously mentioned, replace # with the list number. You can also use the filename/path.
  • Before you can switch from one buffer to another it needed to be saved by saying, :w. Not sure if this can be changed in some global settings.